1package android.animation; 2 3/** 4 * This class provides a simple callback mechanism to listeners that is synchronized with all 5 * other animators in the system. There is no duration, interpolation, or object value-setting 6 * with this Animator. Instead, it is simply started, after which it proceeds to send out events 7 * on every animation frame to its TimeListener (if set), with information about this animator, 8 * the total elapsed time, and the elapsed time since the previous animation frame. 9 */ 10public class TimeAnimator extends ValueAnimator { 11 12 private TimeListener mListener; 13 private long mPreviousTime = -1; 14 15 @Override 16 public void start() { 17 mPreviousTime = -1; 18 super.start(); 19 } 20 21 @Override 22 boolean animationFrame(long currentTime) { 23 if (mListener != null) { 24 long totalTime = currentTime - mStartTime; 25 long deltaTime = (mPreviousTime < 0) ? 0 : (currentTime - mPreviousTime); 26 mPreviousTime = currentTime; 27 mListener.onTimeUpdate(this, totalTime, deltaTime); 28 } 29 return false; 30 } 31 32 /** 33 * Sets a listener that is sent update events throughout the life of 34 * an animation. 35 * 36 * @param listener the listener to be set. 37 */ 38 public void setTimeListener(TimeListener listener) { 39 mListener = listener; 40 } 41 42 @Override 43 void animateValue(float fraction) { 44 // Noop 45 } 46 47 @Override 48 void initAnimation() { 49 // noop 50 } 51 52 /** 53 * Implementors of this interface can set themselves as update listeners 54 * to a <code>TimeAnimator</code> instance to receive callbacks on every animation 55 * frame to receive the total time since the animator started and the delta time 56 * since the last frame. The first time the listener is called, 57 * deltaTime will be zero. The same is true for totalTime, unless the animator was 58 * set to a specific {@link ValueAnimator#setCurrentPlayTime(long) currentPlayTime} 59 * prior to starting. 60 */ 61 public static interface TimeListener { 62 /** 63 * <p>Notifies listeners of the occurrence of another frame of the animation, 64 * along with information about the elapsed time.</p> 65 * 66 * @param animation The animator sending out the notification. 67 * @param totalTime The total time elapsed since the animator started, in milliseconds. 68 * @param deltaTime The time elapsed since the previous frame, in milliseconds. 69 */ 70 void onTimeUpdate(TimeAnimator animation, long totalTime, long deltaTime); 71 72 } 73} 74