From 051d35e41f7b21cd8a1608bdce10cf70952c6be4 Mon Sep 17 00:00:00 2001 From: Chet Haase Date: Tue, 14 Dec 2010 07:20:58 -0800 Subject: Adding TimeAnimator capability (hidden for now) This new class allows listeners to receive callbacks with elapsed time that are sent on the same animation updates as other animators in the system. It will allow simulations that go beyond the current animation system while handling the actual animation timing system and ensuring that those simulations sync up with other typical animations. Change-Id: Iac91c39634218793f6598a7dec5ed71dc9630258 --- core/java/android/animation/TimeAnimator.java | 78 ++++++++++++++++++++++++++ core/java/android/animation/ValueAnimator.java | 14 ++--- 2 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 core/java/android/animation/TimeAnimator.java diff --git a/core/java/android/animation/TimeAnimator.java b/core/java/android/animation/TimeAnimator.java new file mode 100644 index 0000000..0a96d59 --- /dev/null +++ b/core/java/android/animation/TimeAnimator.java @@ -0,0 +1,78 @@ +package android.animation; + +/** + * This class provides a simple callback mechanism to listeners that is synchronized with other + * animators in the system. There is no duration, interpolation, or object value-setting + * with this Animator. Instead, it is simply started and proceeds to send out events on every + * animation frame to its TimeListener (if set), with information about this animator, + * the total elapsed time, and the time since the last animation frame. + * + * @hide + */ +public class TimeAnimator extends ValueAnimator { + + private TimeListener mListener; + private long mPreviousTime = -1; + + @Override + boolean animationFrame(long currentTime) { + if (mPlayingState == STOPPED) { + mPlayingState = RUNNING; + if (mSeekTime < 0) { + mStartTime = currentTime; + } else { + mStartTime = currentTime - mSeekTime; + // Now that we're playing, reset the seek time + mSeekTime = -1; + } + } + if (mListener != null) { + long totalTime = currentTime - mStartTime; + long deltaTime = (mPreviousTime < 0) ? 0 : (currentTime - mPreviousTime); + mPreviousTime = currentTime; + mListener.onTimeUpdate(this, totalTime, deltaTime); + } + return false; + } + + /** + * Sets a listener that is sent update events throughout the life of + * an animation. + * + * @param listener the listener to be set. + */ + public void setTimeListener(TimeListener listener) { + mListener = listener; + } + + @Override + void animateValue(float fraction) { + // Noop + } + + @Override + void initAnimation() { + // noop + } + + /** + * Implementors of this interface can set themselves as update listeners + * to a TimeAnimator instance to receive callbacks on every animation + * frame to receive the total time since the animator started and the delta time + * since the last frame. The first time the listener is called, totalTime and + * deltaTime should both be zero. + * + * @hide + */ + public static interface TimeListener { + /** + *

Notifies listeners of the occurrence of another frame of the animation, + * along with information about the elapsed time.

+ * + * @param animation The animator sending out the notification. + * @param totalTime The + */ + void onTimeUpdate(TimeAnimator animation, long totalTime, long deltaTime); + + } +} diff --git a/core/java/android/animation/ValueAnimator.java b/core/java/android/animation/ValueAnimator.java index 1542c49..50082f9 100755 --- a/core/java/android/animation/ValueAnimator.java +++ b/core/java/android/animation/ValueAnimator.java @@ -60,9 +60,9 @@ public class ValueAnimator extends Animator { * Values used with internal variable mPlayingState to indicate the current state of an * animation. */ - private static final int STOPPED = 0; // Not yet playing - private static final int RUNNING = 1; // Playing normally - private static final int SEEKED = 2; // Seeked to some time value + static final int STOPPED = 0; // Not yet playing + static final int RUNNING = 1; // Playing normally + static final int SEEKED = 2; // Seeked to some time value /** * Internal variables @@ -74,13 +74,13 @@ public class ValueAnimator extends Animator { // The first time that the animation's animateFrame() method is called. This time is used to // determine elapsed time (and therefore the elapsed fraction) in subsequent calls // to animateFrame() - private long mStartTime; + long mStartTime; /** * Set when setCurrentPlayTime() is called. If negative, animation is not currently seeked * to a value. */ - private long mSeekTime = -1; + long mSeekTime = -1; // TODO: We access the following ThreadLocal variables often, some of them on every update. // If ThreadLocal access is significantly expensive, we may want to put all of these @@ -178,7 +178,7 @@ public class ValueAnimator extends Animator { * has been cancel()'d or end()'d since the last animation frame. Possible values are * STOPPED, RUNNING, SEEKED. */ - private int mPlayingState = STOPPED; + int mPlayingState = STOPPED; /** * Flag that denotes whether the animation is set up and ready to go. Used to @@ -1051,7 +1051,7 @@ public class ValueAnimator extends Animator { * @return true if the animation's duration, including any repetitions due to * repeatCount has been exceeded and the animation should be ended. */ - private boolean animationFrame(long currentTime) { + boolean animationFrame(long currentTime) { boolean done = false; if (mPlayingState == STOPPED) { -- cgit v1.1