ValueAnimator.java revision 56b0b570890dbb7baa00da95f5be2eb3e0122f0a
1a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase/*
2a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Copyright (C) 2010 The Android Open Source Project
3a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *
4a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Licensed under the Apache License, Version 2.0 (the "License");
5a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * you may not use this file except in compliance with the License.
6a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * You may obtain a copy of the License at
7a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *
8a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *      http://www.apache.org/licenses/LICENSE-2.0
9a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *
10a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Unless required by applicable law or agreed to in writing, software
11a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * distributed under the License is distributed on an "AS IS" BASIS,
12a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * See the License for the specific language governing permissions and
14a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * limitations under the License.
15a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */
16a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
17a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haasepackage android.animation;
18a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
19c615c6fc9caca76cd96998f86e1f1e6393aeadbbTor Norbyeimport android.annotation.CallSuper;
207764b920f21e0b9250122ff26533d5dac98df6b3George Mountimport android.annotation.IntDef;
21a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.os.Looper;
2218772ea228f3d292629c4f0b5f6392d047e0530dRomain Guyimport android.os.Trace;
232970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haaseimport android.util.AndroidRuntimeException;
24c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brownimport android.util.Log;
25a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.view.animation.AccelerateDecelerateInterpolator;
26a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.view.animation.AnimationUtils;
2727c1d4debb3848f5accd5673fffeeacad3e61648Chet Haaseimport android.view.animation.LinearInterpolator;
28a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
297764b920f21e0b9250122ff26533d5dac98df6b3George Mountimport java.lang.annotation.Retention;
307764b920f21e0b9250122ff26533d5dac98df6b3George Mountimport java.lang.annotation.RetentionPolicy;
31a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport java.util.ArrayList;
32a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport java.util.HashMap;
33a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
34a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase/**
35a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This class provides a simple timing engine for running animations
36a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * which calculate animated values and set them on target objects.
37a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *
38a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>There is a single timing pulse that all animations use. It runs in a
39a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * custom handler to ensure that property changes happen on the UI thread.</p>
40a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *
41a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>By default, ValueAnimator uses non-linear time interpolation, via the
42a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link AccelerateDecelerateInterpolator} class, which accelerates into and decelerates
43a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * out of an animation. This behavior can be changed by calling
44e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase * {@link ValueAnimator#setInterpolator(TimeInterpolator)}.</p>
453aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez *
46d430753cba09acb07af8b313286f247c78a41a32Chet Haase * <p>Animators can be created from either code or resource files. Here is an example
47d430753cba09acb07af8b313286f247c78a41a32Chet Haase * of a ValueAnimator resource file:</p>
48d430753cba09acb07af8b313286f247c78a41a32Chet Haase *
49d430753cba09acb07af8b313286f247c78a41a32Chet Haase * {@sample development/samples/ApiDemos/res/anim/animator.xml ValueAnimatorResources}
50d430753cba09acb07af8b313286f247c78a41a32Chet Haase *
51d430753cba09acb07af8b313286f247c78a41a32Chet Haase * <p>It is also possible to use a combination of {@link PropertyValuesHolder} and
52d430753cba09acb07af8b313286f247c78a41a32Chet Haase * {@link Keyframe} resource tags to create a multi-step animation.
53d430753cba09acb07af8b313286f247c78a41a32Chet Haase * Note that you can specify explicit fractional values (from 0 to 1) for
54d430753cba09acb07af8b313286f247c78a41a32Chet Haase * each keyframe to determine when, in the overall duration, the animation should arrive at that
55d430753cba09acb07af8b313286f247c78a41a32Chet Haase * value. Alternatively, you can leave the fractions off and the keyframes will be equally
56d430753cba09acb07af8b313286f247c78a41a32Chet Haase * distributed within the total duration:</p>
57d430753cba09acb07af8b313286f247c78a41a32Chet Haase *
58d430753cba09acb07af8b313286f247c78a41a32Chet Haase * {@sample development/samples/ApiDemos/res/anim/value_animator_pvh_kf.xml
59d430753cba09acb07af8b313286f247c78a41a32Chet Haase * ValueAnimatorKeyframeResources}
60d430753cba09acb07af8b313286f247c78a41a32Chet Haase *
613aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <div class="special reference">
623aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <h3>Developer Guides</h3>
633aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <p>For more information about animating with {@code ValueAnimator}, read the
643aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <a href="{@docRoot}guide/topics/graphics/prop-animation.html#value-animator">Property
653aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * Animation</a> developer guide.</p>
663aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * </div>
67a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */
6818772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy@SuppressWarnings("unchecked")
693618d30f8ab6018025b11869676b309c3b4961cfDoris Liupublic class ValueAnimator extends Animator implements AnimationHandler.AnimationFrameCallback {
70c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    private static final String TAG = "ValueAnimator";
71c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    private static final boolean DEBUG = false;
72a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
73a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
74a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Internal constants
75a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
76d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase    private static float sDurationScale = 1.0f;
77a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
78a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
79a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Internal variables
80a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * NOTE: This object implements the clone() method, making a deep copy of any referenced
81a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * objects. As other non-trivial fields are added to this class, make sure to add logic
82a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * to clone() to make deep copies of them.
83a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
84a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
85c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    /**
86c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * The first time that the animation's animateFrame() method is called. This time is used to
87c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * determine elapsed time (and therefore the elapsed fraction) in subsequent calls
88c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * to animateFrame().
89c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     *
90c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * Whenever mStartTime is set, you must also update mStartTimeCommitted.
91c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     */
92051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    long mStartTime;
93a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
94a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
95c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * When true, the start time has been firmly committed as a chosen reference point in
96c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * time by which the progress of the animation will be evaluated.  When false, the
97c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * start time may be updated when the first animation frame is committed so as
98c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * to compensate for jank that may have occurred between when the start time was
99c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * initialized and when the frame was actually drawn.
100c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     *
101c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * This flag is generally set to false during the first frame of the animation
102c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * when the animation playing state transitions from STOPPED to RUNNING or
103c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * resumes after having been paused.  This flag is set to true when the start time
104c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * is firmly committed and should not be further compensated for jank.
105c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     */
106c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    boolean mStartTimeCommitted;
107c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown
108c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    /**
109a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Set when setCurrentPlayTime() is called. If negative, animation is not currently seeked
110a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * to a value.
111a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1120d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase    float mSeekFraction = -1;
113a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1148aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    /**
1158aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * Set on the next frame after pause() is called, used to calculate a new startTime
1168aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * or delayStartTime which allows the animator to continue from the point at which
1178aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * it was paused. If negative, has not yet been set.
1188aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     */
1198aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    private long mPauseTime;
1208aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
1218aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    /**
1228aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * Set when an animator is resumed. This triggers logic in the next frame which
1238aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * actually resumes the animator.
1248aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     */
1258aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    private boolean mResumed = false;
1268aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
127a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The time interpolator to be used if none is set on the animation
128e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    private static final TimeInterpolator sDefaultInterpolator =
129e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase            new AccelerateDecelerateInterpolator();
130a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
131a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
132f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * Flag to indicate whether this animator is playing in reverse mode, specifically
133f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * by being started or interrupted by a call to reverse(). This flag is different than
134f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * mPlayingBackwards, which indicates merely whether the current iteration of the
135f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * animator is playing in reverse. It is used in corner cases to determine proper end
136f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * behavior.
137f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     */
138f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase    private boolean mReversing;
139f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase
140f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase    /**
141fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu     * Tracks the overall fraction of the animation, ranging from 0 to mRepeatCount + 1
142a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
143fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu    private float mOverallFraction = 0f;
144a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
145a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
146a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     * Tracks current elapsed/eased fraction, for querying in getAnimatedFraction().
147fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu     * This is calculated by interpolating the fraction (range: [0, 1]) in the current iteration.
148a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     */
149a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    private float mCurrentFraction = 0f;
150a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase
151a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    /**
1523618d30f8ab6018025b11869676b309c3b4961cfDoris Liu     * Tracks the time (in milliseconds) when the last frame arrived.
153a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1543618d30f8ab6018025b11869676b309c3b4961cfDoris Liu    private long mLastFrameTime = 0;
155a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
156a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
157b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * Additional playing state to indicate whether an animator has been start()'d. There is
158b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * some lag between a call to start() and the first animation frame. We should still note
159b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * that the animation has been started, even if it's first animation frame has not yet
160b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * happened, and reflect that state in isRunning().
161b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * Note that delayed animations are different: they are not started until their first
162b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * animation frame, which occurs after their delay elapses.
163b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     */
1648b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    private boolean mRunning = false;
1658b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase
1668b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    /**
1678b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     * Additional playing state to indicate whether an animator has been start()'d, whether or
1688b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     * not there is a nonzero startDelay.
1698b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     */
170b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase    private boolean mStarted = false;
171b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase
172b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase    /**
1738aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * Tracks whether we've notified listeners of the onAnimationStart() event. This can be
17417cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase     * complex to keep track of since we notify listeners at different times depending on
17517cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase     * startDelay and whether start() was called before end().
17617cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase     */
17717cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase    private boolean mStartListenersCalled = false;
17817cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase
17917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase    /**
180a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Flag that denotes whether the animation is set up and ready to go. Used to
181a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * set up animation that has not yet been started.
182a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
183a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    boolean mInitialized = false;
184a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1853dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu    /**
1863dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu     * Flag that tracks whether animation has been requested to end.
1873dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu     */
1883dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu    private boolean mAnimationEndRequested = false;
1893dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu
190a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    //
191a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // Backing variables
192a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    //
193a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
194a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // How long the animation should last in ms
195fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu    private long mDuration = 300;
196a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
197fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu    // The amount of time in ms to delay starting the animation after start() is called. Note
198fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu    // that this start delay is unscaled. When there is a duration scale set on the animator, the
199fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu    // scaling factor will be applied to this delay.
200fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu    private long mStartDelay = 0;
201a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
202a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The number of times the animation will repeat. The default is 0, which means the animation
203a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // will play only once
204a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private int mRepeatCount = 0;
205a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
206a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
207a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The type of repetition that will occur when repeatMode is nonzero. RESTART means the
208a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation will start from the beginning on every new cycle. REVERSE means the animation
209a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will reverse directions on each iteration.
210a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
211a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private int mRepeatMode = RESTART;
212a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
213a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
214a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The time interpolator to be used. The elapsed fraction of the animation will be passed
215a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * through this interpolator to calculate the interpolated fraction, which is then used to
216a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * calculate the animated values.
217a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
218e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    private TimeInterpolator mInterpolator = sDefaultInterpolator;
219a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
220a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
221a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The set of listeners to be sent events through the life of an animation.
222a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
223d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck    ArrayList<AnimatorUpdateListener> mUpdateListeners = null;
224a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
225a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
226a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The property/value sets being animated.
227a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
228a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    PropertyValuesHolder[] mValues;
229a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
230a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
231a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * A hashmap of the PropertyValuesHolder objects. This map is used to lookup animated values
232a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * by property name during calls to getAnimatedValue(String).
233a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
234a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    HashMap<String, PropertyValuesHolder> mValuesMap;
235a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
236a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
237a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Public constants
238a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
239a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
2407764b920f21e0b9250122ff26533d5dac98df6b3George Mount    /** @hide */
2417764b920f21e0b9250122ff26533d5dac98df6b3George Mount    @IntDef({RESTART, REVERSE})
2427764b920f21e0b9250122ff26533d5dac98df6b3George Mount    @Retention(RetentionPolicy.SOURCE)
2437764b920f21e0b9250122ff26533d5dac98df6b3George Mount    public @interface RepeatMode {}
2447764b920f21e0b9250122ff26533d5dac98df6b3George Mount
245a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
246a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * When the animation reaches the end and <code>repeatCount</code> is INFINITE
247a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * or a positive value, the animation restarts from the beginning.
248a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
249a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static final int RESTART = 1;
250a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
251a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * When the animation reaches the end and <code>repeatCount</code> is INFINITE
252a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * or a positive value, the animation reverses direction on every iteration.
253a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
254a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static final int REVERSE = 2;
255a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
256a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This value used used with the {@link #setRepeatCount(int)} property to repeat
257a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the animation indefinitely.
258a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
259a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static final int INFINITE = -1;
260a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
261c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase    /**
262c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase     * @hide
263c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase     */
264c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase    public static void setDurationScale(float durationScale) {
265c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase        sDurationScale = durationScale;
266c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase    }
267c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase
268a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
269ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown     * @hide
270ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown     */
271ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown    public static float getDurationScale() {
272ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown        return sDurationScale;
273ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown    }
274ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown
275ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown    /**
276a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Creates a new ValueAnimator object. This default constructor is primarily for
2772794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * use internally; the factory methods which take parameters are more generally
278a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * useful.
279a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
280a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public ValueAnimator() {
281a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
282a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
283a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
2842794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between int values. A single
2852794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
2862794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
2872794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
2882794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
2892794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
290a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
2912794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
2922794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
29341f041d9986f8a5d45b6cb0b86e881c81a412168Chet Haase     */
2942794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofInt(int... values) {
2952794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
2962794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setIntValues(values);
2972794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
2982794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
2992794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
3002794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
3011ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * Constructs and returns a ValueAnimator that animates between color values. A single
3021ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * value implies that that value is the one being animated to. However, this is not typically
3031ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * useful in a ValueAnimator object because there is no way for the object to determine the
3041ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * starting value for the animation (unlike ObjectAnimator, which can derive that value
3051ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * from the target object and property being animated). Therefore, there should typically
3061ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * be two or more values.
3071ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     *
3081ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * @param values A set of values that the animation will animate between over time.
3091ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * @return A ValueAnimator object that is set up to animate between the given values.
3101ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     */
3111ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount    public static ValueAnimator ofArgb(int... values) {
3121ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount        ValueAnimator anim = new ValueAnimator();
3131ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount        anim.setIntValues(values);
3141ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount        anim.setEvaluator(ArgbEvaluator.getInstance());
3151ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount        return anim;
3161ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount    }
3171ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount
3181ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount    /**
3192794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between float values. A single
3202794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
3212794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
3222794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
3232794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
3242794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
3252794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3262794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
3272794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
3282794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
3292794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofFloat(float... values) {
3302794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
3312794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setFloatValues(values);
3322794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
3332794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
3342794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
3352794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
3362794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between the values
3372794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * specified in the PropertyValuesHolder objects.
3382794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3392794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of PropertyValuesHolder objects whose values will be animated
3402794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * between over time.
3412794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
3422794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
3432794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofPropertyValuesHolder(PropertyValuesHolder... values) {
3442794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
3452794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setValues(values);
3462794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
3472794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
3482794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
3492794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between Object values. A single
3502794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
3512794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
3522794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
3532794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
3542794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
3552794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3562794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>Since ValueAnimator does not know how to animate between arbitrary Objects, this
3572794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * factory method also takes a TypeEvaluator object that the ValueAnimator will use
3582794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * to perform that interpolation.
3592794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3602794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param evaluator A TypeEvaluator that will be called on each animation frame to
3612794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * provide the ncessry interpolation between the Object values to derive the animated
3622794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value.
3632794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
3642794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
3652794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
3662794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofObject(TypeEvaluator evaluator, Object... values) {
3672794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
3682794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setObjectValues(values);
3692794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setEvaluator(evaluator);
3702794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
3712794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
3722794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
3732794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
3742794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Sets int values that will be animated between. A single
3752794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
3762794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
3772794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
3782794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
3792794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
3802794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3812794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>If there are already multiple sets of values defined for this ValueAnimator via more
3822794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * than one PropertyValuesHolder object, this method will set the values for the first
3832794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * of those objects.</p>
3842794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3852794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
3862794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
3872794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public void setIntValues(int... values) {
3882794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (values == null || values.length == 0) {
3892794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            return;
3902794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
3912794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (mValues == null || mValues.length == 0) {
39218772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy            setValues(PropertyValuesHolder.ofInt("", values));
3932794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        } else {
3942794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            PropertyValuesHolder valuesHolder = mValues[0];
3952794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            valuesHolder.setIntValues(values);
3962794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
3972794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        // New property/values/target should cause re-initialization prior to starting
3982794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        mInitialized = false;
3992794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
4002794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
4012794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
4022794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Sets float values that will be animated between. A single
4032794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
4042794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
4052794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
4062794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
4072794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
4082794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4092794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>If there are already multiple sets of values defined for this ValueAnimator via more
4102794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * than one PropertyValuesHolder object, this method will set the values for the first
4112794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * of those objects.</p>
4122794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4132794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
4142794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
4152794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public void setFloatValues(float... values) {
4162794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (values == null || values.length == 0) {
4172794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            return;
41841f041d9986f8a5d45b6cb0b86e881c81a412168Chet Haase        }
4192794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (mValues == null || mValues.length == 0) {
42018772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy            setValues(PropertyValuesHolder.ofFloat("", values));
4212794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        } else {
4222794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            PropertyValuesHolder valuesHolder = mValues[0];
4232794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            valuesHolder.setFloatValues(values);
4242794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
4252794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        // New property/values/target should cause re-initialization prior to starting
4262794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        mInitialized = false;
4272794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
4282794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
4292794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
4302794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Sets the values to animate between for this animation. A single
4312794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
4322794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
4332794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
4342794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
4352794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
4362794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4372794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>If there are already multiple sets of values defined for this ValueAnimator via more
4382794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * than one PropertyValuesHolder object, this method will set the values for the first
4392794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * of those objects.</p>
4402794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4412794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>There should be a TypeEvaluator set on the ValueAnimator that knows how to interpolate
4422794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * between these value objects. ValueAnimator only knows how to interpolate between the
4432794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * primitive types specified in the other setValues() methods.</p>
4442794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4452794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values The set of values to animate between.
4462794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
4472794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public void setObjectValues(Object... values) {
4482794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (values == null || values.length == 0) {
4492794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            return;
4502794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
4512794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (mValues == null || mValues.length == 0) {
45218772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy            setValues(PropertyValuesHolder.ofObject("", null, values));
4532794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        } else {
4542794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            PropertyValuesHolder valuesHolder = mValues[0];
4552794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            valuesHolder.setObjectValues(values);
4562794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
4572794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        // New property/values/target should cause re-initialization prior to starting
4582794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        mInitialized = false;
459a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
460a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
461a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
462a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets the values, per property, being animated between. This function is called internally
463f76a50ce8fdc6aea22cabc77b2977a1a15a79630Ken Wakasa     * by the constructors of ValueAnimator that take a list of values. But a ValueAnimator can
464a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * be constructed without values and this method can be called to set the values manually
465a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * instead.
466a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
467a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param values The set of values, per property, being animated between.
468a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
469a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setValues(PropertyValuesHolder... values) {
470a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        int numValues = values.length;
471a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mValues = values;
472a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues);
473a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        for (int i = 0; i < numValues; ++i) {
47418772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy            PropertyValuesHolder valuesHolder = values[i];
475a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mValuesMap.put(valuesHolder.getPropertyName(), valuesHolder);
476a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
4770e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase        // New property/values/target should cause re-initialization prior to starting
4780e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase        mInitialized = false;
479a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
480a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
481a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
482a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Returns the values that this ValueAnimator animates between. These values are stored in
483a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * PropertyValuesHolder objects, even if the ValueAnimator was created with a simple list
484a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of value objects instead.
485a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
486a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return PropertyValuesHolder[] An array of PropertyValuesHolder objects which hold the
487a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * values, per property, that define the animation.
488a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
489a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public PropertyValuesHolder[] getValues() {
490a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mValues;
491a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
492a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
493a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
494a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This function is called immediately before processing the first animation
495a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frame of an animation. If there is a nonzero <code>startDelay</code>, the
496a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function is called after that delay ends.
497a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * It takes care of the final initialization steps for the
498a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation.
499a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
500a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *  <p>Overrides of this method should call the superclass method to ensure
501a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *  that internal mechanisms for the animation are set up correctly.</p>
502a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
503c615c6fc9caca76cd96998f86e1f1e6393aeadbbTor Norbye    @CallSuper
504a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    void initAnimation() {
505a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (!mInitialized) {
506a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            int numValues = mValues.length;
507a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 0; i < numValues; ++i) {
508a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mValues[i].init();
509a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
510a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mInitialized = true;
511a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
512a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
513a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
514a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
5152794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Sets the length of the animation. The default duration is 300 milliseconds.
516a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
51727c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase     * @param duration The length of the animation, in milliseconds. This value cannot
51827c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase     * be negative.
5192794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return ValueAnimator The object called with setDuration(). This return
5202794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value makes it easier to compose statements together that construct and then set the
5212794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * duration, as in <code>ValueAnimator.ofInt(0, 10).setDuration(500).start()</code>.
522a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
523c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    @Override
5242794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public ValueAnimator setDuration(long duration) {
52527c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase        if (duration < 0) {
52627c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase            throw new IllegalArgumentException("Animators cannot have negative duration: " +
52727c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase                    duration);
52827c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase        }
529fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        mDuration = duration;
5302794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return this;
531a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
532a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
533fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu    private long getScaledDuration() {
534fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        return (long)(mDuration * sDurationScale);
5357a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown    }
5367a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown
537a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
5382794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Gets the length of the animation. The default duration is 300 milliseconds.
539a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
540a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return The length of the animation, in milliseconds.
541a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
542c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    @Override
543a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public long getDuration() {
544fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        return mDuration;
545a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
546a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
5471309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu    @Override
5481309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu    public long getTotalDuration() {
5491309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu        if (mRepeatCount == INFINITE) {
5501309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu            return DURATION_INFINITE;
5511309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu        } else {
552fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            return mStartDelay + (mDuration * (mRepeatCount + 1));
5531309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu        }
5541309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu    }
5551309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu
5561309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu    /**
557a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets the position of the animation to the specified point in time. This time should
558a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * be between 0 and the total duration of the animation, including any repetition. If
559a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the animation has not yet been started, then it will not advance forward after it is
560a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * set to this time; it will simply set the time to this value and perform any appropriate
561a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * actions based on that time. If the animation is already running, then setCurrentPlayTime()
562a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will set the current playing time to this value and continue playing from that point.
563a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
564a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param playTime The time, in milliseconds, to which the animation is advanced or rewound.
565a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
566a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setCurrentPlayTime(long playTime) {
567fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        float fraction = mDuration > 0 ? (float) playTime / mDuration : 1;
5680d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase        setCurrentFraction(fraction);
5690d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase    }
5700d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase
5710d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase    /**
5720d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * Sets the position of the animation to the specified fraction. This fraction should
5730d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * be between 0 and the total fraction of the animation, including any repetition. That is,
5740d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * a fraction of 0 will position the animation at the beginning, a value of 1 at the end,
575f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * and a value of 2 at the end of a reversing animator that repeats once. If
5760d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * the animation has not yet been started, then it will not advance forward after it is
5770d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * set to this fraction; it will simply set the fraction to this value and perform any
5780d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * appropriate actions based on that fraction. If the animation is already running, then
5790d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * setCurrentFraction() will set the current fraction to this value and continue
5805a5afe010d2f8fb7e8f00858b8a305b4745c0469Eino-Ville Talvala     * playing from that point. {@link Animator.AnimatorListener} events are not called
581f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * due to changing the fraction; those events are only processed while the animation
582f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * is running.
5830d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     *
584f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * @param fraction The fraction to which the animation is advanced or rewound. Values
585f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * outside the range of 0 to the maximum fraction for the animator will be clamped to
586f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * the correct range.
5870d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     */
5880d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase    public void setCurrentFraction(float fraction) {
589a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        initAnimation();
590fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        fraction = clampFraction(fraction);
591fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        long seekTime = (long) (getScaledDuration() * fraction);
592f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        long currentTime = AnimationUtils.currentAnimationTimeMillis();
593f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        mStartTime = currentTime - seekTime;
594c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown        mStartTimeCommitted = true; // do not allow start time to be compensated for jank
5953618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        if (!mRunning) {
5960d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase            mSeekFraction = fraction;
597a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
598fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        mOverallFraction = fraction;
599fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        final float currentIterationFraction = getCurrentIterationFraction(fraction);
600fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        animateValue(currentIterationFraction);
601fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu    }
602fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu
603fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu    /**
604fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu     * Calculates current iteration based on the overall fraction. The overall fraction will be
605fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu     * in the range of [0, mRepeatCount + 1]. Both current iteration and fraction in the current
606fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu     * iteration can be derived from it.
607fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu     */
608fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu    private int getCurrentIteration(float fraction) {
609fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        fraction = clampFraction(fraction);
610fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        // If the overall fraction is a positive integer, we consider the current iteration to be
611fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        // complete. In other words, the fraction for the current iteration would be 1, and the
612fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        // current iteration would be overall fraction - 1.
613fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        double iteration = Math.floor(fraction);
614fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        if (fraction == iteration && fraction > 0) {
615fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            iteration--;
616fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        }
617fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        return (int) iteration;
618fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu    }
619fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu
620fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu    /**
621fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu     * Calculates the fraction of the current iteration, taking into account whether the animation
622fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu     * should be played backwards. E.g. When the animation is played backwards in an iteration,
623fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu     * the fraction for that iteration will go from 1f to 0f.
624fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu     */
625fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu    private float getCurrentIterationFraction(float fraction) {
626fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        fraction = clampFraction(fraction);
627fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        int iteration = getCurrentIteration(fraction);
628fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        float currentFraction = fraction - iteration;
629fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        return shouldPlayBackward(iteration) ? 1f - currentFraction : currentFraction;
630fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu    }
631fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu
632fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu    /**
633fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu     * Clamps fraction into the correct range: [0, mRepeatCount + 1]. If repeat count is infinite,
634fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu     * no upper bound will be set for the fraction.
635fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu     *
636fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu     * @param fraction fraction to be clamped
637fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu     * @return fraction clamped into the range of [0, mRepeatCount + 1]
638fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu     */
639fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu    private float clampFraction(float fraction) {
640fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        if (fraction < 0) {
641fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            fraction = 0;
642fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        } else if (mRepeatCount != INFINITE) {
643fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            fraction = Math.min(fraction, mRepeatCount + 1);
644fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        }
645fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        return fraction;
646fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu    }
647fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu
648fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu    /**
649fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu     * Calculates the direction of animation playing (i.e. forward or backward), based on 1)
650fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu     * whether the entire animation is being reversed, 2) repeat mode applied to the current
651fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu     * iteration.
652fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu     */
653fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu    private boolean shouldPlayBackward(int iteration) {
654fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        if (iteration > 0 && mRepeatMode == REVERSE &&
655fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu                (iteration < (mRepeatCount + 1) || mRepeatCount == INFINITE)) {
656fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            // if we were seeked to some other iteration in a reversing animator,
657fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            // figure out the correct direction to start playing based on the iteration
658fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            if (mReversing) {
659fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu                return (iteration % 2) == 0;
660fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            } else {
661fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu                return (iteration % 2) != 0;
662fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            }
663fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        } else {
664fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            return mReversing;
665f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        }
666a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
667a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
668a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
669a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Gets the current position of the animation in time, which is equal to the current
670a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * time minus the time that the animation started. An animation that is not yet started will
6714365e5a13755ffbdb977586c90b8b515add599aaChet Haase     * return a value of zero, unless the animation has has its play time set via
6724365e5a13755ffbdb977586c90b8b515add599aaChet Haase     * {@link #setCurrentPlayTime(long)} or {@link #setCurrentFraction(float)}, in which case
6734365e5a13755ffbdb977586c90b8b515add599aaChet Haase     * it will return the time that was set.
674a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
675a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return The current position in time of the animation.
676a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
677a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public long getCurrentPlayTime() {
6784365e5a13755ffbdb977586c90b8b515add599aaChet Haase        if (!mInitialized || (!mStarted && mSeekFraction < 0)) {
679a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return 0;
680a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
6814365e5a13755ffbdb977586c90b8b515add599aaChet Haase        if (mSeekFraction >= 0) {
682fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            return (long) (mDuration * mSeekFraction);
6834365e5a13755ffbdb977586c90b8b515add599aaChet Haase        }
684fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        float durationScale = sDurationScale == 0 ? 1 : sDurationScale;
685fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        return (long) ((AnimationUtils.currentAnimationTimeMillis() - mStartTime) / durationScale);
686a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
687a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
688a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
689a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, to delay starting the animation after
690a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link #start()} is called.
691a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
692a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return the number of milliseconds to delay running the animation
693a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
694c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    @Override
695a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public long getStartDelay() {
696fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        return mStartDelay;
697a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
698a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
699a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
700a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, to delay starting the animation after
701a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link #start()} is called.
702a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
703a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param startDelay The amount of the delay, in milliseconds
704a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
705c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    @Override
706a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setStartDelay(long startDelay) {
707fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        mStartDelay = startDelay;
708a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
709a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
710a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
711a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, between each frame of the animation. This is a
712a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * requested time that the animation will attempt to honor, but the actual delay between
713a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frames may be different, depending on system load and capabilities. This is a static
714a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function because the same delay will be applied to all animations, since they are all
715a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * run off of a single timing loop.
716a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
71796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     * The frame delay may be ignored when the animation system uses an external timing
71896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     * source, such as the display refresh rate (vsync), to govern animations.
71996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     *
7202b2e2c8d252b33fa25ccba1e37322256cd44b3d5Doris Liu     * Note that this method should be called from the same thread that {@link #start()} is
7212b2e2c8d252b33fa25ccba1e37322256cd44b3d5Doris Liu     * called in order to check the frame delay for that animation. A runtime exception will be
7222b2e2c8d252b33fa25ccba1e37322256cd44b3d5Doris Liu     * thrown if the calling thread does not have a Looper.
7232b2e2c8d252b33fa25ccba1e37322256cd44b3d5Doris Liu     *
724a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return the requested time between frames, in milliseconds
725a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
726a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static long getFrameDelay() {
7273618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        return AnimationHandler.getInstance().getFrameDelay();
728a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
729a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
730a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
731a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, between each frame of the animation. This is a
732a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * requested time that the animation will attempt to honor, but the actual delay between
733a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frames may be different, depending on system load and capabilities. This is a static
734a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function because the same delay will be applied to all animations, since they are all
735a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * run off of a single timing loop.
736a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
73796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     * The frame delay may be ignored when the animation system uses an external timing
73896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     * source, such as the display refresh rate (vsync), to govern animations.
73996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     *
7402b2e2c8d252b33fa25ccba1e37322256cd44b3d5Doris Liu     * Note that this method should be called from the same thread that {@link #start()} is
7412b2e2c8d252b33fa25ccba1e37322256cd44b3d5Doris Liu     * called in order to have the new frame delay take effect on that animation. A runtime
7422b2e2c8d252b33fa25ccba1e37322256cd44b3d5Doris Liu     * exception will be thrown if the calling thread does not have a Looper.
7432b2e2c8d252b33fa25ccba1e37322256cd44b3d5Doris Liu     *
744a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param frameDelay the requested time between frames, in milliseconds
745a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
746a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static void setFrameDelay(long frameDelay) {
7473618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        AnimationHandler.getInstance().setFrameDelay(frameDelay);
748a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
749a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
750a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
751a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The most recent value calculated by this <code>ValueAnimator</code> when there is just one
752a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * property being animated. This value is only sensible while the animation is running. The main
753a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * purpose for this read-only property is to retrieve the value from the <code>ValueAnimator</code>
754a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * during a call to {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which
755a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is called during each animation frame, immediately after the value is calculated.
756a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
757a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return animatedValue The value most recently calculated by this <code>ValueAnimator</code> for
758a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the single property being animated. If there are several properties being animated
759a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * (specified by several PropertyValuesHolder objects in the constructor), this function
760a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * returns the animated value for the first of those objects.
761a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
762a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public Object getAnimatedValue() {
763a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mValues != null && mValues.length > 0) {
764a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return mValues[0].getAnimatedValue();
765a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
766a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        // Shouldn't get here; should always have values unless ValueAnimator was set up wrong
767a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return null;
768a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
769a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
770a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
771a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The most recent value calculated by this <code>ValueAnimator</code> for <code>propertyName</code>.
772a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The main purpose for this read-only property is to retrieve the value from the
773a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <code>ValueAnimator</code> during a call to
774a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which
775a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is called during each animation frame, immediately after the value is calculated.
776a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
777a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return animatedValue The value most recently calculated for the named property
778a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * by this <code>ValueAnimator</code>.
779a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
780a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public Object getAnimatedValue(String propertyName) {
781a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        PropertyValuesHolder valuesHolder = mValuesMap.get(propertyName);
782a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (valuesHolder != null) {
783a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return valuesHolder.getAnimatedValue();
784a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        } else {
785a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // At least avoid crashing if called with bogus propertyName
786a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return null;
787a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
788a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
789a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
790a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
791a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets how many times the animation should be repeated. If the repeat
792a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * count is 0, the animation is never repeated. If the repeat count is
793a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * greater than 0 or {@link #INFINITE}, the repeat mode will be taken
794a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * into account. The repeat count is 0 by default.
795a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
796a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param value the number of times the animation should be repeated
797a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
798a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setRepeatCount(int value) {
799a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mRepeatCount = value;
800a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
801a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
802a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Defines how many times the animation should repeat. The default value
803a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is 0.
804a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
805a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return the number of times the animation should repeat, or {@link #INFINITE}
806a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
807a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public int getRepeatCount() {
808a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mRepeatCount;
809a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
810a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
811a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
812a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Defines what this animation should do when it reaches the end. This
813a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * setting is applied only when the repeat count is either greater than
814a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * 0 or {@link #INFINITE}. Defaults to {@link #RESTART}.
815a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
816a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param value {@link #RESTART} or {@link #REVERSE}
817a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
8187764b920f21e0b9250122ff26533d5dac98df6b3George Mount    public void setRepeatMode(@RepeatMode int value) {
819a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mRepeatMode = value;
820a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
821a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
822a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
823a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Defines what this animation should do when it reaches the end.
824a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
825a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return either one of {@link #REVERSE} or {@link #RESTART}
826a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
8277764b920f21e0b9250122ff26533d5dac98df6b3George Mount    @RepeatMode
828a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public int getRepeatMode() {
829a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mRepeatMode;
830a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
831a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
832a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
833a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Adds a listener to the set of listeners that are sent update events through the life of
834a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * an animation. This method is called on all listeners for every frame of the animation,
835a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * after the values for the animation have been calculated.
836a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
837a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param listener the listener to be added to the current set of listeners for this animation.
838a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
839a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void addUpdateListener(AnimatorUpdateListener listener) {
840a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners == null) {
841a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mUpdateListeners = new ArrayList<AnimatorUpdateListener>();
842a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
843a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mUpdateListeners.add(listener);
844a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
845a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
846a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
8473060421045d4d9e411797f91bb509824b03e33fbJim Miller     * Removes all listeners from the set listening to frame updates for this animation.
8483060421045d4d9e411797f91bb509824b03e33fbJim Miller     */
8493060421045d4d9e411797f91bb509824b03e33fbJim Miller    public void removeAllUpdateListeners() {
8503060421045d4d9e411797f91bb509824b03e33fbJim Miller        if (mUpdateListeners == null) {
8513060421045d4d9e411797f91bb509824b03e33fbJim Miller            return;
8523060421045d4d9e411797f91bb509824b03e33fbJim Miller        }
8533060421045d4d9e411797f91bb509824b03e33fbJim Miller        mUpdateListeners.clear();
8543060421045d4d9e411797f91bb509824b03e33fbJim Miller        mUpdateListeners = null;
8553060421045d4d9e411797f91bb509824b03e33fbJim Miller    }
8563060421045d4d9e411797f91bb509824b03e33fbJim Miller
8573060421045d4d9e411797f91bb509824b03e33fbJim Miller    /**
858a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Removes a listener from the set listening to frame updates for this animation.
859a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
860a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param listener the listener to be removed from the current set of update listeners
861a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * for this animation.
862a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
863a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void removeUpdateListener(AnimatorUpdateListener listener) {
864a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners == null) {
865a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return;
866a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
867a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mUpdateListeners.remove(listener);
868a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners.size() == 0) {
869a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mUpdateListeners = null;
870a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
871a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
872a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
873a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
874a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
875a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The time interpolator used in calculating the elapsed fraction of this animation. The
876a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * interpolator determines whether the animation runs with linear or non-linear motion,
877a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * such as acceleration and deceleration. The default value is
878a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link android.view.animation.AccelerateDecelerateInterpolator}
879a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
88027c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase     * @param value the interpolator to be used by this animation. A value of <code>null</code>
88127c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase     * will result in linear interpolation.
882a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
883a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
884e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    public void setInterpolator(TimeInterpolator value) {
885a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (value != null) {
886a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mInterpolator = value;
88727c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase        } else {
88827c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase            mInterpolator = new LinearInterpolator();
889a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
890a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
891a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
892a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
893a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Returns the timing interpolator that this ValueAnimator uses.
894a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
895a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return The timing interpolator for this ValueAnimator.
896a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
897430742f09063574271e6c4091de13b9b9e762514Chet Haase    @Override
898e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    public TimeInterpolator getInterpolator() {
899a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mInterpolator;
900a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
901a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
902a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
903a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The type evaluator to be used when calculating the animated values of this animation.
904b2ab04ffb6894f399d5c9ceb15f64eb17b654426Chet Haase     * The system will automatically assign a float or int evaluator based on the type
905a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of <code>startValue</code> and <code>endValue</code> in the constructor. But if these values
906a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * are not one of these primitive types, or if different evaluation is desired (such as is
907a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * necessary with int values that represent colors), a custom evaluator needs to be assigned.
90853ee3316bcb3590ff156b3fd7108903c0817c35dChet Haase     * For example, when running an animation on color values, the {@link ArgbEvaluator}
909a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * should be used to get correct RGB color interpolation.
910a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
911a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>If this ValueAnimator has only one set of values being animated between, this evaluator
912a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will be used for that set. If there are several sets of values being animated, which is
913fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase     * the case if PropertyValuesHolder objects were set on the ValueAnimator, then the evaluator
914a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is assigned just to the first PropertyValuesHolder object.</p>
915a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
916a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param value the evaluator to be used this animation
917a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
918a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setEvaluator(TypeEvaluator value) {
919a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (value != null && mValues != null && mValues.length > 0) {
920a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mValues[0].setEvaluator(value);
921a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
922a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
923a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
92417cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase    private void notifyStartListeners() {
92517cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        if (mListeners != null && !mStartListenersCalled) {
92617cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            ArrayList<AnimatorListener> tmpListeners =
92717cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                    (ArrayList<AnimatorListener>) mListeners.clone();
92817cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            int numListeners = tmpListeners.size();
92917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            for (int i = 0; i < numListeners; ++i) {
93017cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                tmpListeners.get(i).onAnimationStart(this);
93117cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            }
93217cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        }
93317cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        mStartListenersCalled = true;
93417cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase    }
93517cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase
936a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
937a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Start the animation playing. This version of start() takes a boolean flag that indicates
938a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * whether the animation should play in reverse. The flag is usually false, but may be set
9392970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * to true if called from the reverse() method.
9402970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     *
9412970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * <p>The animation started by calling this method will be run on the thread that called
9422970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * this method. This thread should have a Looper on it (a runtime exception will be thrown if
9432970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * this is not the case). Also, if the animation will animate
9442970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * properties of objects in the view hierarchy, then the calling thread should be the UI
9452970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * thread for that view hierarchy.</p>
946a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
947a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param playBackwards Whether the ValueAnimator should start playing in reverse.
948a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
949a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private void start(boolean playBackwards) {
9502970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        if (Looper.myLooper() == null) {
9512970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            throw new AndroidRuntimeException("Animators may only be run on Looper threads");
9523060421045d4d9e411797f91bb509824b03e33fbJim Miller        }
953f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        mReversing = playBackwards;
954fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        // Special case: reversing from seek-to-0 should act as if not seeked at all.
955fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        if (playBackwards && mSeekFraction != -1 && mSeekFraction != 0) {
956fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            if (mRepeatCount == INFINITE) {
957fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu                // Calculate the fraction of the current iteration.
958fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu                float fraction = (float) (mSeekFraction - Math.floor(mSeekFraction));
959fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu                mSeekFraction = 1 - fraction;
960f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            } else {
961fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu                mSeekFraction = 1 + mRepeatCount - mSeekFraction;
962f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            }
963f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        }
9648b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        mStarted = true;
9658aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        mPaused = false;
9663618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        mRunning = false;
9673618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        AnimationHandler animationHandler = AnimationHandler.getInstance();
968fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        animationHandler.addAnimationFrameCallback(this, (long) (mStartDelay * sDurationScale));
969f57bfe2fefc87fdb1dcc27b0f4b3a11996c15da2Doris Liu
970907c177b04d07d5d84005ac92df9bde6af7c394dGeorge Mount        if (mStartDelay == 0 || mSeekFraction >= 0) {
971f57bfe2fefc87fdb1dcc27b0f4b3a11996c15da2Doris Liu            // If there's no start delay, init the animation and notify start listeners right away
972fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            // to be consistent with the previous behavior. Otherwise, postpone this until the first
973fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            // frame after the start delay.
974f57bfe2fefc87fdb1dcc27b0f4b3a11996c15da2Doris Liu            startAnimation();
975b199da785b0174a2addcf0c4c31304d005dc69e9Doris Liu            if (mSeekFraction == -1) {
976b199da785b0174a2addcf0c4c31304d005dc69e9Doris Liu                // No seek, start at play time 0. Note that the reason we are not using fraction 0
977b199da785b0174a2addcf0c4c31304d005dc69e9Doris Liu                // is because for animations with 0 duration, we want to be consistent with pre-N
978b199da785b0174a2addcf0c4c31304d005dc69e9Doris Liu                // behavior: skip to the final value immediately.
979b199da785b0174a2addcf0c4c31304d005dc69e9Doris Liu                setCurrentPlayTime(0);
980b199da785b0174a2addcf0c4c31304d005dc69e9Doris Liu            } else {
981b199da785b0174a2addcf0c4c31304d005dc69e9Doris Liu                setCurrentFraction(mSeekFraction);
982b199da785b0174a2addcf0c4c31304d005dc69e9Doris Liu            }
983f57bfe2fefc87fdb1dcc27b0f4b3a11996c15da2Doris Liu        }
984a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
985a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
986a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
987a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void start() {
988a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        start(false);
989a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
990a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
991a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
992a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void cancel() {
9933618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        if (Looper.myLooper() == null) {
9943618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            throw new AndroidRuntimeException("Animators may only be run on Looper threads");
9953618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        }
9963dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu
9973dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu        // If end has already been requested, through a previous end() or cancel() call, no-op
9983dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu        // until animation starts again.
9993dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu        if (mAnimationEndRequested) {
10003dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu            return;
10013dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu        }
10023dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu
10032970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        // Only cancel if the animation is actually running or has been started and is about
10042970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        // to run
10053618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        // Only notify listeners if the animator has actually started
10063618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        if ((mStarted || mRunning) && mListeners != null) {
10073618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            if (!mRunning) {
10083618d30f8ab6018025b11869676b309c3b4961cfDoris Liu                // If it's not yet running, then start listeners weren't called. Call them now.
10093618d30f8ab6018025b11869676b309c3b4961cfDoris Liu                notifyStartListeners();
10103618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            }
10113618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            ArrayList<AnimatorListener> tmpListeners =
10123618d30f8ab6018025b11869676b309c3b4961cfDoris Liu                    (ArrayList<AnimatorListener>) mListeners.clone();
10133618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            for (AnimatorListener listener : tmpListeners) {
10143618d30f8ab6018025b11869676b309c3b4961cfDoris Liu                listener.onAnimationCancel(this);
10157dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase            }
10162970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        }
10173618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        endAnimation();
10183618d30f8ab6018025b11869676b309c3b4961cfDoris Liu
1019a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1020a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1021a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
1022a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void end() {
10233618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        if (Looper.myLooper() == null) {
10243618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            throw new AndroidRuntimeException("Animators may only be run on Looper threads");
10253618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        }
10263618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        if (!mRunning) {
1027a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // Special case if the animation has not yet started; get it ready for ending
10283618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            startAnimation();
102917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            mStarted = true;
1030add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase        } else if (!mInitialized) {
1031add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase            initAnimation();
1032e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase        }
1033fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        animateValue(shouldPlayBackward(mRepeatCount) ? 0f : 1f);
10343618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        endAnimation();
1035a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1036a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1037a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
10388aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    public void resume() {
1039a06b3f19d498e2c387e7a976f94d3cd5f1d77c35George Mount        if (Looper.myLooper() == null) {
1040a06b3f19d498e2c387e7a976f94d3cd5f1d77c35George Mount            throw new AndroidRuntimeException("Animators may only be resumed from the same " +
1041a06b3f19d498e2c387e7a976f94d3cd5f1d77c35George Mount                    "thread that the animator was started on");
1042a06b3f19d498e2c387e7a976f94d3cd5f1d77c35George Mount        }
1043a06b3f19d498e2c387e7a976f94d3cd5f1d77c35George Mount        if (mPaused && !mResumed) {
10448aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            mResumed = true;
1045a06b3f19d498e2c387e7a976f94d3cd5f1d77c35George Mount            if (mPauseTime > 0) {
1046a06b3f19d498e2c387e7a976f94d3cd5f1d77c35George Mount                AnimationHandler handler = AnimationHandler.getInstance();
1047a06b3f19d498e2c387e7a976f94d3cd5f1d77c35George Mount                handler.addAnimationFrameCallback(this, 0);
1048a06b3f19d498e2c387e7a976f94d3cd5f1d77c35George Mount            }
10498aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        }
10508aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        super.resume();
10518aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    }
10528aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
10538aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    @Override
10548aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    public void pause() {
10558aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        boolean previouslyPaused = mPaused;
10568aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        super.pause();
10578aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        if (!previouslyPaused && mPaused) {
10588aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            mPauseTime = -1;
10598aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            mResumed = false;
10608aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        }
10618aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    }
10628aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
10638aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    @Override
1064a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public boolean isRunning() {
10653618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        return mRunning;
10668b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    }
10678b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase
10688b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    @Override
10698b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    public boolean isStarted() {
10708b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        return mStarted;
1071a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1072a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1073a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1074a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Plays the ValueAnimator in reverse. If the animation is already running,
1075a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * it will stop itself and play backwards from the point reached when reverse was called.
1076a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * If the animation is not currently running, then it will start from the end and
1077a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * play backwards. This behavior is only set for the current animation; future playing
1078a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of the animation will use the default behavior of playing forward.
1079a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
10807bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui    @Override
1081a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void reverse() {
10823618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        if (mRunning) {
1083a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            long currentTime = AnimationUtils.currentAnimationTimeMillis();
1084a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            long currentPlayTime = currentTime - mStartTime;
1085fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            long timeLeft = getScaledDuration() - currentPlayTime;
1086a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mStartTime = currentTime - timeLeft;
1087c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            mStartTimeCommitted = true; // do not allow start time to be compensated for jank
1088f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            mReversing = !mReversing;
1089f43fb2a57f152b5760d8792fec26f36d46b23817Chet Haase        } else if (mStarted) {
1090f43fb2a57f152b5760d8792fec26f36d46b23817Chet Haase            end();
1091a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        } else {
1092a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            start(true);
1093a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1094a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1095a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1096a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
10977bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui     * @hide
10987bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui     */
10997bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui    @Override
11007bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui    public boolean canReverse() {
11017bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui        return true;
11027bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui    }
11037bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui
11047bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui    /**
1105a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Called internally to end an animation by removing it from the animations list. Must be
1106a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * called on the UI thread.
1107a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
11083dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu    private void endAnimation() {
11093dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu        if (mAnimationEndRequested) {
11103dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu            return;
11113dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu        }
11123618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        AnimationHandler handler = AnimationHandler.getInstance();
11133618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        handler.removeCallback(this);
11143dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu
11153dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu        mAnimationEndRequested = true;
11168aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        mPaused = false;
111717cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        if ((mStarted || mRunning) && mListeners != null) {
111817cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            if (!mRunning) {
111917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                // If it's not yet running, then start listeners weren't called. Call them now.
112017cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                notifyStartListeners();
112117cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase             }
1122a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            ArrayList<AnimatorListener> tmpListeners =
1123a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    (ArrayList<AnimatorListener>) mListeners.clone();
11247c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            int numListeners = tmpListeners.size();
11257c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            for (int i = 0; i < numListeners; ++i) {
11267c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                tmpListeners.get(i).onAnimationEnd(this);
1127a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1128a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
11298b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        mRunning = false;
1130b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase        mStarted = false;
113117cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        mStartListenersCalled = false;
1132f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        mReversing = false;
1133f57bfe2fefc87fdb1dcc27b0f4b3a11996c15da2Doris Liu        mLastFrameTime = 0;
11349b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase        if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
11359b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase            Trace.asyncTraceEnd(Trace.TRACE_TAG_VIEW, getNameForTrace(),
11369b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase                    System.identityHashCode(this));
11379b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase        }
1138a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1139a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1140a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1141a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Called internally to start an animation by adding it to the active animations list. Must be
1142a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * called on the UI thread.
1143a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
11443618d30f8ab6018025b11869676b309c3b4961cfDoris Liu    private void startAnimation() {
11459b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase        if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
11469b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase            Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW, getNameForTrace(),
11479b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase                    System.identityHashCode(this));
11489b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase        }
114966d48566537bd2d037e1eff919542731b93fa85cDoris Liu
115066d48566537bd2d037e1eff919542731b93fa85cDoris Liu        mAnimationEndRequested = false;
1151a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        initAnimation();
11523618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        mRunning = true;
1153fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        if (mSeekFraction >= 0) {
1154fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            mOverallFraction = mSeekFraction;
1155fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        } else {
1156fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            mOverallFraction = 0f;
1157fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        }
11583618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        if (mListeners != null) {
115917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            notifyStartListeners();
1160a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1161a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1162a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1163a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1164fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase     * Returns the name of this animator for debugging purposes.
1165fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase     */
1166fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase    String getNameForTrace() {
1167fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase        return "animator";
1168fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase    }
1169fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase
1170a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1171c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * Applies an adjustment to the animation to compensate for jank between when
1172c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * the animation first ran and when the frame was drawn.
11733618d30f8ab6018025b11869676b309c3b4961cfDoris Liu     * @hide
1174c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     */
11753618d30f8ab6018025b11869676b309c3b4961cfDoris Liu    public void commitAnimationFrame(long frameTime) {
1176c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown        if (!mStartTimeCommitted) {
1177c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            mStartTimeCommitted = true;
11783618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            long adjustment = frameTime - mLastFrameTime;
11793618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            if (adjustment > 0) {
1180c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                mStartTime += adjustment;
1181c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                if (DEBUG) {
1182c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                    Log.d(TAG, "Adjusted start time by " + adjustment + " ms: " + toString());
1183c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                }
1184c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            }
1185c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown        }
1186c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    }
1187c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown
1188c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    /**
1189a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This internal function processes a single animation frame for a given animation. The
1190a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * currentTime parameter is the timing pulse sent by the handler, used to calculate the
1191a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * elapsed duration, and therefore
1192a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the elapsed fraction, of the animation. The return value indicates whether the animation
1193a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * should be ended (which happens when the elapsed time of the animation exceeds the
1194a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation's duration, including the repeatCount).
1195a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1196a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param currentTime The current time, as tracked by the static timing handler
1197a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return true if the animation's duration, including any repetitions due to
11983618d30f8ab6018025b11869676b309c3b4961cfDoris Liu     * <code>repeatCount</code> has been exceeded and the animation should be ended.
1199a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
12003618d30f8ab6018025b11869676b309c3b4961cfDoris Liu    boolean animateBasedOnTime(long currentTime) {
1201a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        boolean done = false;
12023618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        if (mRunning) {
120356b0b570890dbb7baa00da95f5be2eb3e0122f0aDoris Liu            final long scaledDuration = getScaledDuration();
120456b0b570890dbb7baa00da95f5be2eb3e0122f0aDoris Liu            final float fraction = scaledDuration > 0 ?
120556b0b570890dbb7baa00da95f5be2eb3e0122f0aDoris Liu                    (float)(currentTime - mStartTime) / scaledDuration : 1f;
1206fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            final float lastFraction = mOverallFraction;
1207fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            final boolean newIteration = (int) fraction > (int) lastFraction;
1208fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            final boolean lastIterationFinished = (fraction >= mRepeatCount + 1) &&
1209fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu                    (mRepeatCount != INFINITE);
121056b0b570890dbb7baa00da95f5be2eb3e0122f0aDoris Liu            if (scaledDuration == 0) {
121156b0b570890dbb7baa00da95f5be2eb3e0122f0aDoris Liu                // 0 duration animator, ignore the repeat count and skip to the end
121256b0b570890dbb7baa00da95f5be2eb3e0122f0aDoris Liu                done = true;
121356b0b570890dbb7baa00da95f5be2eb3e0122f0aDoris Liu            } else if (newIteration && !lastIterationFinished) {
1214fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu                // Time to repeat
1215fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu                if (mListeners != null) {
1216fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu                    int numListeners = mListeners.size();
1217fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu                    for (int i = 0; i < numListeners; ++i) {
1218fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu                        mListeners.get(i).onAnimationRepeat(this);
1219a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    }
1220a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                }
1221fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            } else if (lastIterationFinished) {
1222fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu                done = true;
1223a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1224fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            mOverallFraction = clampFraction(fraction);
1225fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            float currentIterationFraction = getCurrentIterationFraction(mOverallFraction);
1226fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu            animateValue(currentIterationFraction);
1227a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1228a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return done;
1229a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1230a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1231a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
123220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     * Processes a frame of the animation, adjusting the start time if needed.
123320c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     *
123420c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     * @param frameTime The frame time.
123520c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     * @return true if the animation has ended.
12363618d30f8ab6018025b11869676b309c3b4961cfDoris Liu     * @hide
123720c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     */
12383618d30f8ab6018025b11869676b309c3b4961cfDoris Liu    public final void doAnimationFrame(long frameTime) {
12393618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        AnimationHandler handler = AnimationHandler.getInstance();
1240f57bfe2fefc87fdb1dcc27b0f4b3a11996c15da2Doris Liu        if (mLastFrameTime == 0) {
12413618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            // First frame
12423618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            handler.addOneShotCommitCallback(this);
1243f57bfe2fefc87fdb1dcc27b0f4b3a11996c15da2Doris Liu            if (mStartDelay > 0) {
1244f57bfe2fefc87fdb1dcc27b0f4b3a11996c15da2Doris Liu                startAnimation();
1245f57bfe2fefc87fdb1dcc27b0f4b3a11996c15da2Doris Liu            }
12460d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase            if (mSeekFraction < 0) {
124720c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown                mStartTime = frameTime;
124820c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            } else {
1249fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu                long seekTime = (long) (getScaledDuration() * mSeekFraction);
12500d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase                mStartTime = frameTime - seekTime;
12510d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase                mSeekFraction = -1;
125220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            }
1253c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            mStartTimeCommitted = false; // allow start time to be compensated for jank
125420c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        }
1255f57bfe2fefc87fdb1dcc27b0f4b3a11996c15da2Doris Liu        mLastFrameTime = frameTime;
12568aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        if (mPaused) {
1257a06b3f19d498e2c387e7a976f94d3cd5f1d77c35George Mount            mPauseTime = frameTime;
1258a06b3f19d498e2c387e7a976f94d3cd5f1d77c35George Mount            handler.removeCallback(this);
12593618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            return;
12608aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        } else if (mResumed) {
12618aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            mResumed = false;
12628aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            if (mPauseTime > 0) {
12638aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                // Offset by the duration that the animation was paused
12648aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                mStartTime += (frameTime - mPauseTime);
1265c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                mStartTimeCommitted = false; // allow start time to be compensated for jank
12668aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            }
12673618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            handler.addOneShotCommitCallback(this);
12688aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        }
126920c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        // The frame time might be before the start time during the first frame of
127020c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        // an animation.  The "current time" must always be on or after the start
127120c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        // time to avoid animating frames at negative time intervals.  In practice, this
127220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        // is very rare and only happens when seeking backwards.
127320c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        final long currentTime = Math.max(frameTime, mStartTime);
12743618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        boolean finished = animateBasedOnTime(currentTime);
12753618d30f8ab6018025b11869676b309c3b4961cfDoris Liu
12763618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        if (finished) {
12773618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            endAnimation();
12783618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        }
127920c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown    }
128020c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown
128120c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown    /**
1282a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     * Returns the current animation fraction, which is the elapsed/interpolated fraction used in
1283a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     * the most recent frame update on the animation.
1284a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     *
1285a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     * @return Elapsed/interpolated fraction of the animation.
1286a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     */
1287a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    public float getAnimatedFraction() {
1288a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase        return mCurrentFraction;
1289a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    }
1290a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase
1291a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    /**
1292a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This method is called with the elapsed fraction of the animation during every
1293a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation frame. This function turns the elapsed fraction into an interpolated fraction
1294a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * and then into an animated value (from the evaluator. The function is called mostly during
1295a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation updates, but it is also called when the <code>end()</code>
1296a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function is called, to set the final value on the property.
1297a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1298a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>Overrides of this method must call the superclass to perform the calculation
1299a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of the animated value.</p>
1300a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1301a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param fraction The elapsed fraction of the animation.
1302a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1303c615c6fc9caca76cd96998f86e1f1e6393aeadbbTor Norbye    @CallSuper
1304a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    void animateValue(float fraction) {
1305a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        fraction = mInterpolator.getInterpolation(fraction);
1306a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase        mCurrentFraction = fraction;
1307a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        int numValues = mValues.length;
1308a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        for (int i = 0; i < numValues; ++i) {
1309a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mValues[i].calculateValue(fraction);
1310a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1311a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners != null) {
1312a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            int numListeners = mUpdateListeners.size();
1313a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 0; i < numListeners; ++i) {
1314a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mUpdateListeners.get(i).onAnimationUpdate(this);
1315a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1316a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1317a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1318a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1319a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
1320a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public ValueAnimator clone() {
1321a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        final ValueAnimator anim = (ValueAnimator) super.clone();
1322a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners != null) {
1323d422dc358f0100106dc07d7b903201eb9b043b11Yigit Boyar            anim.mUpdateListeners = new ArrayList<AnimatorUpdateListener>(mUpdateListeners);
1324a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
13250d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase        anim.mSeekFraction = -1;
1326f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        anim.mReversing = false;
1327a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mInitialized = false;
132826e9a19900bae56b012425a114685d42dfa2fde1ztenghui        anim.mStarted = false;
132926e9a19900bae56b012425a114685d42dfa2fde1ztenghui        anim.mRunning = false;
133026e9a19900bae56b012425a114685d42dfa2fde1ztenghui        anim.mPaused = false;
133126e9a19900bae56b012425a114685d42dfa2fde1ztenghui        anim.mResumed = false;
133226e9a19900bae56b012425a114685d42dfa2fde1ztenghui        anim.mStartListenersCalled = false;
1333e1b5c2b48a5cf1dd3712dde35b59bc18851b4018ztenghui        anim.mStartTime = 0;
1334e1b5c2b48a5cf1dd3712dde35b59bc18851b4018ztenghui        anim.mStartTimeCommitted = false;
13353dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu        anim.mAnimationEndRequested = false;
1336e1b5c2b48a5cf1dd3712dde35b59bc18851b4018ztenghui        anim.mPauseTime = 0;
13373618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        anim.mLastFrameTime = 0;
1338fbe94ece70c8064e9d9d6f6c278bb9ab310911cdDoris Liu        anim.mOverallFraction = 0;
1339e1b5c2b48a5cf1dd3712dde35b59bc18851b4018ztenghui        anim.mCurrentFraction = 0;
134026e9a19900bae56b012425a114685d42dfa2fde1ztenghui
1341a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        PropertyValuesHolder[] oldValues = mValues;
1342a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (oldValues != null) {
1343a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            int numValues = oldValues.length;
1344a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            anim.mValues = new PropertyValuesHolder[numValues];
1345a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            anim.mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues);
1346a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 0; i < numValues; ++i) {
1347d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase                PropertyValuesHolder newValuesHolder = oldValues[i].clone();
1348d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase                anim.mValues[i] = newValuesHolder;
1349d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase                anim.mValuesMap.put(newValuesHolder.getPropertyName(), newValuesHolder);
1350a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1351a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1352a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return anim;
1353a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1354a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1355a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1356a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Implementors of this interface can add themselves as update listeners
1357a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * to an <code>ValueAnimator</code> instance to receive callbacks on every animation
1358a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frame, after the current frame's values have been calculated for that
1359a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <code>ValueAnimator</code>.
1360a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1361a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static interface AnimatorUpdateListener {
1362a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        /**
1363a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * <p>Notifies the occurrence of another frame of the animation.</p>
1364a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         *
1365a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * @param animation The animation which was repeated.
1366a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         */
1367a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        void onAnimationUpdate(ValueAnimator animation);
1368a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1369a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1370599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick
1371599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick    /**
1372599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     * Return the number of animations currently running.
1373599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     *
13749c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown     * Used by StrictMode internally to annotate violations.
13759c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown     * May be called on arbitrary threads!
1376599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     *
1377599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     * @hide
1378599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     */
1379599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick    public static int getCurrentAnimationsCount() {
13803618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        return AnimationHandler.getAnimationCount();
13818901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy    }
1382e9140a72b1059574046a624b471b2c3a35806496Chet Haase
1383e9140a72b1059574046a624b471b2c3a35806496Chet Haase    @Override
1384e9140a72b1059574046a624b471b2c3a35806496Chet Haase    public String toString() {
1385e9140a72b1059574046a624b471b2c3a35806496Chet Haase        String returnVal = "ValueAnimator@" + Integer.toHexString(hashCode());
1386e9140a72b1059574046a624b471b2c3a35806496Chet Haase        if (mValues != null) {
1387e9140a72b1059574046a624b471b2c3a35806496Chet Haase            for (int i = 0; i < mValues.length; ++i) {
1388e9140a72b1059574046a624b471b2c3a35806496Chet Haase                returnVal += "\n    " + mValues[i].toString();
1389e9140a72b1059574046a624b471b2c3a35806496Chet Haase            }
1390e9140a72b1059574046a624b471b2c3a35806496Chet Haase        }
1391e9140a72b1059574046a624b471b2c3a35806496Chet Haase        return returnVal;
1392e9140a72b1059574046a624b471b2c3a35806496Chet Haase    }
1393d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck
1394d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck    /**
1395d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <p>Whether or not the ValueAnimator is allowed to run asynchronously off of
1396d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * the UI thread. This is a hint that informs the ValueAnimator that it is
1397d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * OK to run the animation off-thread, however ValueAnimator may decide
1398d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * that it must run the animation on the UI thread anyway. For example if there
1399d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * is an {@link AnimatorUpdateListener} the animation will run on the UI thread,
1400d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * regardless of the value of this hint.</p>
1401d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *
1402d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <p>Regardless of whether or not the animation runs asynchronously, all
1403d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * listener callbacks will be called on the UI thread.</p>
1404d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *
1405d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <p>To be able to use this hint the following must be true:</p>
1406d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <ol>
1407d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <li>{@link #getAnimatedFraction()} is not needed (it will return undefined values).</li>
1408d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <li>The animator is immutable while {@link #isStarted()} is true. Requests
1409d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    to change values, duration, delay, etc... may be ignored.</li>
1410d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <li>Lifecycle callback events may be asynchronous. Events such as
1411d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    {@link Animator.AnimatorListener#onAnimationEnd(Animator)} or
1412d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    {@link Animator.AnimatorListener#onAnimationRepeat(Animator)} may end up delayed
1413d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    as they must be posted back to the UI thread, and any actions performed
1414d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    by those callbacks (such as starting new animations) will not happen
1415d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    in the same frame.</li>
1416d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <li>State change requests ({@link #cancel()}, {@link #end()}, {@link #reverse()}, etc...)
1417d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    may be asynchronous. It is guaranteed that all state changes that are
1418d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    performed on the UI thread in the same frame will be applied as a single
1419d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    atomic update, however that frame may be the current frame,
1420d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    the next frame, or some future frame. This will also impact the observed
1421d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    state of the Animator. For example, {@link #isStarted()} may still return true
1422d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    after a call to {@link #end()}. Using the lifecycle callbacks is preferred over
1423d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    queries to {@link #isStarted()}, {@link #isRunning()}, and {@link #isPaused()}
1424d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    for this reason.</li>
1425d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * </ol>
1426d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * @hide
1427d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     */
1428c01bd1167a1b08d59557f214ddc48cf24d3b8d0aJohn Reck    @Override
1429d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck    public void setAllowRunningAsynchronously(boolean mayRunAsync) {
1430d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck        // It is up to subclasses to support this, if they can.
1431d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck    }
1432599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick}
1433