1051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haasepackage android.animation;
2051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase
3051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase/**
4a33de55404eb2133d1bae2add3f6e8708459f56dChet Haase * This class provides a simple callback mechanism to listeners that is synchronized with all
5a33de55404eb2133d1bae2add3f6e8708459f56dChet Haase * other animators in the system. There is no duration, interpolation, or object value-setting
6a33de55404eb2133d1bae2add3f6e8708459f56dChet Haase * with this Animator. Instead, it is simply started, after which it proceeds to send out events
7a33de55404eb2133d1bae2add3f6e8708459f56dChet Haase * on every animation frame to its TimeListener (if set), with information about this animator,
8a33de55404eb2133d1bae2add3f6e8708459f56dChet Haase * the total elapsed time, and the elapsed time since the previous animation frame.
9051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase */
10051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haasepublic class TimeAnimator extends ValueAnimator {
11051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase
12051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    private TimeListener mListener;
13051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    private long mPreviousTime = -1;
14051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase
15051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    @Override
1608d05e3d1d6ade6924266296033981a96b47d5fbDaniel Sandler    public void start() {
1708d05e3d1d6ade6924266296033981a96b47d5fbDaniel Sandler        mPreviousTime = -1;
1808d05e3d1d6ade6924266296033981a96b47d5fbDaniel Sandler        super.start();
1908d05e3d1d6ade6924266296033981a96b47d5fbDaniel Sandler    }
2008d05e3d1d6ade6924266296033981a96b47d5fbDaniel Sandler
2108d05e3d1d6ade6924266296033981a96b47d5fbDaniel Sandler    @Override
22051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    boolean animationFrame(long currentTime) {
23051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase        if (mListener != null) {
24051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase            long totalTime = currentTime - mStartTime;
25051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase            long deltaTime = (mPreviousTime < 0) ? 0 : (currentTime - mPreviousTime);
26051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase            mPreviousTime = currentTime;
27051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase            mListener.onTimeUpdate(this, totalTime, deltaTime);
28051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase        }
29051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase        return false;
30051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    }
31051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase
32051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    /**
33051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase     * Sets a listener that is sent update events throughout the life of
34051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase     * an animation.
35051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase     *
36051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase     * @param listener the listener to be set.
37051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase     */
38051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    public void setTimeListener(TimeListener listener) {
39051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase        mListener = listener;
40051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    }
41051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase
42051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    @Override
43051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    void animateValue(float fraction) {
44051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase        // Noop
45051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    }
46051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase
47051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    @Override
48051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    void initAnimation() {
49051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase        // noop
50051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    }
51051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase
52051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    /**
53051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase     * Implementors of this interface can set themselves as update listeners
54051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase     * to a <code>TimeAnimator</code> instance to receive callbacks on every animation
55051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase     * frame to receive the total time since the animator started and the delta time
56a33de55404eb2133d1bae2add3f6e8708459f56dChet Haase     * since the last frame. The first time the listener is called,
57a33de55404eb2133d1bae2add3f6e8708459f56dChet Haase     * deltaTime will be zero. The same is true for totalTime, unless the animator was
58a33de55404eb2133d1bae2add3f6e8708459f56dChet Haase     * set to a specific {@link ValueAnimator#setCurrentPlayTime(long) currentPlayTime}
59a33de55404eb2133d1bae2add3f6e8708459f56dChet Haase     * prior to starting.
60051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase     */
61051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    public static interface TimeListener {
62051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase        /**
63051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase         * <p>Notifies listeners of the occurrence of another frame of the animation,
64051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase         * along with information about the elapsed time.</p>
65051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase         *
66051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase         * @param animation The animator sending out the notification.
67a33de55404eb2133d1bae2add3f6e8708459f56dChet Haase         * @param totalTime The total time elapsed since the animator started, in milliseconds.
68a33de55404eb2133d1bae2add3f6e8708459f56dChet Haase         * @param deltaTime The time elapsed since the previous frame, in milliseconds.
69051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase         */
70051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase        void onTimeUpdate(TimeAnimator animation, long totalTime, long deltaTime);
71051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase
72051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    }
73051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase}
74