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