ValueAnimator.java revision e1b5c2b48a5cf1dd3712dde35b59bc18851b4018
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;
20a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.os.Looper;
2118772ea228f3d292629c4f0b5f6392d047e0530dRomain Guyimport android.os.Trace;
222970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haaseimport android.util.AndroidRuntimeException;
23c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brownimport android.util.Log;
2496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brownimport android.view.Choreographer;
25a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.view.animation.AccelerateDecelerateInterpolator;
26a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.view.animation.AnimationUtils;
2727c1d4debb3848f5accd5673fffeeacad3e61648Chet Haaseimport android.view.animation.LinearInterpolator;
28a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
29a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport java.util.ArrayList;
30a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport java.util.HashMap;
31a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
32a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase/**
33a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This class provides a simple timing engine for running animations
34a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * which calculate animated values and set them on target objects.
35a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *
36a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>There is a single timing pulse that all animations use. It runs in a
37a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * custom handler to ensure that property changes happen on the UI thread.</p>
38a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *
39a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>By default, ValueAnimator uses non-linear time interpolation, via the
40a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link AccelerateDecelerateInterpolator} class, which accelerates into and decelerates
41a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * out of an animation. This behavior can be changed by calling
42e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase * {@link ValueAnimator#setInterpolator(TimeInterpolator)}.</p>
433aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez *
44d430753cba09acb07af8b313286f247c78a41a32Chet Haase * <p>Animators can be created from either code or resource files. Here is an example
45d430753cba09acb07af8b313286f247c78a41a32Chet Haase * of a ValueAnimator resource file:</p>
46d430753cba09acb07af8b313286f247c78a41a32Chet Haase *
47d430753cba09acb07af8b313286f247c78a41a32Chet Haase * {@sample development/samples/ApiDemos/res/anim/animator.xml ValueAnimatorResources}
48d430753cba09acb07af8b313286f247c78a41a32Chet Haase *
49d430753cba09acb07af8b313286f247c78a41a32Chet Haase * <p>It is also possible to use a combination of {@link PropertyValuesHolder} and
50d430753cba09acb07af8b313286f247c78a41a32Chet Haase * {@link Keyframe} resource tags to create a multi-step animation.
51d430753cba09acb07af8b313286f247c78a41a32Chet Haase * Note that you can specify explicit fractional values (from 0 to 1) for
52d430753cba09acb07af8b313286f247c78a41a32Chet Haase * each keyframe to determine when, in the overall duration, the animation should arrive at that
53d430753cba09acb07af8b313286f247c78a41a32Chet Haase * value. Alternatively, you can leave the fractions off and the keyframes will be equally
54d430753cba09acb07af8b313286f247c78a41a32Chet Haase * distributed within the total duration:</p>
55d430753cba09acb07af8b313286f247c78a41a32Chet Haase *
56d430753cba09acb07af8b313286f247c78a41a32Chet Haase * {@sample development/samples/ApiDemos/res/anim/value_animator_pvh_kf.xml
57d430753cba09acb07af8b313286f247c78a41a32Chet Haase * ValueAnimatorKeyframeResources}
58d430753cba09acb07af8b313286f247c78a41a32Chet Haase *
593aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <div class="special reference">
603aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <h3>Developer Guides</h3>
613aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <p>For more information about animating with {@code ValueAnimator}, read the
623aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <a href="{@docRoot}guide/topics/graphics/prop-animation.html#value-animator">Property
633aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * Animation</a> developer guide.</p>
643aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * </div>
65a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */
6618772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy@SuppressWarnings("unchecked")
672794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haasepublic class ValueAnimator extends Animator {
68c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    private static final String TAG = "ValueAnimator";
69c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    private static final boolean DEBUG = false;
70a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
71a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
72a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Internal constants
73a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
74d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase    private static float sDurationScale = 1.0f;
75a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
76a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
77a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Values used with internal variable mPlayingState to indicate the current state of an
78a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation.
79a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
80051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    static final int STOPPED    = 0; // Not yet playing
81051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    static final int RUNNING    = 1; // Playing normally
82051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    static final int SEEKED     = 2; // Seeked to some time value
83a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
84a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
85a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Internal variables
86a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * NOTE: This object implements the clone() method, making a deep copy of any referenced
87a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * objects. As other non-trivial fields are added to this class, make sure to add logic
88a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * to clone() to make deep copies of them.
89a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
90a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
91c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    /**
92c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * The first time that the animation's animateFrame() method is called. This time is used to
93c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * determine elapsed time (and therefore the elapsed fraction) in subsequent calls
94c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * to animateFrame().
95c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     *
96c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * Whenever mStartTime is set, you must also update mStartTimeCommitted.
97c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     */
98051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    long mStartTime;
99a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
100a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
101c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * When true, the start time has been firmly committed as a chosen reference point in
102c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * time by which the progress of the animation will be evaluated.  When false, the
103c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * start time may be updated when the first animation frame is committed so as
104c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * to compensate for jank that may have occurred between when the start time was
105c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * initialized and when the frame was actually drawn.
106c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     *
107c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * This flag is generally set to false during the first frame of the animation
108c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * when the animation playing state transitions from STOPPED to RUNNING or
109c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * resumes after having been paused.  This flag is set to true when the start time
110c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * is firmly committed and should not be further compensated for jank.
111c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     */
112c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    boolean mStartTimeCommitted;
113c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown
114c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    /**
115a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Set when setCurrentPlayTime() is called. If negative, animation is not currently seeked
116a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * to a value.
117a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1180d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase    float mSeekFraction = -1;
119a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1208aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    /**
1218aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * Set on the next frame after pause() is called, used to calculate a new startTime
1228aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * or delayStartTime which allows the animator to continue from the point at which
1238aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * it was paused. If negative, has not yet been set.
1248aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     */
1258aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    private long mPauseTime;
1268aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
1278aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    /**
1288aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * Set when an animator is resumed. This triggers logic in the next frame which
1298aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * actually resumes the animator.
1308aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     */
1318aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    private boolean mResumed = false;
1328aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
1338aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
134a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The static sAnimationHandler processes the internal timing loop on which all animations
135a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // are based
136be19e030a14c8e398e8af97fa898ea80187704dfChet Haase    /**
137be19e030a14c8e398e8af97fa898ea80187704dfChet Haase     * @hide
138be19e030a14c8e398e8af97fa898ea80187704dfChet Haase     */
139be19e030a14c8e398e8af97fa898ea80187704dfChet Haase    protected static ThreadLocal<AnimationHandler> sAnimationHandler =
140e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase            new ThreadLocal<AnimationHandler>();
141e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase
142a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The time interpolator to be used if none is set on the animation
143e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    private static final TimeInterpolator sDefaultInterpolator =
144e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase            new AccelerateDecelerateInterpolator();
145a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
146a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
147a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Used to indicate whether the animation is currently playing in reverse. This causes the
148a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * elapsed fraction to be inverted to calculate the appropriate values.
149a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
150a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private boolean mPlayingBackwards = false;
151a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
152a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
153f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * Flag to indicate whether this animator is playing in reverse mode, specifically
154f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * by being started or interrupted by a call to reverse(). This flag is different than
155f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * mPlayingBackwards, which indicates merely whether the current iteration of the
156f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * animator is playing in reverse. It is used in corner cases to determine proper end
157f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * behavior.
158f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     */
159f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase    private boolean mReversing;
160f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase
161f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase    /**
162a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This variable tracks the current iteration that is playing. When mCurrentIteration exceeds the
163a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * repeatCount (if repeatCount!=INFINITE), the animation ends
164a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
165a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private int mCurrentIteration = 0;
166a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
167a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
168a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     * Tracks current elapsed/eased fraction, for querying in getAnimatedFraction().
169a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     */
170a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    private float mCurrentFraction = 0f;
171a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase
172a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    /**
173a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Tracks whether a startDelay'd animation has begun playing through the startDelay.
174a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
175a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private boolean mStartedDelay = false;
176a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
177a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
178a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Tracks the time at which the animation began playing through its startDelay. This is
179a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * different from the mStartTime variable, which is used to track when the animation became
180a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * active (which is when the startDelay expired and the animation was added to the active
181a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animations list).
182a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
183a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private long mDelayStartTime;
184a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
185a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
186a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Flag that represents the current state of the animation. Used to figure out when to start
187a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * an animation (if state == STOPPED). Also used to end an animation that
188a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * has been cancel()'d or end()'d since the last animation frame. Possible values are
189e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase     * STOPPED, RUNNING, SEEKED.
190a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
191051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    int mPlayingState = STOPPED;
192a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
193a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
194b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * Additional playing state to indicate whether an animator has been start()'d. There is
195b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * some lag between a call to start() and the first animation frame. We should still note
196b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * that the animation has been started, even if it's first animation frame has not yet
197b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * happened, and reflect that state in isRunning().
198b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * Note that delayed animations are different: they are not started until their first
199b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * animation frame, which occurs after their delay elapses.
200b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     */
2018b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    private boolean mRunning = false;
2028b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase
2038b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    /**
2048b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     * Additional playing state to indicate whether an animator has been start()'d, whether or
2058b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     * not there is a nonzero startDelay.
2068b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     */
207b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase    private boolean mStarted = false;
208b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase
209b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase    /**
2108aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * Tracks whether we've notified listeners of the onAnimationStart() event. This can be
21117cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase     * complex to keep track of since we notify listeners at different times depending on
21217cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase     * startDelay and whether start() was called before end().
21317cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase     */
21417cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase    private boolean mStartListenersCalled = false;
21517cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase
21617cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase    /**
217a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Flag that denotes whether the animation is set up and ready to go. Used to
218a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * set up animation that has not yet been started.
219a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
220a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    boolean mInitialized = false;
221a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
222a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    //
223a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // Backing variables
224a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    //
225a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
226a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // How long the animation should last in ms
227c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase    private long mDuration = (long)(300 * sDurationScale);
228d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase    private long mUnscaledDuration = 300;
229a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
230a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The amount of time in ms to delay starting the animation after start() is called
231a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private long mStartDelay = 0;
232d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase    private long mUnscaledStartDelay = 0;
233a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
234a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The number of times the animation will repeat. The default is 0, which means the animation
235a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // will play only once
236a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private int mRepeatCount = 0;
237a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
238a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
239a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The type of repetition that will occur when repeatMode is nonzero. RESTART means the
240a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation will start from the beginning on every new cycle. REVERSE means the animation
241a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will reverse directions on each iteration.
242a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
243a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private int mRepeatMode = RESTART;
244a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
245a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
246a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The time interpolator to be used. The elapsed fraction of the animation will be passed
247a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * through this interpolator to calculate the interpolated fraction, which is then used to
248a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * calculate the animated values.
249a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
250e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    private TimeInterpolator mInterpolator = sDefaultInterpolator;
251a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
252a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
253a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The set of listeners to be sent events through the life of an animation.
254a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
255d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck    ArrayList<AnimatorUpdateListener> mUpdateListeners = null;
256a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
257a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
258a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The property/value sets being animated.
259a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
260a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    PropertyValuesHolder[] mValues;
261a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
262a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
263a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * A hashmap of the PropertyValuesHolder objects. This map is used to lookup animated values
264a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * by property name during calls to getAnimatedValue(String).
265a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
266a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    HashMap<String, PropertyValuesHolder> mValuesMap;
267a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
268a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
269a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Public constants
270a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
271a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
272a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
273a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * When the animation reaches the end and <code>repeatCount</code> is INFINITE
274a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * or a positive value, the animation restarts from the beginning.
275a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
276a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static final int RESTART = 1;
277a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
278a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * When the animation reaches the end and <code>repeatCount</code> is INFINITE
279a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * or a positive value, the animation reverses direction on every iteration.
280a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
281a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static final int REVERSE = 2;
282a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
283a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This value used used with the {@link #setRepeatCount(int)} property to repeat
284a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the animation indefinitely.
285a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
286a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static final int INFINITE = -1;
287a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
288c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase
289c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase    /**
290c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase     * @hide
291c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase     */
292c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase    public static void setDurationScale(float durationScale) {
293c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase        sDurationScale = durationScale;
294c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase    }
295c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase
296a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
297ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown     * @hide
298ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown     */
299ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown    public static float getDurationScale() {
300ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown        return sDurationScale;
301ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown    }
302ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown
303ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown    /**
304a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Creates a new ValueAnimator object. This default constructor is primarily for
3052794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * use internally; the factory methods which take parameters are more generally
306a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * useful.
307a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
308a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public ValueAnimator() {
309a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
310a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
311a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
3122794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between int values. A single
3132794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
3142794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
3152794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
3162794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
3172794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
318a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
3192794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
3202794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
32141f041d9986f8a5d45b6cb0b86e881c81a412168Chet Haase     */
3222794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofInt(int... values) {
3232794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
3242794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setIntValues(values);
3252794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
3262794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
3272794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
3282794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
3291ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * Constructs and returns a ValueAnimator that animates between color values. A single
3301ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * value implies that that value is the one being animated to. However, this is not typically
3311ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * useful in a ValueAnimator object because there is no way for the object to determine the
3321ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * starting value for the animation (unlike ObjectAnimator, which can derive that value
3331ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * from the target object and property being animated). Therefore, there should typically
3341ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * be two or more values.
3351ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     *
3361ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * @param values A set of values that the animation will animate between over time.
3371ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * @return A ValueAnimator object that is set up to animate between the given values.
3381ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     */
3391ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount    public static ValueAnimator ofArgb(int... values) {
3401ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount        ValueAnimator anim = new ValueAnimator();
3411ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount        anim.setIntValues(values);
3421ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount        anim.setEvaluator(ArgbEvaluator.getInstance());
3431ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount        return anim;
3441ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount    }
3451ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount
3461ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount    /**
3472794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between float values. A single
3482794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
3492794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
3502794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
3512794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
3522794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
3532794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3542794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
3552794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
3562794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
3572794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofFloat(float... values) {
3582794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
3592794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setFloatValues(values);
3602794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
3612794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
3622794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
3632794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
3642794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between the values
3652794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * specified in the PropertyValuesHolder objects.
3662794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3672794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of PropertyValuesHolder objects whose values will be animated
3682794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * between over time.
3692794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
3702794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
3712794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofPropertyValuesHolder(PropertyValuesHolder... values) {
3722794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
3732794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setValues(values);
3742794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
3752794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
3762794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
3772794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between Object values. A single
3782794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
3792794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
3802794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
3812794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
3822794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
3832794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3842794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>Since ValueAnimator does not know how to animate between arbitrary Objects, this
3852794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * factory method also takes a TypeEvaluator object that the ValueAnimator will use
3862794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * to perform that interpolation.
3872794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3882794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param evaluator A TypeEvaluator that will be called on each animation frame to
3892794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * provide the ncessry interpolation between the Object values to derive the animated
3902794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value.
3912794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
3922794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
3932794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
3942794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofObject(TypeEvaluator evaluator, Object... values) {
3952794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
3962794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setObjectValues(values);
3972794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setEvaluator(evaluator);
3982794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
3992794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
4002794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
4012794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
4022794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Sets int 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 setIntValues(int... values) {
4162794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (values == null || values.length == 0) {
4172794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            return;
4182794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
4192794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (mValues == null || mValues.length == 0) {
42018772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy            setValues(PropertyValuesHolder.ofInt("", values));
4212794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        } else {
4222794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            PropertyValuesHolder valuesHolder = mValues[0];
4232794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            valuesHolder.setIntValues(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 float values that will be animated between. 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     * @param values A set of values that the animation will animate between over time.
4422794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
4432794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public void setFloatValues(float... values) {
4442794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (values == null || values.length == 0) {
4452794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            return;
44641f041d9986f8a5d45b6cb0b86e881c81a412168Chet Haase        }
4472794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (mValues == null || mValues.length == 0) {
44818772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy            setValues(PropertyValuesHolder.ofFloat("", values));
4492794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        } else {
4502794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            PropertyValuesHolder valuesHolder = mValues[0];
4512794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            valuesHolder.setFloatValues(values);
4522794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
4532794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        // New property/values/target should cause re-initialization prior to starting
4542794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        mInitialized = false;
4552794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
4562794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
4572794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
4582794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Sets the values to animate between for this animation. A single
4592794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
4602794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
4612794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
4622794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
4632794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
4642794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4652794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>If there are already multiple sets of values defined for this ValueAnimator via more
4662794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * than one PropertyValuesHolder object, this method will set the values for the first
4672794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * of those objects.</p>
4682794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4692794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>There should be a TypeEvaluator set on the ValueAnimator that knows how to interpolate
4702794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * between these value objects. ValueAnimator only knows how to interpolate between the
4712794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * primitive types specified in the other setValues() methods.</p>
4722794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4732794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values The set of values to animate between.
4742794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
4752794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public void setObjectValues(Object... values) {
4762794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (values == null || values.length == 0) {
4772794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            return;
4782794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
4792794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (mValues == null || mValues.length == 0) {
48018772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy            setValues(PropertyValuesHolder.ofObject("", null, values));
4812794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        } else {
4822794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            PropertyValuesHolder valuesHolder = mValues[0];
4832794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            valuesHolder.setObjectValues(values);
4842794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
4852794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        // New property/values/target should cause re-initialization prior to starting
4862794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        mInitialized = false;
487a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
488a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
489a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
490a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets the values, per property, being animated between. This function is called internally
491f76a50ce8fdc6aea22cabc77b2977a1a15a79630Ken Wakasa     * by the constructors of ValueAnimator that take a list of values. But a ValueAnimator can
492a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * be constructed without values and this method can be called to set the values manually
493a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * instead.
494a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
495a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param values The set of values, per property, being animated between.
496a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
497a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setValues(PropertyValuesHolder... values) {
498a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        int numValues = values.length;
499a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mValues = values;
500a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues);
501a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        for (int i = 0; i < numValues; ++i) {
50218772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy            PropertyValuesHolder valuesHolder = values[i];
503a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mValuesMap.put(valuesHolder.getPropertyName(), valuesHolder);
504a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
5050e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase        // New property/values/target should cause re-initialization prior to starting
5060e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase        mInitialized = false;
507a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
508a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
509a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
510a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Returns the values that this ValueAnimator animates between. These values are stored in
511a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * PropertyValuesHolder objects, even if the ValueAnimator was created with a simple list
512a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of value objects instead.
513a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
514a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return PropertyValuesHolder[] An array of PropertyValuesHolder objects which hold the
515a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * values, per property, that define the animation.
516a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
517a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public PropertyValuesHolder[] getValues() {
518a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mValues;
519a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
520a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
521a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
522a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This function is called immediately before processing the first animation
523a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frame of an animation. If there is a nonzero <code>startDelay</code>, the
524a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function is called after that delay ends.
525a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * It takes care of the final initialization steps for the
526a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation.
527a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
528a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *  <p>Overrides of this method should call the superclass method to ensure
529a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *  that internal mechanisms for the animation are set up correctly.</p>
530a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
531c615c6fc9caca76cd96998f86e1f1e6393aeadbbTor Norbye    @CallSuper
532a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    void initAnimation() {
533a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (!mInitialized) {
534a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            int numValues = mValues.length;
535a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 0; i < numValues; ++i) {
536a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mValues[i].init();
537a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
538a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mInitialized = true;
539a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
540a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
541a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
542a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
543a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
5442794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Sets the length of the animation. The default duration is 300 milliseconds.
545a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
54627c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase     * @param duration The length of the animation, in milliseconds. This value cannot
54727c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase     * be negative.
5482794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return ValueAnimator The object called with setDuration(). This return
5492794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value makes it easier to compose statements together that construct and then set the
5502794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * duration, as in <code>ValueAnimator.ofInt(0, 10).setDuration(500).start()</code>.
551a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
552c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    @Override
5532794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public ValueAnimator setDuration(long duration) {
55427c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase        if (duration < 0) {
55527c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase            throw new IllegalArgumentException("Animators cannot have negative duration: " +
55627c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase                    duration);
55727c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase        }
558d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase        mUnscaledDuration = duration;
5597a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown        updateScaledDuration();
5602794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return this;
561a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
562a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
5637a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown    private void updateScaledDuration() {
5647a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown        mDuration = (long)(mUnscaledDuration * sDurationScale);
5657a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown    }
5667a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown
567a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
5682794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Gets the length of the animation. The default duration is 300 milliseconds.
569a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
570a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return The length of the animation, in milliseconds.
571a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
572c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    @Override
573a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public long getDuration() {
574d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase        return mUnscaledDuration;
575a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
576a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
577a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
578a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets the position of the animation to the specified point in time. This time should
579a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * be between 0 and the total duration of the animation, including any repetition. If
580a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the animation has not yet been started, then it will not advance forward after it is
581a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * set to this time; it will simply set the time to this value and perform any appropriate
582a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * actions based on that time. If the animation is already running, then setCurrentPlayTime()
583a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will set the current playing time to this value and continue playing from that point.
584a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
585a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param playTime The time, in milliseconds, to which the animation is advanced or rewound.
586a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
587a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setCurrentPlayTime(long playTime) {
5885a25e5bae223bbee56dab75e36d1d947c8c3cb11Chet Haase        float fraction = mUnscaledDuration > 0 ? (float) playTime / mUnscaledDuration : 1;
5890d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase        setCurrentFraction(fraction);
5900d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase    }
5910d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase
5920d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase    /**
5930d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * Sets the position of the animation to the specified fraction. This fraction should
5940d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * be between 0 and the total fraction of the animation, including any repetition. That is,
5950d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * a fraction of 0 will position the animation at the beginning, a value of 1 at the end,
596f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * and a value of 2 at the end of a reversing animator that repeats once. If
5970d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * the animation has not yet been started, then it will not advance forward after it is
5980d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * set to this fraction; it will simply set the fraction to this value and perform any
5990d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * appropriate actions based on that fraction. If the animation is already running, then
6000d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * setCurrentFraction() will set the current fraction to this value and continue
6015a5afe010d2f8fb7e8f00858b8a305b4745c0469Eino-Ville Talvala     * playing from that point. {@link Animator.AnimatorListener} events are not called
602f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * due to changing the fraction; those events are only processed while the animation
603f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * is running.
6040d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     *
605f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * @param fraction The fraction to which the animation is advanced or rewound. Values
606f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * outside the range of 0 to the maximum fraction for the animator will be clamped to
607f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * the correct range.
6080d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     */
6090d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase    public void setCurrentFraction(float fraction) {
610a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        initAnimation();
611f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        if (fraction < 0) {
612f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            fraction = 0;
613f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        }
614f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        int iteration = (int) fraction;
615f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        if (fraction == 1) {
616f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            iteration -= 1;
617f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        } else if (fraction > 1) {
618f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            if (iteration < (mRepeatCount + 1) || mRepeatCount == INFINITE) {
619f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                if (mRepeatMode == REVERSE) {
620f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                    mPlayingBackwards = (iteration % 2) != 0;
621f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                }
622f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                fraction = fraction % 1f;
623f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            } else {
624f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                fraction = 1;
625f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                iteration -= 1;
626f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            }
627f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        } else {
628f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            mPlayingBackwards = mReversing;
629f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        }
630f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        mCurrentIteration = iteration;
631f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        long seekTime = (long) (mDuration * fraction);
632f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        long currentTime = AnimationUtils.currentAnimationTimeMillis();
633f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        mStartTime = currentTime - seekTime;
634c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown        mStartTimeCommitted = true; // do not allow start time to be compensated for jank
635a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mPlayingState != RUNNING) {
6360d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase            mSeekFraction = fraction;
637a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mPlayingState = SEEKED;
638a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
639f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        if (mPlayingBackwards) {
640f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            fraction = 1f - fraction;
641f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        }
6420d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase        animateValue(fraction);
643a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
644a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
645a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
646a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Gets the current position of the animation in time, which is equal to the current
647a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * time minus the time that the animation started. An animation that is not yet started will
648a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * return a value of zero.
649a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
650a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return The current position in time of the animation.
651a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
652a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public long getCurrentPlayTime() {
653a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (!mInitialized || mPlayingState == STOPPED) {
654a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return 0;
655a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
656a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return AnimationUtils.currentAnimationTimeMillis() - mStartTime;
657a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
658a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
659a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
660a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This custom, static handler handles the timing pulse that is shared by
661a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * all active animations. This approach ensures that the setting of animation
662a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * values will happen on the UI thread and that all animations will share
663a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the same times for calculating their values, which makes synchronizing
664a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animations possible.
665a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
66620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     * The handler uses the Choreographer for executing periodic callbacks.
667be19e030a14c8e398e8af97fa898ea80187704dfChet Haase     *
668be19e030a14c8e398e8af97fa898ea80187704dfChet Haase     * @hide
669a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
67018772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy    @SuppressWarnings("unchecked")
671c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    protected static class AnimationHandler {
6729c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        // The per-thread list of all active animations
673be19e030a14c8e398e8af97fa898ea80187704dfChet Haase        /** @hide */
674be19e030a14c8e398e8af97fa898ea80187704dfChet Haase        protected final ArrayList<ValueAnimator> mAnimations = new ArrayList<ValueAnimator>();
6759c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown
6762936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase        // Used in doAnimationFrame() to avoid concurrent modifications of mAnimations
6772936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase        private final ArrayList<ValueAnimator> mTmpAnimations = new ArrayList<ValueAnimator>();
6782936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase
6799c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        // The per-thread set of animations to be started on the next animation frame
680be19e030a14c8e398e8af97fa898ea80187704dfChet Haase        /** @hide */
681be19e030a14c8e398e8af97fa898ea80187704dfChet Haase        protected final ArrayList<ValueAnimator> mPendingAnimations = new ArrayList<ValueAnimator>();
6829c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown
6839c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        /**
6849c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown         * Internal per-thread collections used to avoid set collisions as animations start and end
6859c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown         * while being processed.
686be19e030a14c8e398e8af97fa898ea80187704dfChet Haase         * @hide
6879c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown         */
688be19e030a14c8e398e8af97fa898ea80187704dfChet Haase        protected final ArrayList<ValueAnimator> mDelayedAnims = new ArrayList<ValueAnimator>();
6899c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        private final ArrayList<ValueAnimator> mEndingAnims = new ArrayList<ValueAnimator>();
6909c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        private final ArrayList<ValueAnimator> mReadyAnims = new ArrayList<ValueAnimator>();
6919c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown
69296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        private final Choreographer mChoreographer;
6934a06c8008b2edd6677f9a411af79b0a4971b87feJeff Brown        private boolean mAnimationScheduled;
694c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown        private long mLastFrameTime;
69596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown
69696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        private AnimationHandler() {
69796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            mChoreographer = Choreographer.getInstance();
69896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        }
69996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown
700a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        /**
70120c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown         * Start animating on the next frame.
702a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         */
70320c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        public void start() {
70420c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            scheduleAnimation();
70596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        }
706a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
707c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown        void doAnimationFrame(long frameTime) {
708c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            mLastFrameTime = frameTime;
709c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown
71096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // mPendingAnimations holds any animations that have requested to be started
71196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // We're going to clear mPendingAnimations, but starting animation may
71296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // cause more to be added to the pending list (for example, if one animation
71396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // starting triggers another starting). So we loop until mPendingAnimations
71496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // is empty.
71596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            while (mPendingAnimations.size() > 0) {
71696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                ArrayList<ValueAnimator> pendingCopy =
71796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                        (ArrayList<ValueAnimator>) mPendingAnimations.clone();
71896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                mPendingAnimations.clear();
71996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                int count = pendingCopy.size();
72096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                for (int i = 0; i < count; ++i) {
72196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    ValueAnimator anim = pendingCopy.get(i);
72296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    // If the animation has a startDelay, place it on the delayed list
72396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    if (anim.mStartDelay == 0) {
72496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                        anim.startAnimation(this);
72596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    } else {
72696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                        mDelayedAnims.add(anim);
727a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    }
72896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                }
72996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            }
730c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown
731c6ffab32415a58bbb010dcd115684f9dbc249710Chet Haase            // Next, process animations currently sitting on the delayed queue, adding
73296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // them to the active animations if they are ready
73396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            int numDelayedAnims = mDelayedAnims.size();
73496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            for (int i = 0; i < numDelayedAnims; ++i) {
73596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                ValueAnimator anim = mDelayedAnims.get(i);
73620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown                if (anim.delayedAnimationFrame(frameTime)) {
73796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    mReadyAnims.add(anim);
73896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                }
73996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            }
74096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            int numReadyAnims = mReadyAnims.size();
74196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            if (numReadyAnims > 0) {
74296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                for (int i = 0; i < numReadyAnims; ++i) {
74396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    ValueAnimator anim = mReadyAnims.get(i);
74496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    anim.startAnimation(this);
74596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    anim.mRunning = true;
74696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    mDelayedAnims.remove(anim);
74796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                }
74896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                mReadyAnims.clear();
74996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            }
75096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown
75196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // Now process all active animations. The return value from animationFrame()
75296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // tells the handler whether it should now be ended
75396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            int numAnims = mAnimations.size();
7542936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase            for (int i = 0; i < numAnims; ++i) {
7552936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase                mTmpAnimations.add(mAnimations.get(i));
7562936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase            }
7572936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase            for (int i = 0; i < numAnims; ++i) {
7582936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase                ValueAnimator anim = mTmpAnimations.get(i);
7592936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase                if (mAnimations.contains(anim) && anim.doAnimationFrame(frameTime)) {
76096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    mEndingAnims.add(anim);
76196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                }
76296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            }
7632936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase            mTmpAnimations.clear();
76496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            if (mEndingAnims.size() > 0) {
7652936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase                for (int i = 0; i < mEndingAnims.size(); ++i) {
76696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    mEndingAnims.get(i).endAnimation(this);
76796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                }
76896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                mEndingAnims.clear();
76996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            }
77096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown
771c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            // Schedule final commit for the frame.
772c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            mChoreographer.postCallback(Choreographer.CALLBACK_COMMIT, mCommit, null);
773c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown
77496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // If there are still active or delayed animations, schedule a future call to
77596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // onAnimate to process the next frame of the animations.
77620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            if (!mAnimations.isEmpty() || !mDelayedAnims.isEmpty()) {
77720c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown                scheduleAnimation();
778a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
779a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
78096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown
781c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown        void commitAnimationFrame(long frameTime) {
782c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            final long adjustment = frameTime - mLastFrameTime;
783c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            final int numAnims = mAnimations.size();
784c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            for (int i = 0; i < numAnims; ++i) {
785c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                mAnimations.get(i).commitAnimationFrame(adjustment);
786c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            }
78720c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        }
78820c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown
78920c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        private void scheduleAnimation() {
79020c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            if (!mAnimationScheduled) {
791c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                mChoreographer.postCallback(Choreographer.CALLBACK_ANIMATION, mAnimate, null);
79220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown                mAnimationScheduled = true;
79320c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            }
79496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        }
795c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown
796c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown        // Called by the Choreographer.
797c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown        final Runnable mAnimate = new Runnable() {
798c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            @Override
799c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            public void run() {
800c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                mAnimationScheduled = false;
801c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                doAnimationFrame(mChoreographer.getFrameTime());
802c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            }
803c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown        };
804c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown
805c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown        // Called by the Choreographer.
806c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown        final Runnable mCommit = new Runnable() {
807c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            @Override
808c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            public void run() {
809c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                commitAnimationFrame(mChoreographer.getFrameTime());
810c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            }
811c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown        };
812a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
813a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
814a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
815a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, to delay starting the animation after
816a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link #start()} is called.
817a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
818a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return the number of milliseconds to delay running the animation
819a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
820c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    @Override
821a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public long getStartDelay() {
822d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase        return mUnscaledStartDelay;
823a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
824a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
825a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
826a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, to delay starting the animation after
827a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link #start()} is called.
828a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
829a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param startDelay The amount of the delay, in milliseconds
830a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
831c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    @Override
832a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setStartDelay(long startDelay) {
833d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase        this.mStartDelay = (long)(startDelay * sDurationScale);
834d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase        mUnscaledStartDelay = startDelay;
835a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
836a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
837a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
838a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, between each frame of the animation. This is a
839a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * requested time that the animation will attempt to honor, but the actual delay between
840a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frames may be different, depending on system load and capabilities. This is a static
841a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function because the same delay will be applied to all animations, since they are all
842a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * run off of a single timing loop.
843a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
84496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     * The frame delay may be ignored when the animation system uses an external timing
84596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     * source, such as the display refresh rate (vsync), to govern animations.
84696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     *
847a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return the requested time between frames, in milliseconds
848a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
849a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static long getFrameDelay() {
85096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        return Choreographer.getFrameDelay();
851a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
852a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
853a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
854a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, between each frame of the animation. This is a
855a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * requested time that the animation will attempt to honor, but the actual delay between
856a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frames may be different, depending on system load and capabilities. This is a static
857a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function because the same delay will be applied to all animations, since they are all
858a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * run off of a single timing loop.
859a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
86096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     * The frame delay may be ignored when the animation system uses an external timing
86196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     * source, such as the display refresh rate (vsync), to govern animations.
86296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     *
863a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param frameDelay the requested time between frames, in milliseconds
864a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
865a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static void setFrameDelay(long frameDelay) {
86696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        Choreographer.setFrameDelay(frameDelay);
867a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
868a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
869a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
870a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The most recent value calculated by this <code>ValueAnimator</code> when there is just one
871a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * property being animated. This value is only sensible while the animation is running. The main
872a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * purpose for this read-only property is to retrieve the value from the <code>ValueAnimator</code>
873a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * during a call to {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which
874a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is called during each animation frame, immediately after the value is calculated.
875a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
876a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return animatedValue The value most recently calculated by this <code>ValueAnimator</code> for
877a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the single property being animated. If there are several properties being animated
878a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * (specified by several PropertyValuesHolder objects in the constructor), this function
879a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * returns the animated value for the first of those objects.
880a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
881a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public Object getAnimatedValue() {
882a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mValues != null && mValues.length > 0) {
883a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return mValues[0].getAnimatedValue();
884a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
885a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        // Shouldn't get here; should always have values unless ValueAnimator was set up wrong
886a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return null;
887a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
888a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
889a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
890a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The most recent value calculated by this <code>ValueAnimator</code> for <code>propertyName</code>.
891a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The main purpose for this read-only property is to retrieve the value from the
892a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <code>ValueAnimator</code> during a call to
893a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which
894a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is called during each animation frame, immediately after the value is calculated.
895a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
896a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return animatedValue The value most recently calculated for the named property
897a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * by this <code>ValueAnimator</code>.
898a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
899a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public Object getAnimatedValue(String propertyName) {
900a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        PropertyValuesHolder valuesHolder = mValuesMap.get(propertyName);
901a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (valuesHolder != null) {
902a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return valuesHolder.getAnimatedValue();
903a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        } else {
904a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // At least avoid crashing if called with bogus propertyName
905a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return null;
906a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
907a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
908a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
909a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
910a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets how many times the animation should be repeated. If the repeat
911a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * count is 0, the animation is never repeated. If the repeat count is
912a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * greater than 0 or {@link #INFINITE}, the repeat mode will be taken
913a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * into account. The repeat count is 0 by default.
914a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
915a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param value the number of times the animation should be repeated
916a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
917a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setRepeatCount(int value) {
918a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mRepeatCount = value;
919a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
920a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
921a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Defines how many times the animation should repeat. The default value
922a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is 0.
923a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
924a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return the number of times the animation should repeat, or {@link #INFINITE}
925a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
926a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public int getRepeatCount() {
927a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mRepeatCount;
928a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
929a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
930a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
931a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Defines what this animation should do when it reaches the end. This
932a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * setting is applied only when the repeat count is either greater than
933a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * 0 or {@link #INFINITE}. Defaults to {@link #RESTART}.
934a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
935a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param value {@link #RESTART} or {@link #REVERSE}
936a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
937a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setRepeatMode(int value) {
938a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mRepeatMode = value;
939a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
940a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
941a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
942a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Defines what this animation should do when it reaches the end.
943a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
944a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return either one of {@link #REVERSE} or {@link #RESTART}
945a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
946a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public int getRepeatMode() {
947a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mRepeatMode;
948a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
949a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
950a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
951a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Adds a listener to the set of listeners that are sent update events through the life of
952a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * an animation. This method is called on all listeners for every frame of the animation,
953a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * after the values for the animation have been calculated.
954a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
955a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param listener the listener to be added to the current set of listeners for this animation.
956a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
957a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void addUpdateListener(AnimatorUpdateListener listener) {
958a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners == null) {
959a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mUpdateListeners = new ArrayList<AnimatorUpdateListener>();
960a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
961a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mUpdateListeners.add(listener);
962a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
963a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
964a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
9653060421045d4d9e411797f91bb509824b03e33fbJim Miller     * Removes all listeners from the set listening to frame updates for this animation.
9663060421045d4d9e411797f91bb509824b03e33fbJim Miller     */
9673060421045d4d9e411797f91bb509824b03e33fbJim Miller    public void removeAllUpdateListeners() {
9683060421045d4d9e411797f91bb509824b03e33fbJim Miller        if (mUpdateListeners == null) {
9693060421045d4d9e411797f91bb509824b03e33fbJim Miller            return;
9703060421045d4d9e411797f91bb509824b03e33fbJim Miller        }
9713060421045d4d9e411797f91bb509824b03e33fbJim Miller        mUpdateListeners.clear();
9723060421045d4d9e411797f91bb509824b03e33fbJim Miller        mUpdateListeners = null;
9733060421045d4d9e411797f91bb509824b03e33fbJim Miller    }
9743060421045d4d9e411797f91bb509824b03e33fbJim Miller
9753060421045d4d9e411797f91bb509824b03e33fbJim Miller    /**
976a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Removes a listener from the set listening to frame updates for this animation.
977a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
978a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param listener the listener to be removed from the current set of update listeners
979a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * for this animation.
980a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
981a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void removeUpdateListener(AnimatorUpdateListener listener) {
982a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners == null) {
983a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return;
984a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
985a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mUpdateListeners.remove(listener);
986a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners.size() == 0) {
987a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mUpdateListeners = null;
988a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
989a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
990a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
991a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
992a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
993a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The time interpolator used in calculating the elapsed fraction of this animation. The
994a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * interpolator determines whether the animation runs with linear or non-linear motion,
995a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * such as acceleration and deceleration. The default value is
996a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link android.view.animation.AccelerateDecelerateInterpolator}
997a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
99827c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase     * @param value the interpolator to be used by this animation. A value of <code>null</code>
99927c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase     * will result in linear interpolation.
1000a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1001a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
1002e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    public void setInterpolator(TimeInterpolator value) {
1003a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (value != null) {
1004a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mInterpolator = value;
100527c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase        } else {
100627c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase            mInterpolator = new LinearInterpolator();
1007a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1008a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1009a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1010a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1011a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Returns the timing interpolator that this ValueAnimator uses.
1012a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1013a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return The timing interpolator for this ValueAnimator.
1014a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1015430742f09063574271e6c4091de13b9b9e762514Chet Haase    @Override
1016e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    public TimeInterpolator getInterpolator() {
1017a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mInterpolator;
1018a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1019a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1020a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1021a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The type evaluator to be used when calculating the animated values of this animation.
1022b2ab04ffb6894f399d5c9ceb15f64eb17b654426Chet Haase     * The system will automatically assign a float or int evaluator based on the type
1023a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of <code>startValue</code> and <code>endValue</code> in the constructor. But if these values
1024a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * are not one of these primitive types, or if different evaluation is desired (such as is
1025a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * necessary with int values that represent colors), a custom evaluator needs to be assigned.
102653ee3316bcb3590ff156b3fd7108903c0817c35dChet Haase     * For example, when running an animation on color values, the {@link ArgbEvaluator}
1027a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * should be used to get correct RGB color interpolation.
1028a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1029a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>If this ValueAnimator has only one set of values being animated between, this evaluator
1030a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will be used for that set. If there are several sets of values being animated, which is
1031fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase     * the case if PropertyValuesHolder objects were set on the ValueAnimator, then the evaluator
1032a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is assigned just to the first PropertyValuesHolder object.</p>
1033a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1034a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param value the evaluator to be used this animation
1035a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1036a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setEvaluator(TypeEvaluator value) {
1037a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (value != null && mValues != null && mValues.length > 0) {
1038a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mValues[0].setEvaluator(value);
1039a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1040a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1041a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
104217cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase    private void notifyStartListeners() {
104317cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        if (mListeners != null && !mStartListenersCalled) {
104417cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            ArrayList<AnimatorListener> tmpListeners =
104517cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                    (ArrayList<AnimatorListener>) mListeners.clone();
104617cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            int numListeners = tmpListeners.size();
104717cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            for (int i = 0; i < numListeners; ++i) {
104817cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                tmpListeners.get(i).onAnimationStart(this);
104917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            }
105017cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        }
105117cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        mStartListenersCalled = true;
105217cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase    }
105317cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase
1054a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1055a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Start the animation playing. This version of start() takes a boolean flag that indicates
1056a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * whether the animation should play in reverse. The flag is usually false, but may be set
10572970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * to true if called from the reverse() method.
10582970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     *
10592970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * <p>The animation started by calling this method will be run on the thread that called
10602970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * this method. This thread should have a Looper on it (a runtime exception will be thrown if
10612970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * this is not the case). Also, if the animation will animate
10622970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * properties of objects in the view hierarchy, then the calling thread should be the UI
10632970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * thread for that view hierarchy.</p>
1064a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1065a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param playBackwards Whether the ValueAnimator should start playing in reverse.
1066a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1067a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private void start(boolean playBackwards) {
10682970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        if (Looper.myLooper() == null) {
10692970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            throw new AndroidRuntimeException("Animators may only be run on Looper threads");
10703060421045d4d9e411797f91bb509824b03e33fbJim Miller        }
1071f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        mReversing = playBackwards;
10722970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        mPlayingBackwards = playBackwards;
1073f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        if (playBackwards && mSeekFraction != -1) {
1074f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            if (mSeekFraction == 0 && mCurrentIteration == 0) {
1075f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                // special case: reversing from seek-to-0 should act as if not seeked at all
1076f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                mSeekFraction = 0;
1077f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            } else if (mRepeatCount == INFINITE) {
1078f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                mSeekFraction = 1 - (mSeekFraction % 1);
1079f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            } else {
1080f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                mSeekFraction = 1 + mRepeatCount - (mCurrentIteration + mSeekFraction);
1081f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            }
1082f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            mCurrentIteration = (int) mSeekFraction;
1083f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            mSeekFraction = mSeekFraction % 1;
1084f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        }
1085f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        if (mCurrentIteration > 0 && mRepeatMode == REVERSE &&
1086f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                (mCurrentIteration < (mRepeatCount + 1) || mRepeatCount == INFINITE)) {
1087f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            // if we were seeked to some other iteration in a reversing animator,
1088f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            // figure out the correct direction to start playing based on the iteration
1089f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            if (playBackwards) {
1090f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                mPlayingBackwards = (mCurrentIteration % 2) == 0;
1091f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            } else {
1092f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                mPlayingBackwards = (mCurrentIteration % 2) != 0;
1093f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            }
1094f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        }
10950d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase        int prevPlayingState = mPlayingState;
1096add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase        mPlayingState = STOPPED;
10978b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        mStarted = true;
1098add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase        mStartedDelay = false;
10998aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        mPaused = false;
11007a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown        updateScaledDuration(); // in case the scale factor has changed since creation time
11019c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        AnimationHandler animationHandler = getOrCreateAnimationHandler();
11029c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        animationHandler.mPendingAnimations.add(this);
11032970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        if (mStartDelay == 0) {
1104add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase            // This sets the initial value of the animation, prior to actually starting it running
11050d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase            if (prevPlayingState != SEEKED) {
11060d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase                setCurrentPlayTime(0);
11070d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase            }
1108154f14508a11627d5a995b6fe2a14a83d794a6feChet Haase            mPlayingState = STOPPED;
11098b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase            mRunning = true;
111017cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            notifyStartListeners();
1111a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
111220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        animationHandler.start();
1113a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1114a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1115a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
1116a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void start() {
1117a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        start(false);
1118a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1119a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1120a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
1121a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void cancel() {
11222970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        // Only cancel if the animation is actually running or has been started and is about
11232970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        // to run
11249c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        AnimationHandler handler = getOrCreateAnimationHandler();
11259c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        if (mPlayingState != STOPPED
11269c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown                || handler.mPendingAnimations.contains(this)
11279c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown                || handler.mDelayedAnims.contains(this)) {
1128b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase            // Only notify listeners if the animator has actually started
112917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            if ((mStarted || mRunning) && mListeners != null) {
113017cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                if (!mRunning) {
113117cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                    // If it's not yet running, then start listeners weren't called. Call them now.
113217cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                    notifyStartListeners();
113317cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                }
11347dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                ArrayList<AnimatorListener> tmpListeners =
11357dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                        (ArrayList<AnimatorListener>) mListeners.clone();
11367dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                for (AnimatorListener listener : tmpListeners) {
11377dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                    listener.onAnimationCancel(this);
11387dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                }
11397dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase            }
11409c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown            endAnimation(handler);
11412970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        }
1142a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1143a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1144a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
1145a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void end() {
11469c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        AnimationHandler handler = getOrCreateAnimationHandler();
11479c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        if (!handler.mAnimations.contains(this) && !handler.mPendingAnimations.contains(this)) {
1148a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // Special case if the animation has not yet started; get it ready for ending
1149a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mStartedDelay = false;
11509c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown            startAnimation(handler);
115117cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            mStarted = true;
1152add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase        } else if (!mInitialized) {
1153add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase            initAnimation();
1154e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase        }
11554dd176864310e1d9519bf6b88918913e9927984fChet Haase        animateValue(mPlayingBackwards ? 0f : 1f);
11569c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        endAnimation(handler);
1157a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1158a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1159a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
11608aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    public void resume() {
11618aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        if (mPaused) {
11628aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            mResumed = true;
11638aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        }
11648aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        super.resume();
11658aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    }
11668aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
11678aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    @Override
11688aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    public void pause() {
11698aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        boolean previouslyPaused = mPaused;
11708aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        super.pause();
11718aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        if (!previouslyPaused && mPaused) {
11728aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            mPauseTime = -1;
11738aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            mResumed = false;
11748aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        }
11758aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    }
11768aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
11778aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    @Override
1178a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public boolean isRunning() {
11798b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        return (mPlayingState == RUNNING || mRunning);
11808b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    }
11818b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase
11828b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    @Override
11838b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    public boolean isStarted() {
11848b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        return mStarted;
1185a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1186a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1187a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1188a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Plays the ValueAnimator in reverse. If the animation is already running,
1189a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * it will stop itself and play backwards from the point reached when reverse was called.
1190a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * If the animation is not currently running, then it will start from the end and
1191a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * play backwards. This behavior is only set for the current animation; future playing
1192a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of the animation will use the default behavior of playing forward.
1193a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
11947bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui    @Override
1195a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void reverse() {
1196a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mPlayingBackwards = !mPlayingBackwards;
1197a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mPlayingState == RUNNING) {
1198a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            long currentTime = AnimationUtils.currentAnimationTimeMillis();
1199a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            long currentPlayTime = currentTime - mStartTime;
1200a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            long timeLeft = mDuration - currentPlayTime;
1201a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mStartTime = currentTime - timeLeft;
1202c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            mStartTimeCommitted = true; // do not allow start time to be compensated for jank
1203f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            mReversing = !mReversing;
1204f43fb2a57f152b5760d8792fec26f36d46b23817Chet Haase        } else if (mStarted) {
1205f43fb2a57f152b5760d8792fec26f36d46b23817Chet Haase            end();
1206a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        } else {
1207a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            start(true);
1208a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1209a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1210a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1211a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
12127bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui     * @hide
12137bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui     */
12147bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui    @Override
12157bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui    public boolean canReverse() {
12167bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui        return true;
12177bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui    }
12187bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui
12197bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui    /**
1220a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Called internally to end an animation by removing it from the animations list. Must be
1221a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * called on the UI thread.
1222d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * @hide
1223a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1224d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck    protected void endAnimation(AnimationHandler handler) {
12259c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        handler.mAnimations.remove(this);
12269c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        handler.mPendingAnimations.remove(this);
12279c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        handler.mDelayedAnims.remove(this);
1228a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mPlayingState = STOPPED;
12298aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        mPaused = false;
123017cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        if ((mStarted || mRunning) && mListeners != null) {
123117cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            if (!mRunning) {
123217cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                // If it's not yet running, then start listeners weren't called. Call them now.
123317cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                notifyStartListeners();
123417cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase             }
1235a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            ArrayList<AnimatorListener> tmpListeners =
1236a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    (ArrayList<AnimatorListener>) mListeners.clone();
12377c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            int numListeners = tmpListeners.size();
12387c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            for (int i = 0; i < numListeners; ++i) {
12397c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                tmpListeners.get(i).onAnimationEnd(this);
1240a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1241a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
12428b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        mRunning = false;
1243b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase        mStarted = false;
124417cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        mStartListenersCalled = false;
1245caf46486f54cdc899e383dfc776ec33a81b089a1Chet Haase        mPlayingBackwards = false;
1246f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        mReversing = false;
1247f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        mCurrentIteration = 0;
12489b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase        if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
12499b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase            Trace.asyncTraceEnd(Trace.TRACE_TAG_VIEW, getNameForTrace(),
12509b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase                    System.identityHashCode(this));
12519b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase        }
1252a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1253a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1254a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1255a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Called internally to start an animation by adding it to the active animations list. Must be
1256a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * called on the UI thread.
1257a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
12589c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown    private void startAnimation(AnimationHandler handler) {
12599b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase        if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
12609b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase            Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW, getNameForTrace(),
12619b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase                    System.identityHashCode(this));
12629b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase        }
1263a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        initAnimation();
12649c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        handler.mAnimations.add(this);
1265b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase        if (mStartDelay > 0 && mListeners != null) {
1266b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase            // Listeners were already notified in start() if startDelay is 0; this is
1267b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase            // just for delayed animations
126817cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            notifyStartListeners();
1269a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1270a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1271a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1272a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1273fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase     * Returns the name of this animator for debugging purposes.
1274fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase     */
1275fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase    String getNameForTrace() {
1276fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase        return "animator";
1277fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase    }
1278fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase
1279fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase
1280fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase    /**
1281a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Internal function called to process an animation frame on an animation that is currently
1282a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * sleeping through its <code>startDelay</code> phase. The return value indicates whether it
1283a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * should be woken up and put on the active animations queue.
1284a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1285a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param currentTime The current animation time, used to calculate whether the animation
1286a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * has exceeded its <code>startDelay</code> and should be started.
1287a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return True if the animation's <code>startDelay</code> has been exceeded and the animation
1288a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * should be added to the set of active animations.
1289a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1290a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private boolean delayedAnimationFrame(long currentTime) {
1291a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (!mStartedDelay) {
1292a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mStartedDelay = true;
1293a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mDelayStartTime = currentTime;
12942ed16ac6238227dab2687519268f3683f045e2acGeorge Mount        }
12952ed16ac6238227dab2687519268f3683f045e2acGeorge Mount        if (mPaused) {
12962ed16ac6238227dab2687519268f3683f045e2acGeorge Mount            if (mPauseTime < 0) {
12972ed16ac6238227dab2687519268f3683f045e2acGeorge Mount                mPauseTime = currentTime;
12988aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            }
12992ed16ac6238227dab2687519268f3683f045e2acGeorge Mount            return false;
13002ed16ac6238227dab2687519268f3683f045e2acGeorge Mount        } else if (mResumed) {
13012ed16ac6238227dab2687519268f3683f045e2acGeorge Mount            mResumed = false;
13022ed16ac6238227dab2687519268f3683f045e2acGeorge Mount            if (mPauseTime > 0) {
13032ed16ac6238227dab2687519268f3683f045e2acGeorge Mount                // Offset by the duration that the animation was paused
13042ed16ac6238227dab2687519268f3683f045e2acGeorge Mount                mDelayStartTime += (currentTime - mPauseTime);
1305a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1306a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
13072ed16ac6238227dab2687519268f3683f045e2acGeorge Mount        long deltaTime = currentTime - mDelayStartTime;
13082ed16ac6238227dab2687519268f3683f045e2acGeorge Mount        if (deltaTime > mStartDelay) {
1309c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            // startDelay ended - start the anim and record the mStartTime appropriately
1310c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            mStartTime = mDelayStartTime + mStartDelay;
1311c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            mStartTimeCommitted = true; // do not allow start time to be compensated for jank
13122ed16ac6238227dab2687519268f3683f045e2acGeorge Mount            mPlayingState = RUNNING;
13132ed16ac6238227dab2687519268f3683f045e2acGeorge Mount            return true;
13142ed16ac6238227dab2687519268f3683f045e2acGeorge Mount        }
1315a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return false;
1316a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1317a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1318a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1319c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * Applies an adjustment to the animation to compensate for jank between when
1320c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * the animation first ran and when the frame was drawn.
1321c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     */
1322c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    void commitAnimationFrame(long adjustment) {
1323c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown        if (!mStartTimeCommitted) {
1324c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            mStartTimeCommitted = true;
1325c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            if (mPlayingState == RUNNING && adjustment > 0) {
1326c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                mStartTime += adjustment;
1327c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                if (DEBUG) {
1328c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                    Log.d(TAG, "Adjusted start time by " + adjustment + " ms: " + toString());
1329c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                }
1330c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            }
1331c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown        }
1332c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    }
1333c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown
1334c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    /**
1335a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This internal function processes a single animation frame for a given animation. The
1336a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * currentTime parameter is the timing pulse sent by the handler, used to calculate the
1337a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * elapsed duration, and therefore
1338a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the elapsed fraction, of the animation. The return value indicates whether the animation
1339a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * should be ended (which happens when the elapsed time of the animation exceeds the
1340a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation's duration, including the repeatCount).
1341a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1342a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param currentTime The current time, as tracked by the static timing handler
1343a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return true if the animation's duration, including any repetitions due to
13448aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * <code>repeatCount</code>, has been exceeded and the animation should be ended.
1345a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1346051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    boolean animationFrame(long currentTime) {
1347a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        boolean done = false;
1348a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        switch (mPlayingState) {
1349a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        case RUNNING:
1350a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        case SEEKED:
135170d4ba15b1f0c1133c5aabc86de828b41e482fffChet Haase            float fraction = mDuration > 0 ? (float)(currentTime - mStartTime) / mDuration : 1f;
1352f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            if (mDuration == 0 && mRepeatCount != INFINITE) {
1353f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                // Skip to the end
1354f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                mCurrentIteration = mRepeatCount;
1355f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                if (!mReversing) {
1356f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                    mPlayingBackwards = false;
1357f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                }
1358f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            }
1359a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (fraction >= 1f) {
1360d15e94f0309a91d5a75d03a9ae165121e7f24907Chet Haase                if (mCurrentIteration < mRepeatCount || mRepeatCount == INFINITE) {
1361a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    // Time to repeat
1362a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    if (mListeners != null) {
13637c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                        int numListeners = mListeners.size();
13647c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                        for (int i = 0; i < numListeners; ++i) {
13657c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                            mListeners.get(i).onAnimationRepeat(this);
1366a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                        }
1367a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    }
1368a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    if (mRepeatMode == REVERSE) {
136918772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy                        mPlayingBackwards = !mPlayingBackwards;
1370a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    }
1371f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                    mCurrentIteration += (int) fraction;
1372730666858692ea396f5ad779654b5d86ff90b6caChet Haase                    fraction = fraction % 1f;
1373a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    mStartTime += mDuration;
1374c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                    // Note: We do not need to update the value of mStartTimeCommitted here
1375c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                    // since we just added a duration offset.
1376a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                } else {
1377a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    done = true;
1378a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    fraction = Math.min(fraction, 1.0f);
1379a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                }
1380a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1381a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (mPlayingBackwards) {
1382a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                fraction = 1f - fraction;
1383a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1384a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            animateValue(fraction);
1385a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            break;
1386a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1387a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1388a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return done;
1389a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1390a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1391a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
139220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     * Processes a frame of the animation, adjusting the start time if needed.
139320c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     *
139420c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     * @param frameTime The frame time.
139520c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     * @return true if the animation has ended.
139620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     */
139720c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown    final boolean doAnimationFrame(long frameTime) {
139820c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        if (mPlayingState == STOPPED) {
139920c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            mPlayingState = RUNNING;
14000d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase            if (mSeekFraction < 0) {
140120c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown                mStartTime = frameTime;
140220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            } else {
14030d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase                long seekTime = (long) (mDuration * mSeekFraction);
14040d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase                mStartTime = frameTime - seekTime;
14050d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase                mSeekFraction = -1;
140620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            }
1407c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            mStartTimeCommitted = false; // allow start time to be compensated for jank
140820c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        }
14098aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        if (mPaused) {
14108aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            if (mPauseTime < 0) {
14118aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                mPauseTime = frameTime;
14128aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            }
14138aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            return false;
14148aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        } else if (mResumed) {
14158aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            mResumed = false;
14168aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            if (mPauseTime > 0) {
14178aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                // Offset by the duration that the animation was paused
14188aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                mStartTime += (frameTime - mPauseTime);
1419c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                mStartTimeCommitted = false; // allow start time to be compensated for jank
14208aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            }
14218aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        }
142220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        // The frame time might be before the start time during the first frame of
142320c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        // an animation.  The "current time" must always be on or after the start
142420c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        // time to avoid animating frames at negative time intervals.  In practice, this
142520c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        // is very rare and only happens when seeking backwards.
142620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        final long currentTime = Math.max(frameTime, mStartTime);
142720c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        return animationFrame(currentTime);
142820c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown    }
142920c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown
143020c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown    /**
1431a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     * Returns the current animation fraction, which is the elapsed/interpolated fraction used in
1432a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     * the most recent frame update on the animation.
1433a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     *
1434a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     * @return Elapsed/interpolated fraction of the animation.
1435a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     */
1436a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    public float getAnimatedFraction() {
1437a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase        return mCurrentFraction;
1438a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    }
1439a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase
1440a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    /**
1441a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This method is called with the elapsed fraction of the animation during every
1442a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation frame. This function turns the elapsed fraction into an interpolated fraction
1443a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * and then into an animated value (from the evaluator. The function is called mostly during
1444a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation updates, but it is also called when the <code>end()</code>
1445a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function is called, to set the final value on the property.
1446a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1447a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>Overrides of this method must call the superclass to perform the calculation
1448a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of the animated value.</p>
1449a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1450a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param fraction The elapsed fraction of the animation.
1451a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1452c615c6fc9caca76cd96998f86e1f1e6393aeadbbTor Norbye    @CallSuper
1453a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    void animateValue(float fraction) {
1454a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        fraction = mInterpolator.getInterpolation(fraction);
1455a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase        mCurrentFraction = fraction;
1456a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        int numValues = mValues.length;
1457a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        for (int i = 0; i < numValues; ++i) {
1458a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mValues[i].calculateValue(fraction);
1459a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1460a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners != null) {
1461a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            int numListeners = mUpdateListeners.size();
1462a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 0; i < numListeners; ++i) {
1463a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mUpdateListeners.get(i).onAnimationUpdate(this);
1464a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1465a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1466a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1467a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1468a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
1469a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public ValueAnimator clone() {
1470a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        final ValueAnimator anim = (ValueAnimator) super.clone();
1471a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners != null) {
1472d422dc358f0100106dc07d7b903201eb9b043b11Yigit Boyar            anim.mUpdateListeners = new ArrayList<AnimatorUpdateListener>(mUpdateListeners);
1473a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
14740d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase        anim.mSeekFraction = -1;
1475a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mPlayingBackwards = false;
1476f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        anim.mReversing = false;
1477a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mCurrentIteration = 0;
1478a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mInitialized = false;
1479a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mPlayingState = STOPPED;
1480a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mStartedDelay = false;
148126e9a19900bae56b012425a114685d42dfa2fde1ztenghui        anim.mStarted = false;
148226e9a19900bae56b012425a114685d42dfa2fde1ztenghui        anim.mRunning = false;
148326e9a19900bae56b012425a114685d42dfa2fde1ztenghui        anim.mPaused = false;
148426e9a19900bae56b012425a114685d42dfa2fde1ztenghui        anim.mResumed = false;
148526e9a19900bae56b012425a114685d42dfa2fde1ztenghui        anim.mStartListenersCalled = false;
1486e1b5c2b48a5cf1dd3712dde35b59bc18851b4018ztenghui        anim.mStartTime = 0;
1487e1b5c2b48a5cf1dd3712dde35b59bc18851b4018ztenghui        anim.mStartTimeCommitted = false;
1488e1b5c2b48a5cf1dd3712dde35b59bc18851b4018ztenghui        anim.mPauseTime = 0;
1489e1b5c2b48a5cf1dd3712dde35b59bc18851b4018ztenghui        anim.mCurrentFraction = 0;
1490e1b5c2b48a5cf1dd3712dde35b59bc18851b4018ztenghui        anim.mDelayStartTime = 0;
149126e9a19900bae56b012425a114685d42dfa2fde1ztenghui
1492a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        PropertyValuesHolder[] oldValues = mValues;
1493a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (oldValues != null) {
1494a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            int numValues = oldValues.length;
1495a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            anim.mValues = new PropertyValuesHolder[numValues];
1496a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            anim.mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues);
1497a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 0; i < numValues; ++i) {
1498d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase                PropertyValuesHolder newValuesHolder = oldValues[i].clone();
1499d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase                anim.mValues[i] = newValuesHolder;
1500d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase                anim.mValuesMap.put(newValuesHolder.getPropertyName(), newValuesHolder);
1501a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1502a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1503a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return anim;
1504a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1505a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1506a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1507a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Implementors of this interface can add themselves as update listeners
1508a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * to an <code>ValueAnimator</code> instance to receive callbacks on every animation
1509a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frame, after the current frame's values have been calculated for that
1510a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <code>ValueAnimator</code>.
1511a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1512a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static interface AnimatorUpdateListener {
1513a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        /**
1514a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * <p>Notifies the occurrence of another frame of the animation.</p>
1515a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         *
1516a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * @param animation The animation which was repeated.
1517a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         */
1518a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        void onAnimationUpdate(ValueAnimator animation);
1519a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1520a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1521599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick
1522599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick    /**
1523599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     * Return the number of animations currently running.
1524599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     *
15259c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown     * Used by StrictMode internally to annotate violations.
15269c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown     * May be called on arbitrary threads!
1527599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     *
1528599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     * @hide
1529599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     */
1530599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick    public static int getCurrentAnimationsCount() {
15319c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        AnimationHandler handler = sAnimationHandler.get();
15329c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        return handler != null ? handler.mAnimations.size() : 0;
1533599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick    }
15348901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy
15358901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy    /**
15368901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy     * Clear all animations on this thread, without canceling or ending them.
15378901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy     * This should be used with caution.
15388901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy     *
15398901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy     * @hide
15408901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy     */
15418901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy    public static void clearAllAnimations() {
15429c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        AnimationHandler handler = sAnimationHandler.get();
15439c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        if (handler != null) {
15449c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown            handler.mAnimations.clear();
15459c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown            handler.mPendingAnimations.clear();
15469c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown            handler.mDelayedAnims.clear();
15479c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        }
15489c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown    }
15499c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown
155018772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy    private static AnimationHandler getOrCreateAnimationHandler() {
15519c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        AnimationHandler handler = sAnimationHandler.get();
15529c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        if (handler == null) {
15539c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown            handler = new AnimationHandler();
15549c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown            sAnimationHandler.set(handler);
15559c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        }
15569c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        return handler;
15578901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy    }
1558e9140a72b1059574046a624b471b2c3a35806496Chet Haase
1559e9140a72b1059574046a624b471b2c3a35806496Chet Haase    @Override
1560e9140a72b1059574046a624b471b2c3a35806496Chet Haase    public String toString() {
1561e9140a72b1059574046a624b471b2c3a35806496Chet Haase        String returnVal = "ValueAnimator@" + Integer.toHexString(hashCode());
1562e9140a72b1059574046a624b471b2c3a35806496Chet Haase        if (mValues != null) {
1563e9140a72b1059574046a624b471b2c3a35806496Chet Haase            for (int i = 0; i < mValues.length; ++i) {
1564e9140a72b1059574046a624b471b2c3a35806496Chet Haase                returnVal += "\n    " + mValues[i].toString();
1565e9140a72b1059574046a624b471b2c3a35806496Chet Haase            }
1566e9140a72b1059574046a624b471b2c3a35806496Chet Haase        }
1567e9140a72b1059574046a624b471b2c3a35806496Chet Haase        return returnVal;
1568e9140a72b1059574046a624b471b2c3a35806496Chet Haase    }
1569d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck
1570d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck    /**
1571d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <p>Whether or not the ValueAnimator is allowed to run asynchronously off of
1572d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * the UI thread. This is a hint that informs the ValueAnimator that it is
1573d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * OK to run the animation off-thread, however ValueAnimator may decide
1574d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * that it must run the animation on the UI thread anyway. For example if there
1575d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * is an {@link AnimatorUpdateListener} the animation will run on the UI thread,
1576d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * regardless of the value of this hint.</p>
1577d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *
1578d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <p>Regardless of whether or not the animation runs asynchronously, all
1579d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * listener callbacks will be called on the UI thread.</p>
1580d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *
1581d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <p>To be able to use this hint the following must be true:</p>
1582d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <ol>
1583d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <li>{@link #getAnimatedFraction()} is not needed (it will return undefined values).</li>
1584d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <li>The animator is immutable while {@link #isStarted()} is true. Requests
1585d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    to change values, duration, delay, etc... may be ignored.</li>
1586d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <li>Lifecycle callback events may be asynchronous. Events such as
1587d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    {@link Animator.AnimatorListener#onAnimationEnd(Animator)} or
1588d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    {@link Animator.AnimatorListener#onAnimationRepeat(Animator)} may end up delayed
1589d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    as they must be posted back to the UI thread, and any actions performed
1590d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    by those callbacks (such as starting new animations) will not happen
1591d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    in the same frame.</li>
1592d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <li>State change requests ({@link #cancel()}, {@link #end()}, {@link #reverse()}, etc...)
1593d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    may be asynchronous. It is guaranteed that all state changes that are
1594d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    performed on the UI thread in the same frame will be applied as a single
1595d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    atomic update, however that frame may be the current frame,
1596d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    the next frame, or some future frame. This will also impact the observed
1597d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    state of the Animator. For example, {@link #isStarted()} may still return true
1598d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    after a call to {@link #end()}. Using the lifecycle callbacks is preferred over
1599d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    queries to {@link #isStarted()}, {@link #isRunning()}, and {@link #isPaused()}
1600d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    for this reason.</li>
1601d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * </ol>
1602d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * @hide
1603d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     */
1604c01bd1167a1b08d59557f214ddc48cf24d3b8d0aJohn Reck    @Override
1605d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck    public void setAllowRunningAsynchronously(boolean mayRunAsync) {
1606d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck        // It is up to subclasses to support this, if they can.
1607d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck    }
1608599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick}
1609