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    boolean animationFrame(long currentTime) {
17        if (mListener != null) {
18            long totalTime = currentTime - mStartTime;
19            long deltaTime = (mPreviousTime < 0) ? 0 : (currentTime - mPreviousTime);
20            mPreviousTime = currentTime;
21            mListener.onTimeUpdate(this, totalTime, deltaTime);
22        }
23        return false;
24    }
25
26    /**
27     * Sets a listener that is sent update events throughout the life of
28     * an animation.
29     *
30     * @param listener the listener to be set.
31     */
32    public void setTimeListener(TimeListener listener) {
33        mListener = listener;
34    }
35
36    @Override
37    void animateValue(float fraction) {
38        // Noop
39    }
40
41    @Override
42    void initAnimation() {
43        // noop
44    }
45
46    /**
47     * Implementors of this interface can set themselves as update listeners
48     * to a <code>TimeAnimator</code> instance to receive callbacks on every animation
49     * frame to receive the total time since the animator started and the delta time
50     * since the last frame. The first time the listener is called,
51     * deltaTime will be zero. The same is true for totalTime, unless the animator was
52     * set to a specific {@link ValueAnimator#setCurrentPlayTime(long) currentPlayTime}
53     * prior to starting.
54     */
55    public static interface TimeListener {
56        /**
57         * <p>Notifies listeners of the occurrence of another frame of the animation,
58         * along with information about the elapsed time.</p>
59         *
60         * @param animation The animator sending out the notification.
61         * @param totalTime The total time elapsed since the animator started, in milliseconds.
62         * @param deltaTime The time elapsed since the previous frame, in milliseconds.
63         */
64        void onTimeUpdate(TimeAnimator animation, long totalTime, long deltaTime);
65
66    }
67}
68