1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.animation; 18 19 import android.view.animation.AnimationUtils; 20 21 /** 22 * This class provides a simple callback mechanism to listeners that is synchronized with all 23 * other animators in the system. There is no duration, interpolation, or object value-setting 24 * with this Animator. Instead, it is simply started, after which it proceeds to send out events 25 * on every animation frame to its TimeListener (if set), with information about this animator, 26 * the total elapsed time, and the elapsed time since the previous animation frame. 27 */ 28 public class TimeAnimator extends ValueAnimator { 29 30 private TimeListener mListener; 31 private long mPreviousTime = -1; 32 33 @Override 34 public void start() { 35 mPreviousTime = -1; 36 super.start(); 37 } 38 39 @Override 40 boolean animationFrame(long currentTime) { 41 if (mListener != null) { 42 long totalTime = currentTime - mStartTime; 43 long deltaTime = (mPreviousTime < 0) ? 0 : (currentTime - mPreviousTime); 44 mPreviousTime = currentTime; 45 mListener.onTimeUpdate(this, totalTime, deltaTime); 46 } 47 return false; 48 } 49 50 @Override 51 public void setCurrentPlayTime(long playTime) { 52 long currentTime = AnimationUtils.currentAnimationTimeMillis(); 53 mStartTime = Math.max(mStartTime, currentTime - playTime); 54 animationFrame(currentTime); 55 } 56 57 /** 58 * Sets a listener that is sent update events throughout the life of 59 * an animation. 60 * 61 * @param listener the listener to be set. 62 */ 63 public void setTimeListener(TimeListener listener) { 64 mListener = listener; 65 } 66 67 @Override 68 void animateValue(float fraction) { 69 // Noop 70 } 71 72 @Override 73 void initAnimation() { 74 // noop 75 } 76 77 /** 78 * Implementors of this interface can set themselves as update listeners 79 * to a <code>TimeAnimator</code> instance to receive callbacks on every animation 80 * frame to receive the total time since the animator started and the delta time 81 * since the last frame. The first time the listener is called, 82 * deltaTime will be zero. The same is true for totalTime, unless the animator was 83 * set to a specific {@link ValueAnimator#setCurrentPlayTime(long) currentPlayTime} 84 * prior to starting. 85 */ 86 public static interface TimeListener { 87 /** 88 * <p>Notifies listeners of the occurrence of another frame of the animation, 89 * along with information about the elapsed time.</p> 90 * 91 * @param animation The animator sending out the notification. 92 * @param totalTime The total time elapsed since the animator started, in milliseconds. 93 * @param deltaTime The time elapsed since the previous frame, in milliseconds. 94 */ 95 void onTimeUpdate(TimeAnimator animation, long totalTime, long deltaTime); 96 97 } 98 } 99