ValueAnimator.java revision 5a5afe010d2f8fb7e8f00858b8a305b4745c0469
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
19d422dc358f0100106dc07d7b903201eb9b043b11Yigit Boyarimport android.content.res.ConfigurationBoundResourceCache;
20a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.os.Looper;
2118772ea228f3d292629c4f0b5f6392d047e0530dRomain Guyimport android.os.Trace;
222970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haaseimport android.util.AndroidRuntimeException;
2396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brownimport android.view.Choreographer;
24a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.view.animation.AccelerateDecelerateInterpolator;
25a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.view.animation.AnimationUtils;
2627c1d4debb3848f5accd5673fffeeacad3e61648Chet Haaseimport android.view.animation.LinearInterpolator;
27a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
28a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport java.util.ArrayList;
29a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport java.util.HashMap;
30a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
31a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase/**
32a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This class provides a simple timing engine for running animations
33a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * which calculate animated values and set them on target objects.
34a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *
35a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>There is a single timing pulse that all animations use. It runs in a
36a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * custom handler to ensure that property changes happen on the UI thread.</p>
37a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *
38a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>By default, ValueAnimator uses non-linear time interpolation, via the
39a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link AccelerateDecelerateInterpolator} class, which accelerates into and decelerates
40a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * out of an animation. This behavior can be changed by calling
41e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase * {@link ValueAnimator#setInterpolator(TimeInterpolator)}.</p>
423aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez *
433aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <div class="special reference">
443aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <h3>Developer Guides</h3>
453aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <p>For more information about animating with {@code ValueAnimator}, read the
463aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <a href="{@docRoot}guide/topics/graphics/prop-animation.html#value-animator">Property
473aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * Animation</a> developer guide.</p>
483aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * </div>
49a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */
5018772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy@SuppressWarnings("unchecked")
512794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haasepublic class ValueAnimator extends Animator {
52a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
53a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
54a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Internal constants
55a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
56d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase    private static float sDurationScale = 1.0f;
57a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
58a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
59a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Values used with internal variable mPlayingState to indicate the current state of an
60a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation.
61a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
62051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    static final int STOPPED    = 0; // Not yet playing
63051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    static final int RUNNING    = 1; // Playing normally
64051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    static final int SEEKED     = 2; // Seeked to some time value
65a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
66a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
67a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Internal variables
68a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * NOTE: This object implements the clone() method, making a deep copy of any referenced
69a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * objects. As other non-trivial fields are added to this class, make sure to add logic
70a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * to clone() to make deep copies of them.
71a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
72a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
73a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The first time that the animation's animateFrame() method is called. This time is used to
74a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // determine elapsed time (and therefore the elapsed fraction) in subsequent calls
75a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // to animateFrame()
76051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    long mStartTime;
77a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
78a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
79a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Set when setCurrentPlayTime() is called. If negative, animation is not currently seeked
80a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * to a value.
81a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
820d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase    float mSeekFraction = -1;
83a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
848aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    /**
858aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * Set on the next frame after pause() is called, used to calculate a new startTime
868aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * or delayStartTime which allows the animator to continue from the point at which
878aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * it was paused. If negative, has not yet been set.
888aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     */
898aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    private long mPauseTime;
908aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
918aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    /**
928aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * Set when an animator is resumed. This triggers logic in the next frame which
938aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * actually resumes the animator.
948aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     */
958aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    private boolean mResumed = false;
968aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
978aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
98a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The static sAnimationHandler processes the internal timing loop on which all animations
99a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // are based
100be19e030a14c8e398e8af97fa898ea80187704dfChet Haase    /**
101be19e030a14c8e398e8af97fa898ea80187704dfChet Haase     * @hide
102be19e030a14c8e398e8af97fa898ea80187704dfChet Haase     */
103be19e030a14c8e398e8af97fa898ea80187704dfChet Haase    protected static ThreadLocal<AnimationHandler> sAnimationHandler =
104e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase            new ThreadLocal<AnimationHandler>();
105e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase
106a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The time interpolator to be used if none is set on the animation
107e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    private static final TimeInterpolator sDefaultInterpolator =
108e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase            new AccelerateDecelerateInterpolator();
109a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
110a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
111a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Used to indicate whether the animation is currently playing in reverse. This causes the
112a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * elapsed fraction to be inverted to calculate the appropriate values.
113a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
114a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private boolean mPlayingBackwards = false;
115a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
116a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
117f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * Flag to indicate whether this animator is playing in reverse mode, specifically
118f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * by being started or interrupted by a call to reverse(). This flag is different than
119f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * mPlayingBackwards, which indicates merely whether the current iteration of the
120f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * animator is playing in reverse. It is used in corner cases to determine proper end
121f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * behavior.
122f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     */
123f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase    private boolean mReversing;
124f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase
125f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase    /**
126a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This variable tracks the current iteration that is playing. When mCurrentIteration exceeds the
127a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * repeatCount (if repeatCount!=INFINITE), the animation ends
128a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
129a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private int mCurrentIteration = 0;
130a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
131a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
132a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     * Tracks current elapsed/eased fraction, for querying in getAnimatedFraction().
133a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     */
134a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    private float mCurrentFraction = 0f;
135a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase
136a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    /**
137a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Tracks whether a startDelay'd animation has begun playing through the startDelay.
138a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
139a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private boolean mStartedDelay = false;
140a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
141a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
142a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Tracks the time at which the animation began playing through its startDelay. This is
143a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * different from the mStartTime variable, which is used to track when the animation became
144a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * active (which is when the startDelay expired and the animation was added to the active
145a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animations list).
146a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
147a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private long mDelayStartTime;
148a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
149a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
150a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Flag that represents the current state of the animation. Used to figure out when to start
151a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * an animation (if state == STOPPED). Also used to end an animation that
152a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * has been cancel()'d or end()'d since the last animation frame. Possible values are
153e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase     * STOPPED, RUNNING, SEEKED.
154a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
155051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    int mPlayingState = STOPPED;
156a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
157a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
158b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * Additional playing state to indicate whether an animator has been start()'d. There is
159b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * some lag between a call to start() and the first animation frame. We should still note
160b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * that the animation has been started, even if it's first animation frame has not yet
161b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * happened, and reflect that state in isRunning().
162b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * Note that delayed animations are different: they are not started until their first
163b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * animation frame, which occurs after their delay elapses.
164b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     */
1658b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    private boolean mRunning = false;
1668b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase
1678b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    /**
1688b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     * Additional playing state to indicate whether an animator has been start()'d, whether or
1698b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     * not there is a nonzero startDelay.
1708b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     */
171b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase    private boolean mStarted = false;
172b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase
173b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase    /**
1748aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * Tracks whether we've notified listeners of the onAnimationStart() event. This can be
17517cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase     * complex to keep track of since we notify listeners at different times depending on
17617cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase     * startDelay and whether start() was called before end().
17717cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase     */
17817cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase    private boolean mStartListenersCalled = false;
17917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase
18017cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase    /**
181a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Flag that denotes whether the animation is set up and ready to go. Used to
182a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * set up animation that has not yet been started.
183a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
184a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    boolean mInitialized = false;
185a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
186a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    //
187a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // Backing variables
188a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    //
189a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
190a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // How long the animation should last in ms
191c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase    private long mDuration = (long)(300 * sDurationScale);
192d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase    private long mUnscaledDuration = 300;
193a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
194a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The amount of time in ms to delay starting the animation after start() is called
195a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private long mStartDelay = 0;
196d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase    private long mUnscaledStartDelay = 0;
197a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
198a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The number of times the animation will repeat. The default is 0, which means the animation
199a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // will play only once
200a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private int mRepeatCount = 0;
201a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
202a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
203a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The type of repetition that will occur when repeatMode is nonzero. RESTART means the
204a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation will start from the beginning on every new cycle. REVERSE means the animation
205a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will reverse directions on each iteration.
206a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
207a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private int mRepeatMode = RESTART;
208a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
209a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
210a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The time interpolator to be used. The elapsed fraction of the animation will be passed
211a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * through this interpolator to calculate the interpolated fraction, which is then used to
212a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * calculate the animated values.
213a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
214e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    private TimeInterpolator mInterpolator = sDefaultInterpolator;
215a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
216a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
217a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The set of listeners to be sent events through the life of an animation.
218a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
219d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck    ArrayList<AnimatorUpdateListener> mUpdateListeners = null;
220a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
221a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
222a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The property/value sets being animated.
223a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
224a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    PropertyValuesHolder[] mValues;
225a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
226a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
227a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * A hashmap of the PropertyValuesHolder objects. This map is used to lookup animated values
228a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * by property name during calls to getAnimatedValue(String).
229a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
230a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    HashMap<String, PropertyValuesHolder> mValuesMap;
231a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
232a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
233a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Public constants
234a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
235a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
236a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
237a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * When the animation reaches the end and <code>repeatCount</code> is INFINITE
238a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * or a positive value, the animation restarts from the beginning.
239a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
240a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static final int RESTART = 1;
241a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
242a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * When the animation reaches the end and <code>repeatCount</code> is INFINITE
243a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * or a positive value, the animation reverses direction on every iteration.
244a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
245a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static final int REVERSE = 2;
246a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
247a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This value used used with the {@link #setRepeatCount(int)} property to repeat
248a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the animation indefinitely.
249a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
250a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static final int INFINITE = -1;
251a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
252c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase
253c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase    /**
254c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase     * @hide
255c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase     */
256c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase    public static void setDurationScale(float durationScale) {
257c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase        sDurationScale = durationScale;
258c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase    }
259c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase
260a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
261ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown     * @hide
262ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown     */
263ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown    public static float getDurationScale() {
264ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown        return sDurationScale;
265ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown    }
266ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown
267ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown    /**
268a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Creates a new ValueAnimator object. This default constructor is primarily for
2692794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * use internally; the factory methods which take parameters are more generally
270a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * useful.
271a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
272a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public ValueAnimator() {
273a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
274a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
275a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
2762794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between int values. A single
2772794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
2782794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
2792794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
2802794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
2812794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
282a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
2832794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
2842794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
28541f041d9986f8a5d45b6cb0b86e881c81a412168Chet Haase     */
2862794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofInt(int... values) {
2872794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
2882794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setIntValues(values);
2892794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
2902794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
2912794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
2922794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
2931ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * Constructs and returns a ValueAnimator that animates between color values. A single
2941ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * value implies that that value is the one being animated to. However, this is not typically
2951ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * useful in a ValueAnimator object because there is no way for the object to determine the
2961ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * starting value for the animation (unlike ObjectAnimator, which can derive that value
2971ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * from the target object and property being animated). Therefore, there should typically
2981ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * be two or more values.
2991ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     *
3001ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * @param values A set of values that the animation will animate between over time.
3011ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * @return A ValueAnimator object that is set up to animate between the given values.
3021ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     */
3031ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount    public static ValueAnimator ofArgb(int... values) {
3041ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount        ValueAnimator anim = new ValueAnimator();
3051ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount        anim.setIntValues(values);
3061ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount        anim.setEvaluator(ArgbEvaluator.getInstance());
3071ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount        return anim;
3081ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount    }
3091ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount
3101ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount    /**
3112794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between float values. A single
3122794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
3132794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
3142794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
3152794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
3162794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
3172794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3182794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
3192794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
3202794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
3212794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofFloat(float... values) {
3222794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
3232794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setFloatValues(values);
3242794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
3252794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
3262794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
3272794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
3282794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between the values
3292794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * specified in the PropertyValuesHolder objects.
3302794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3312794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of PropertyValuesHolder objects whose values will be animated
3322794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * between over time.
3332794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
3342794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
3352794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofPropertyValuesHolder(PropertyValuesHolder... values) {
3362794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
3372794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setValues(values);
3382794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
3392794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
3402794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
3412794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between Object values. A single
3422794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
3432794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
3442794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
3452794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
3462794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
3472794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3482794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>Since ValueAnimator does not know how to animate between arbitrary Objects, this
3492794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * factory method also takes a TypeEvaluator object that the ValueAnimator will use
3502794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * to perform that interpolation.
3512794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3522794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param evaluator A TypeEvaluator that will be called on each animation frame to
3532794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * provide the ncessry interpolation between the Object values to derive the animated
3542794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value.
3552794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
3562794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
3572794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
3582794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofObject(TypeEvaluator evaluator, Object... values) {
3592794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
3602794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setObjectValues(values);
3612794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setEvaluator(evaluator);
3622794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
3632794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
3642794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
3652794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
3662794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Sets int values that will be animated between. A single
3672794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
3682794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
3692794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
3702794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
3712794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
3722794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3732794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>If there are already multiple sets of values defined for this ValueAnimator via more
3742794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * than one PropertyValuesHolder object, this method will set the values for the first
3752794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * of those objects.</p>
3762794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3772794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
3782794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
3792794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public void setIntValues(int... values) {
3802794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (values == null || values.length == 0) {
3812794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            return;
3822794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
3832794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (mValues == null || mValues.length == 0) {
38418772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy            setValues(PropertyValuesHolder.ofInt("", values));
3852794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        } else {
3862794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            PropertyValuesHolder valuesHolder = mValues[0];
3872794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            valuesHolder.setIntValues(values);
3882794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
3892794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        // New property/values/target should cause re-initialization prior to starting
3902794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        mInitialized = false;
3912794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
3922794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
3932794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
3942794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Sets float values that will be animated between. A single
3952794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
3962794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
3972794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
3982794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
3992794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
4002794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4012794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>If there are already multiple sets of values defined for this ValueAnimator via more
4022794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * than one PropertyValuesHolder object, this method will set the values for the first
4032794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * of those objects.</p>
4042794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4052794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
4062794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
4072794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public void setFloatValues(float... values) {
4082794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (values == null || values.length == 0) {
4092794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            return;
41041f041d9986f8a5d45b6cb0b86e881c81a412168Chet Haase        }
4112794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (mValues == null || mValues.length == 0) {
41218772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy            setValues(PropertyValuesHolder.ofFloat("", values));
4132794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        } else {
4142794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            PropertyValuesHolder valuesHolder = mValues[0];
4152794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            valuesHolder.setFloatValues(values);
4162794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
4172794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        // New property/values/target should cause re-initialization prior to starting
4182794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        mInitialized = false;
4192794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
4202794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
4212794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
4222794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Sets the values to animate between for this animation. A single
4232794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
4242794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
4252794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
4262794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
4272794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
4282794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4292794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>If there are already multiple sets of values defined for this ValueAnimator via more
4302794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * than one PropertyValuesHolder object, this method will set the values for the first
4312794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * of those objects.</p>
4322794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4332794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>There should be a TypeEvaluator set on the ValueAnimator that knows how to interpolate
4342794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * between these value objects. ValueAnimator only knows how to interpolate between the
4352794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * primitive types specified in the other setValues() methods.</p>
4362794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4372794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values The set of values to animate between.
4382794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
4392794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public void setObjectValues(Object... values) {
4402794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (values == null || values.length == 0) {
4412794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            return;
4422794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
4432794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (mValues == null || mValues.length == 0) {
44418772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy            setValues(PropertyValuesHolder.ofObject("", null, values));
4452794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        } else {
4462794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            PropertyValuesHolder valuesHolder = mValues[0];
4472794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            valuesHolder.setObjectValues(values);
4482794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
4492794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        // New property/values/target should cause re-initialization prior to starting
4502794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        mInitialized = false;
451a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
452a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
453a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
454a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets the values, per property, being animated between. This function is called internally
455f76a50ce8fdc6aea22cabc77b2977a1a15a79630Ken Wakasa     * by the constructors of ValueAnimator that take a list of values. But a ValueAnimator can
456a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * be constructed without values and this method can be called to set the values manually
457a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * instead.
458a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
459a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param values The set of values, per property, being animated between.
460a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
461a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setValues(PropertyValuesHolder... values) {
462a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        int numValues = values.length;
463a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mValues = values;
464a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues);
465a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        for (int i = 0; i < numValues; ++i) {
46618772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy            PropertyValuesHolder valuesHolder = values[i];
467a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mValuesMap.put(valuesHolder.getPropertyName(), valuesHolder);
468a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
4690e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase        // New property/values/target should cause re-initialization prior to starting
4700e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase        mInitialized = false;
471a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
472a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
473a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
474a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Returns the values that this ValueAnimator animates between. These values are stored in
475a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * PropertyValuesHolder objects, even if the ValueAnimator was created with a simple list
476a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of value objects instead.
477a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
478a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return PropertyValuesHolder[] An array of PropertyValuesHolder objects which hold the
479a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * values, per property, that define the animation.
480a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
481a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public PropertyValuesHolder[] getValues() {
482a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mValues;
483a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
484a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
485a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
486a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This function is called immediately before processing the first animation
487a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frame of an animation. If there is a nonzero <code>startDelay</code>, the
488a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function is called after that delay ends.
489a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * It takes care of the final initialization steps for the
490a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation.
491a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
492a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *  <p>Overrides of this method should call the superclass method to ensure
493a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *  that internal mechanisms for the animation are set up correctly.</p>
494a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
495a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    void initAnimation() {
496a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (!mInitialized) {
497a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            int numValues = mValues.length;
498a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 0; i < numValues; ++i) {
499a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mValues[i].init();
500a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
501a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mInitialized = true;
502a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
503a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
504a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
505a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
506a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
5072794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Sets the length of the animation. The default duration is 300 milliseconds.
508a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
50927c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase     * @param duration The length of the animation, in milliseconds. This value cannot
51027c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase     * be negative.
5112794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return ValueAnimator The object called with setDuration(). This return
5122794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value makes it easier to compose statements together that construct and then set the
5132794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * duration, as in <code>ValueAnimator.ofInt(0, 10).setDuration(500).start()</code>.
514a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
5152794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public ValueAnimator setDuration(long duration) {
51627c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase        if (duration < 0) {
51727c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase            throw new IllegalArgumentException("Animators cannot have negative duration: " +
51827c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase                    duration);
51927c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase        }
520d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase        mUnscaledDuration = duration;
5217a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown        updateScaledDuration();
5222794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return this;
523a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
524a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
5257a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown    private void updateScaledDuration() {
5267a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown        mDuration = (long)(mUnscaledDuration * sDurationScale);
5277a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown    }
5287a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown
529a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
5302794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Gets the length of the animation. The default duration is 300 milliseconds.
531a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
532a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return The length of the animation, in milliseconds.
533a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
534a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public long getDuration() {
535d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase        return mUnscaledDuration;
536a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
537a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
538a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
539a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets the position of the animation to the specified point in time. This time should
540a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * be between 0 and the total duration of the animation, including any repetition. If
541a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the animation has not yet been started, then it will not advance forward after it is
542a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * set to this time; it will simply set the time to this value and perform any appropriate
543a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * actions based on that time. If the animation is already running, then setCurrentPlayTime()
544a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will set the current playing time to this value and continue playing from that point.
545a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
546a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param playTime The time, in milliseconds, to which the animation is advanced or rewound.
547a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
548a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setCurrentPlayTime(long playTime) {
5495a25e5bae223bbee56dab75e36d1d947c8c3cb11Chet Haase        float fraction = mUnscaledDuration > 0 ? (float) playTime / mUnscaledDuration : 1;
5500d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase        setCurrentFraction(fraction);
5510d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase    }
5520d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase
5530d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase    /**
5540d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * Sets the position of the animation to the specified fraction. This fraction should
5550d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * be between 0 and the total fraction of the animation, including any repetition. That is,
5560d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * a fraction of 0 will position the animation at the beginning, a value of 1 at the end,
557f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * and a value of 2 at the end of a reversing animator that repeats once. If
5580d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * the animation has not yet been started, then it will not advance forward after it is
5590d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * set to this fraction; it will simply set the fraction to this value and perform any
5600d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * appropriate actions based on that fraction. If the animation is already running, then
5610d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * setCurrentFraction() will set the current fraction to this value and continue
5625a5afe010d2f8fb7e8f00858b8a305b4745c0469Eino-Ville Talvala     * playing from that point. {@link Animator.AnimatorListener} events are not called
563f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * due to changing the fraction; those events are only processed while the animation
564f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * is running.
5650d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     *
566f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * @param fraction The fraction to which the animation is advanced or rewound. Values
567f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * outside the range of 0 to the maximum fraction for the animator will be clamped to
568f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * the correct range.
5690d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     */
5700d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase    public void setCurrentFraction(float fraction) {
571a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        initAnimation();
572f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        if (fraction < 0) {
573f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            fraction = 0;
574f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        }
575f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        int iteration = (int) fraction;
576f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        if (fraction == 1) {
577f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            iteration -= 1;
578f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        } else if (fraction > 1) {
579f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            if (iteration < (mRepeatCount + 1) || mRepeatCount == INFINITE) {
580f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                if (mRepeatMode == REVERSE) {
581f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                    mPlayingBackwards = (iteration % 2) != 0;
582f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                }
583f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                fraction = fraction % 1f;
584f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            } else {
585f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                fraction = 1;
586f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                iteration -= 1;
587f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            }
588f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        } else {
589f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            mPlayingBackwards = mReversing;
590f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        }
591f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        mCurrentIteration = iteration;
592f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        long seekTime = (long) (mDuration * fraction);
593f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        long currentTime = AnimationUtils.currentAnimationTimeMillis();
594f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        mStartTime = currentTime - seekTime;
595a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mPlayingState != RUNNING) {
5960d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase            mSeekFraction = fraction;
597a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mPlayingState = SEEKED;
598a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
599f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        if (mPlayingBackwards) {
600f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            fraction = 1f - fraction;
601f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        }
6020d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase        animateValue(fraction);
603a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
604a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
605a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
606a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Gets the current position of the animation in time, which is equal to the current
607a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * time minus the time that the animation started. An animation that is not yet started will
608a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * return a value of zero.
609a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
610a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return The current position in time of the animation.
611a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
612a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public long getCurrentPlayTime() {
613a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (!mInitialized || mPlayingState == STOPPED) {
614a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return 0;
615a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
616a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return AnimationUtils.currentAnimationTimeMillis() - mStartTime;
617a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
618a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
619a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
620a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This custom, static handler handles the timing pulse that is shared by
621a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * all active animations. This approach ensures that the setting of animation
622a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * values will happen on the UI thread and that all animations will share
623a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the same times for calculating their values, which makes synchronizing
624a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animations possible.
625a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
62620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     * The handler uses the Choreographer for executing periodic callbacks.
627be19e030a14c8e398e8af97fa898ea80187704dfChet Haase     *
628be19e030a14c8e398e8af97fa898ea80187704dfChet Haase     * @hide
629a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
63018772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy    @SuppressWarnings("unchecked")
631be19e030a14c8e398e8af97fa898ea80187704dfChet Haase    protected static class AnimationHandler implements Runnable {
6329c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        // The per-thread list of all active animations
633be19e030a14c8e398e8af97fa898ea80187704dfChet Haase        /** @hide */
634be19e030a14c8e398e8af97fa898ea80187704dfChet Haase        protected final ArrayList<ValueAnimator> mAnimations = new ArrayList<ValueAnimator>();
6359c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown
6362936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase        // Used in doAnimationFrame() to avoid concurrent modifications of mAnimations
6372936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase        private final ArrayList<ValueAnimator> mTmpAnimations = new ArrayList<ValueAnimator>();
6382936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase
6399c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        // The per-thread set of animations to be started on the next animation frame
640be19e030a14c8e398e8af97fa898ea80187704dfChet Haase        /** @hide */
641be19e030a14c8e398e8af97fa898ea80187704dfChet Haase        protected final ArrayList<ValueAnimator> mPendingAnimations = new ArrayList<ValueAnimator>();
6429c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown
6439c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        /**
6449c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown         * Internal per-thread collections used to avoid set collisions as animations start and end
6459c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown         * while being processed.
646be19e030a14c8e398e8af97fa898ea80187704dfChet Haase         * @hide
6479c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown         */
648be19e030a14c8e398e8af97fa898ea80187704dfChet Haase        protected final ArrayList<ValueAnimator> mDelayedAnims = new ArrayList<ValueAnimator>();
6499c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        private final ArrayList<ValueAnimator> mEndingAnims = new ArrayList<ValueAnimator>();
6509c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        private final ArrayList<ValueAnimator> mReadyAnims = new ArrayList<ValueAnimator>();
6519c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown
65296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        private final Choreographer mChoreographer;
6534a06c8008b2edd6677f9a411af79b0a4971b87feJeff Brown        private boolean mAnimationScheduled;
65496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown
65596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        private AnimationHandler() {
65696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            mChoreographer = Choreographer.getInstance();
65796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        }
65896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown
659a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        /**
66020c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown         * Start animating on the next frame.
661a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         */
66220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        public void start() {
66320c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            scheduleAnimation();
66496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        }
665a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
66620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        private void doAnimationFrame(long frameTime) {
66796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // mPendingAnimations holds any animations that have requested to be started
66896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // We're going to clear mPendingAnimations, but starting animation may
66996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // cause more to be added to the pending list (for example, if one animation
67096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // starting triggers another starting). So we loop until mPendingAnimations
67196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // is empty.
67296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            while (mPendingAnimations.size() > 0) {
67396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                ArrayList<ValueAnimator> pendingCopy =
67496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                        (ArrayList<ValueAnimator>) mPendingAnimations.clone();
67596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                mPendingAnimations.clear();
67696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                int count = pendingCopy.size();
67796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                for (int i = 0; i < count; ++i) {
67896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    ValueAnimator anim = pendingCopy.get(i);
67996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    // If the animation has a startDelay, place it on the delayed list
68096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    if (anim.mStartDelay == 0) {
68196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                        anim.startAnimation(this);
68296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    } else {
68396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                        mDelayedAnims.add(anim);
684a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    }
68596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                }
68696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            }
687c6ffab32415a58bbb010dcd115684f9dbc249710Chet Haase            // Next, process animations currently sitting on the delayed queue, adding
68896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // them to the active animations if they are ready
68996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            int numDelayedAnims = mDelayedAnims.size();
69096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            for (int i = 0; i < numDelayedAnims; ++i) {
69196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                ValueAnimator anim = mDelayedAnims.get(i);
69220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown                if (anim.delayedAnimationFrame(frameTime)) {
69396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    mReadyAnims.add(anim);
69496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                }
69596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            }
69696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            int numReadyAnims = mReadyAnims.size();
69796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            if (numReadyAnims > 0) {
69896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                for (int i = 0; i < numReadyAnims; ++i) {
69996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    ValueAnimator anim = mReadyAnims.get(i);
70096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    anim.startAnimation(this);
70196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    anim.mRunning = true;
70296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    mDelayedAnims.remove(anim);
70396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                }
70496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                mReadyAnims.clear();
70596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            }
70696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown
70796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // Now process all active animations. The return value from animationFrame()
70896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // tells the handler whether it should now be ended
70996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            int numAnims = mAnimations.size();
7102936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase            for (int i = 0; i < numAnims; ++i) {
7112936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase                mTmpAnimations.add(mAnimations.get(i));
7122936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase            }
7132936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase            for (int i = 0; i < numAnims; ++i) {
7142936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase                ValueAnimator anim = mTmpAnimations.get(i);
7152936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase                if (mAnimations.contains(anim) && anim.doAnimationFrame(frameTime)) {
71696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    mEndingAnims.add(anim);
71796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                }
71896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            }
7192936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase            mTmpAnimations.clear();
72096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            if (mEndingAnims.size() > 0) {
7212936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase                for (int i = 0; i < mEndingAnims.size(); ++i) {
72296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    mEndingAnims.get(i).endAnimation(this);
72396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                }
72496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                mEndingAnims.clear();
72596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            }
72696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown
72796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // If there are still active or delayed animations, schedule a future call to
72896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // onAnimate to process the next frame of the animations.
72920c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            if (!mAnimations.isEmpty() || !mDelayedAnims.isEmpty()) {
73020c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown                scheduleAnimation();
731a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
732a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
73396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown
7344a06c8008b2edd6677f9a411af79b0a4971b87feJeff Brown        // Called by the Choreographer.
73596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        @Override
7364a06c8008b2edd6677f9a411af79b0a4971b87feJeff Brown        public void run() {
7374a06c8008b2edd6677f9a411af79b0a4971b87feJeff Brown            mAnimationScheduled = false;
73820c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            doAnimationFrame(mChoreographer.getFrameTime());
73920c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        }
74020c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown
74120c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        private void scheduleAnimation() {
74220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            if (!mAnimationScheduled) {
74320c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown                mChoreographer.postCallback(Choreographer.CALLBACK_ANIMATION, this, null);
74420c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown                mAnimationScheduled = true;
74520c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            }
74696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        }
747a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
748a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
749a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
750a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, to delay starting the animation after
751a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link #start()} is called.
752a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
753a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return the number of milliseconds to delay running the animation
754a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
755a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public long getStartDelay() {
756d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase        return mUnscaledStartDelay;
757a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
758a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
759a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
760a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, to delay starting the animation after
761a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link #start()} is called.
762a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
763a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param startDelay The amount of the delay, in milliseconds
764a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
765a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setStartDelay(long startDelay) {
766d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase        this.mStartDelay = (long)(startDelay * sDurationScale);
767d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase        mUnscaledStartDelay = startDelay;
768a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
769a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
770a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
771a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, between each frame of the animation. This is a
772a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * requested time that the animation will attempt to honor, but the actual delay between
773a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frames may be different, depending on system load and capabilities. This is a static
774a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function because the same delay will be applied to all animations, since they are all
775a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * run off of a single timing loop.
776a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
77796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     * The frame delay may be ignored when the animation system uses an external timing
77896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     * source, such as the display refresh rate (vsync), to govern animations.
77996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     *
780a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return the requested time between frames, in milliseconds
781a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
782a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static long getFrameDelay() {
78396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        return Choreographer.getFrameDelay();
784a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
785a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
786a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
787a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, between each frame of the animation. This is a
788a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * requested time that the animation will attempt to honor, but the actual delay between
789a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frames may be different, depending on system load and capabilities. This is a static
790a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function because the same delay will be applied to all animations, since they are all
791a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * run off of a single timing loop.
792a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
79396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     * The frame delay may be ignored when the animation system uses an external timing
79496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     * source, such as the display refresh rate (vsync), to govern animations.
79596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     *
796a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param frameDelay the requested time between frames, in milliseconds
797a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
798a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static void setFrameDelay(long frameDelay) {
79996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        Choreographer.setFrameDelay(frameDelay);
800a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
801a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
802a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
803a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The most recent value calculated by this <code>ValueAnimator</code> when there is just one
804a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * property being animated. This value is only sensible while the animation is running. The main
805a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * purpose for this read-only property is to retrieve the value from the <code>ValueAnimator</code>
806a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * during a call to {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which
807a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is called during each animation frame, immediately after the value is calculated.
808a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
809a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return animatedValue The value most recently calculated by this <code>ValueAnimator</code> for
810a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the single property being animated. If there are several properties being animated
811a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * (specified by several PropertyValuesHolder objects in the constructor), this function
812a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * returns the animated value for the first of those objects.
813a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
814a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public Object getAnimatedValue() {
815a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mValues != null && mValues.length > 0) {
816a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return mValues[0].getAnimatedValue();
817a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
818a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        // Shouldn't get here; should always have values unless ValueAnimator was set up wrong
819a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return null;
820a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
821a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
822a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
823a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The most recent value calculated by this <code>ValueAnimator</code> for <code>propertyName</code>.
824a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The main purpose for this read-only property is to retrieve the value from the
825a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <code>ValueAnimator</code> during a call to
826a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which
827a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is called during each animation frame, immediately after the value is calculated.
828a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
829a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return animatedValue The value most recently calculated for the named property
830a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * by this <code>ValueAnimator</code>.
831a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
832a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public Object getAnimatedValue(String propertyName) {
833a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        PropertyValuesHolder valuesHolder = mValuesMap.get(propertyName);
834a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (valuesHolder != null) {
835a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return valuesHolder.getAnimatedValue();
836a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        } else {
837a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // At least avoid crashing if called with bogus propertyName
838a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return null;
839a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
840a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
841a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
842a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
843a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets how many times the animation should be repeated. If the repeat
844a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * count is 0, the animation is never repeated. If the repeat count is
845a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * greater than 0 or {@link #INFINITE}, the repeat mode will be taken
846a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * into account. The repeat count is 0 by default.
847a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
848a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param value the number of times the animation should be repeated
849a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
850a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setRepeatCount(int value) {
851a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mRepeatCount = value;
852a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
853a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
854a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Defines how many times the animation should repeat. The default value
855a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is 0.
856a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
857a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return the number of times the animation should repeat, or {@link #INFINITE}
858a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
859a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public int getRepeatCount() {
860a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mRepeatCount;
861a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
862a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
863a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
864a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Defines what this animation should do when it reaches the end. This
865a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * setting is applied only when the repeat count is either greater than
866a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * 0 or {@link #INFINITE}. Defaults to {@link #RESTART}.
867a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
868a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param value {@link #RESTART} or {@link #REVERSE}
869a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
870a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setRepeatMode(int value) {
871a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mRepeatMode = value;
872a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
873a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
874a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
875a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Defines what this animation should do when it reaches the end.
876a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
877a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return either one of {@link #REVERSE} or {@link #RESTART}
878a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
879a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public int getRepeatMode() {
880a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mRepeatMode;
881a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
882a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
883a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
884a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Adds a listener to the set of listeners that are sent update events through the life of
885a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * an animation. This method is called on all listeners for every frame of the animation,
886a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * after the values for the animation have been calculated.
887a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
888a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param listener the listener to be added to the current set of listeners for this animation.
889a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
890a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void addUpdateListener(AnimatorUpdateListener listener) {
891a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners == null) {
892a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mUpdateListeners = new ArrayList<AnimatorUpdateListener>();
893a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
894a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mUpdateListeners.add(listener);
895a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
896a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
897a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
8983060421045d4d9e411797f91bb509824b03e33fbJim Miller     * Removes all listeners from the set listening to frame updates for this animation.
8993060421045d4d9e411797f91bb509824b03e33fbJim Miller     */
9003060421045d4d9e411797f91bb509824b03e33fbJim Miller    public void removeAllUpdateListeners() {
9013060421045d4d9e411797f91bb509824b03e33fbJim Miller        if (mUpdateListeners == null) {
9023060421045d4d9e411797f91bb509824b03e33fbJim Miller            return;
9033060421045d4d9e411797f91bb509824b03e33fbJim Miller        }
9043060421045d4d9e411797f91bb509824b03e33fbJim Miller        mUpdateListeners.clear();
9053060421045d4d9e411797f91bb509824b03e33fbJim Miller        mUpdateListeners = null;
9063060421045d4d9e411797f91bb509824b03e33fbJim Miller    }
9073060421045d4d9e411797f91bb509824b03e33fbJim Miller
9083060421045d4d9e411797f91bb509824b03e33fbJim Miller    /**
909a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Removes a listener from the set listening to frame updates for this animation.
910a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
911a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param listener the listener to be removed from the current set of update listeners
912a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * for this animation.
913a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
914a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void removeUpdateListener(AnimatorUpdateListener listener) {
915a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners == null) {
916a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return;
917a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
918a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mUpdateListeners.remove(listener);
919a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners.size() == 0) {
920a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mUpdateListeners = null;
921a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
922a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
923a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
924a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
925a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
926a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The time interpolator used in calculating the elapsed fraction of this animation. The
927a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * interpolator determines whether the animation runs with linear or non-linear motion,
928a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * such as acceleration and deceleration. The default value is
929a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link android.view.animation.AccelerateDecelerateInterpolator}
930a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
93127c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase     * @param value the interpolator to be used by this animation. A value of <code>null</code>
93227c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase     * will result in linear interpolation.
933a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
934a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
935e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    public void setInterpolator(TimeInterpolator value) {
936a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (value != null) {
937a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mInterpolator = value;
93827c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase        } else {
93927c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase            mInterpolator = new LinearInterpolator();
940a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
941a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
942a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
943a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
944a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Returns the timing interpolator that this ValueAnimator uses.
945a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
946a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return The timing interpolator for this ValueAnimator.
947a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
948430742f09063574271e6c4091de13b9b9e762514Chet Haase    @Override
949e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    public TimeInterpolator getInterpolator() {
950a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mInterpolator;
951a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
952a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
953a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
954a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The type evaluator to be used when calculating the animated values of this animation.
955b2ab04ffb6894f399d5c9ceb15f64eb17b654426Chet Haase     * The system will automatically assign a float or int evaluator based on the type
956a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of <code>startValue</code> and <code>endValue</code> in the constructor. But if these values
957a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * are not one of these primitive types, or if different evaluation is desired (such as is
958a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * necessary with int values that represent colors), a custom evaluator needs to be assigned.
95953ee3316bcb3590ff156b3fd7108903c0817c35dChet Haase     * For example, when running an animation on color values, the {@link ArgbEvaluator}
960a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * should be used to get correct RGB color interpolation.
961a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
962a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>If this ValueAnimator has only one set of values being animated between, this evaluator
963a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will be used for that set. If there are several sets of values being animated, which is
964fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase     * the case if PropertyValuesHolder objects were set on the ValueAnimator, then the evaluator
965a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is assigned just to the first PropertyValuesHolder object.</p>
966a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
967a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param value the evaluator to be used this animation
968a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
969a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setEvaluator(TypeEvaluator value) {
970a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (value != null && mValues != null && mValues.length > 0) {
971a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mValues[0].setEvaluator(value);
972a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
973a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
974a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
97517cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase    private void notifyStartListeners() {
97617cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        if (mListeners != null && !mStartListenersCalled) {
97717cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            ArrayList<AnimatorListener> tmpListeners =
97817cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                    (ArrayList<AnimatorListener>) mListeners.clone();
97917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            int numListeners = tmpListeners.size();
98017cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            for (int i = 0; i < numListeners; ++i) {
98117cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                tmpListeners.get(i).onAnimationStart(this);
98217cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            }
98317cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        }
98417cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        mStartListenersCalled = true;
98517cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase    }
98617cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase
987a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
988a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Start the animation playing. This version of start() takes a boolean flag that indicates
989a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * whether the animation should play in reverse. The flag is usually false, but may be set
9902970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * to true if called from the reverse() method.
9912970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     *
9922970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * <p>The animation started by calling this method will be run on the thread that called
9932970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * this method. This thread should have a Looper on it (a runtime exception will be thrown if
9942970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * this is not the case). Also, if the animation will animate
9952970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * properties of objects in the view hierarchy, then the calling thread should be the UI
9962970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * thread for that view hierarchy.</p>
997a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
998a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param playBackwards Whether the ValueAnimator should start playing in reverse.
999a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1000a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private void start(boolean playBackwards) {
10012970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        if (Looper.myLooper() == null) {
10022970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            throw new AndroidRuntimeException("Animators may only be run on Looper threads");
10033060421045d4d9e411797f91bb509824b03e33fbJim Miller        }
1004f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        mReversing = playBackwards;
10052970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        mPlayingBackwards = playBackwards;
1006f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        if (playBackwards && mSeekFraction != -1) {
1007f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            if (mSeekFraction == 0 && mCurrentIteration == 0) {
1008f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                // special case: reversing from seek-to-0 should act as if not seeked at all
1009f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                mSeekFraction = 0;
1010f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            } else if (mRepeatCount == INFINITE) {
1011f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                mSeekFraction = 1 - (mSeekFraction % 1);
1012f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            } else {
1013f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                mSeekFraction = 1 + mRepeatCount - (mCurrentIteration + mSeekFraction);
1014f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            }
1015f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            mCurrentIteration = (int) mSeekFraction;
1016f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            mSeekFraction = mSeekFraction % 1;
1017f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        }
1018f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        if (mCurrentIteration > 0 && mRepeatMode == REVERSE &&
1019f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                (mCurrentIteration < (mRepeatCount + 1) || mRepeatCount == INFINITE)) {
1020f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            // if we were seeked to some other iteration in a reversing animator,
1021f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            // figure out the correct direction to start playing based on the iteration
1022f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            if (playBackwards) {
1023f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                mPlayingBackwards = (mCurrentIteration % 2) == 0;
1024f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            } else {
1025f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                mPlayingBackwards = (mCurrentIteration % 2) != 0;
1026f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            }
1027f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        }
10280d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase        int prevPlayingState = mPlayingState;
1029add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase        mPlayingState = STOPPED;
10308b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        mStarted = true;
1031add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase        mStartedDelay = false;
10328aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        mPaused = false;
10337a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown        updateScaledDuration(); // in case the scale factor has changed since creation time
10349c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        AnimationHandler animationHandler = getOrCreateAnimationHandler();
10359c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        animationHandler.mPendingAnimations.add(this);
10362970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        if (mStartDelay == 0) {
1037add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase            // This sets the initial value of the animation, prior to actually starting it running
10380d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase            if (prevPlayingState != SEEKED) {
10390d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase                setCurrentPlayTime(0);
10400d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase            }
1041154f14508a11627d5a995b6fe2a14a83d794a6feChet Haase            mPlayingState = STOPPED;
10428b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase            mRunning = true;
104317cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            notifyStartListeners();
1044a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
104520c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        animationHandler.start();
1046a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1047a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1048a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
1049a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void start() {
1050a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        start(false);
1051a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1052a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1053a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
1054a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void cancel() {
10552970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        // Only cancel if the animation is actually running or has been started and is about
10562970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        // to run
10579c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        AnimationHandler handler = getOrCreateAnimationHandler();
10589c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        if (mPlayingState != STOPPED
10599c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown                || handler.mPendingAnimations.contains(this)
10609c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown                || handler.mDelayedAnims.contains(this)) {
1061b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase            // Only notify listeners if the animator has actually started
106217cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            if ((mStarted || mRunning) && mListeners != null) {
106317cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                if (!mRunning) {
106417cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                    // If it's not yet running, then start listeners weren't called. Call them now.
106517cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                    notifyStartListeners();
106617cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                }
10677dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                ArrayList<AnimatorListener> tmpListeners =
10687dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                        (ArrayList<AnimatorListener>) mListeners.clone();
10697dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                for (AnimatorListener listener : tmpListeners) {
10707dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                    listener.onAnimationCancel(this);
10717dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                }
10727dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase            }
10739c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown            endAnimation(handler);
10742970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        }
1075a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1076a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1077a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
1078a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void end() {
10799c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        AnimationHandler handler = getOrCreateAnimationHandler();
10809c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        if (!handler.mAnimations.contains(this) && !handler.mPendingAnimations.contains(this)) {
1081a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // Special case if the animation has not yet started; get it ready for ending
1082a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mStartedDelay = false;
10839c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown            startAnimation(handler);
108417cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            mStarted = true;
1085add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase        } else if (!mInitialized) {
1086add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase            initAnimation();
1087e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase        }
10884dd176864310e1d9519bf6b88918913e9927984fChet Haase        animateValue(mPlayingBackwards ? 0f : 1f);
10899c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        endAnimation(handler);
1090a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1091a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1092a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
10938aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    public void resume() {
10948aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        if (mPaused) {
10958aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            mResumed = true;
10968aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        }
10978aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        super.resume();
10988aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    }
10998aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
11008aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    @Override
11018aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    public void pause() {
11028aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        boolean previouslyPaused = mPaused;
11038aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        super.pause();
11048aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        if (!previouslyPaused && mPaused) {
11058aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            mPauseTime = -1;
11068aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            mResumed = false;
11078aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        }
11088aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    }
11098aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
11108aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    @Override
1111a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public boolean isRunning() {
11128b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        return (mPlayingState == RUNNING || mRunning);
11138b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    }
11148b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase
11158b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    @Override
11168b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    public boolean isStarted() {
11178b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        return mStarted;
1118a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1119a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1120a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1121a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Plays the ValueAnimator in reverse. If the animation is already running,
1122a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * it will stop itself and play backwards from the point reached when reverse was called.
1123a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * If the animation is not currently running, then it will start from the end and
1124a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * play backwards. This behavior is only set for the current animation; future playing
1125a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of the animation will use the default behavior of playing forward.
1126a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
11277bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui    @Override
1128a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void reverse() {
1129a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mPlayingBackwards = !mPlayingBackwards;
1130a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mPlayingState == RUNNING) {
1131a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            long currentTime = AnimationUtils.currentAnimationTimeMillis();
1132a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            long currentPlayTime = currentTime - mStartTime;
1133a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            long timeLeft = mDuration - currentPlayTime;
1134a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mStartTime = currentTime - timeLeft;
1135f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            mReversing = !mReversing;
1136f43fb2a57f152b5760d8792fec26f36d46b23817Chet Haase        } else if (mStarted) {
1137f43fb2a57f152b5760d8792fec26f36d46b23817Chet Haase            end();
1138a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        } else {
1139a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            start(true);
1140a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1141a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1142a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1143a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
11447bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui     * @hide
11457bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui     */
11467bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui    @Override
11477bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui    public boolean canReverse() {
11487bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui        return true;
11497bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui    }
11507bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui
11517bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui    /**
1152a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Called internally to end an animation by removing it from the animations list. Must be
1153a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * called on the UI thread.
1154d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * @hide
1155a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1156d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck    protected void endAnimation(AnimationHandler handler) {
11579c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        handler.mAnimations.remove(this);
11589c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        handler.mPendingAnimations.remove(this);
11599c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        handler.mDelayedAnims.remove(this);
1160a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mPlayingState = STOPPED;
11618aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        mPaused = false;
116217cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        if ((mStarted || mRunning) && mListeners != null) {
116317cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            if (!mRunning) {
116417cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                // If it's not yet running, then start listeners weren't called. Call them now.
116517cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                notifyStartListeners();
116617cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase             }
1167a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            ArrayList<AnimatorListener> tmpListeners =
1168a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    (ArrayList<AnimatorListener>) mListeners.clone();
11697c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            int numListeners = tmpListeners.size();
11707c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            for (int i = 0; i < numListeners; ++i) {
11717c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                tmpListeners.get(i).onAnimationEnd(this);
1172a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1173a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
11748b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        mRunning = false;
1175b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase        mStarted = false;
117617cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        mStartListenersCalled = false;
1177caf46486f54cdc899e383dfc776ec33a81b089a1Chet Haase        mPlayingBackwards = false;
1178f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        mReversing = false;
1179f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        mCurrentIteration = 0;
11809b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase        if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
11819b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase            Trace.asyncTraceEnd(Trace.TRACE_TAG_VIEW, getNameForTrace(),
11829b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase                    System.identityHashCode(this));
11839b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase        }
1184a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1185a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1186a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1187a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Called internally to start an animation by adding it to the active animations list. Must be
1188a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * called on the UI thread.
1189a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
11909c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown    private void startAnimation(AnimationHandler handler) {
11919b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase        if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
11929b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase            Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW, getNameForTrace(),
11939b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase                    System.identityHashCode(this));
11949b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase        }
1195a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        initAnimation();
11969c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        handler.mAnimations.add(this);
1197b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase        if (mStartDelay > 0 && mListeners != null) {
1198b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase            // Listeners were already notified in start() if startDelay is 0; this is
1199b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase            // just for delayed animations
120017cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            notifyStartListeners();
1201a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1202a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1203a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1204a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1205fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase     * Returns the name of this animator for debugging purposes.
1206fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase     */
1207fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase    String getNameForTrace() {
1208fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase        return "animator";
1209fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase    }
1210fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase
1211fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase
1212fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase    /**
1213a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Internal function called to process an animation frame on an animation that is currently
1214a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * sleeping through its <code>startDelay</code> phase. The return value indicates whether it
1215a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * should be woken up and put on the active animations queue.
1216a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1217a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param currentTime The current animation time, used to calculate whether the animation
1218a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * has exceeded its <code>startDelay</code> and should be started.
1219a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return True if the animation's <code>startDelay</code> has been exceeded and the animation
1220a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * should be added to the set of active animations.
1221a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1222a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private boolean delayedAnimationFrame(long currentTime) {
1223a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (!mStartedDelay) {
1224a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mStartedDelay = true;
1225a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mDelayStartTime = currentTime;
12262ed16ac6238227dab2687519268f3683f045e2acGeorge Mount        }
12272ed16ac6238227dab2687519268f3683f045e2acGeorge Mount        if (mPaused) {
12282ed16ac6238227dab2687519268f3683f045e2acGeorge Mount            if (mPauseTime < 0) {
12292ed16ac6238227dab2687519268f3683f045e2acGeorge Mount                mPauseTime = currentTime;
12308aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            }
12312ed16ac6238227dab2687519268f3683f045e2acGeorge Mount            return false;
12322ed16ac6238227dab2687519268f3683f045e2acGeorge Mount        } else if (mResumed) {
12332ed16ac6238227dab2687519268f3683f045e2acGeorge Mount            mResumed = false;
12342ed16ac6238227dab2687519268f3683f045e2acGeorge Mount            if (mPauseTime > 0) {
12352ed16ac6238227dab2687519268f3683f045e2acGeorge Mount                // Offset by the duration that the animation was paused
12362ed16ac6238227dab2687519268f3683f045e2acGeorge Mount                mDelayStartTime += (currentTime - mPauseTime);
1237a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1238a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
12392ed16ac6238227dab2687519268f3683f045e2acGeorge Mount        long deltaTime = currentTime - mDelayStartTime;
12402ed16ac6238227dab2687519268f3683f045e2acGeorge Mount        if (deltaTime > mStartDelay) {
12412ed16ac6238227dab2687519268f3683f045e2acGeorge Mount            // startDelay ended - start the anim and record the
12422ed16ac6238227dab2687519268f3683f045e2acGeorge Mount            // mStartTime appropriately
12432ed16ac6238227dab2687519268f3683f045e2acGeorge Mount            mStartTime = currentTime - (deltaTime - mStartDelay);
12442ed16ac6238227dab2687519268f3683f045e2acGeorge Mount            mPlayingState = RUNNING;
12452ed16ac6238227dab2687519268f3683f045e2acGeorge Mount            return true;
12462ed16ac6238227dab2687519268f3683f045e2acGeorge Mount        }
1247a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return false;
1248a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1249a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1250a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1251a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This internal function processes a single animation frame for a given animation. The
1252a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * currentTime parameter is the timing pulse sent by the handler, used to calculate the
1253a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * elapsed duration, and therefore
1254a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the elapsed fraction, of the animation. The return value indicates whether the animation
1255a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * should be ended (which happens when the elapsed time of the animation exceeds the
1256a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation's duration, including the repeatCount).
1257a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1258a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param currentTime The current time, as tracked by the static timing handler
1259a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return true if the animation's duration, including any repetitions due to
12608aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * <code>repeatCount</code>, has been exceeded and the animation should be ended.
1261a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1262051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    boolean animationFrame(long currentTime) {
1263a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        boolean done = false;
1264a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        switch (mPlayingState) {
1265a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        case RUNNING:
1266a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        case SEEKED:
126770d4ba15b1f0c1133c5aabc86de828b41e482fffChet Haase            float fraction = mDuration > 0 ? (float)(currentTime - mStartTime) / mDuration : 1f;
1268f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            if (mDuration == 0 && mRepeatCount != INFINITE) {
1269f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                // Skip to the end
1270f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                mCurrentIteration = mRepeatCount;
1271f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                if (!mReversing) {
1272f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                    mPlayingBackwards = false;
1273f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                }
1274f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            }
1275a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (fraction >= 1f) {
1276f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                if (mCurrentIteration < mRepeatCount ||
1277f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                        (mRepeatCount == INFINITE && mDuration != 0)) {
1278a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    // Time to repeat
1279a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    if (mListeners != null) {
12807c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                        int numListeners = mListeners.size();
12817c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                        for (int i = 0; i < numListeners; ++i) {
12827c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                            mListeners.get(i).onAnimationRepeat(this);
1283a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                        }
1284a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    }
1285a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    if (mRepeatMode == REVERSE) {
128618772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy                        mPlayingBackwards = !mPlayingBackwards;
1287a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    }
1288f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                    mCurrentIteration += (int) fraction;
1289730666858692ea396f5ad779654b5d86ff90b6caChet Haase                    fraction = fraction % 1f;
1290a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    mStartTime += mDuration;
1291a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                } else {
1292a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    done = true;
1293a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    fraction = Math.min(fraction, 1.0f);
1294a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                }
1295a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1296a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (mPlayingBackwards) {
1297a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                fraction = 1f - fraction;
1298a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1299a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            animateValue(fraction);
1300a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            break;
1301a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1302a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1303a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return done;
1304a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1305a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1306a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
130720c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     * Processes a frame of the animation, adjusting the start time if needed.
130820c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     *
130920c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     * @param frameTime The frame time.
131020c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     * @return true if the animation has ended.
131120c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     */
131220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown    final boolean doAnimationFrame(long frameTime) {
131320c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        if (mPlayingState == STOPPED) {
131420c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            mPlayingState = RUNNING;
13150d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase            if (mSeekFraction < 0) {
131620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown                mStartTime = frameTime;
131720c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            } else {
13180d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase                long seekTime = (long) (mDuration * mSeekFraction);
13190d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase                mStartTime = frameTime - seekTime;
13200d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase                mSeekFraction = -1;
132120c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            }
132220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        }
13238aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        if (mPaused) {
13248aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            if (mPauseTime < 0) {
13258aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                mPauseTime = frameTime;
13268aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            }
13278aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            return false;
13288aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        } else if (mResumed) {
13298aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            mResumed = false;
13308aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            if (mPauseTime > 0) {
13318aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                // Offset by the duration that the animation was paused
13328aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                mStartTime += (frameTime - mPauseTime);
13338aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            }
13348aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        }
133520c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        // The frame time might be before the start time during the first frame of
133620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        // an animation.  The "current time" must always be on or after the start
133720c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        // time to avoid animating frames at negative time intervals.  In practice, this
133820c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        // is very rare and only happens when seeking backwards.
133920c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        final long currentTime = Math.max(frameTime, mStartTime);
134020c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        return animationFrame(currentTime);
134120c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown    }
134220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown
134320c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown    /**
1344a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     * Returns the current animation fraction, which is the elapsed/interpolated fraction used in
1345a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     * the most recent frame update on the animation.
1346a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     *
1347a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     * @return Elapsed/interpolated fraction of the animation.
1348a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     */
1349a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    public float getAnimatedFraction() {
1350a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase        return mCurrentFraction;
1351a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    }
1352a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase
1353a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    /**
1354a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This method is called with the elapsed fraction of the animation during every
1355a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation frame. This function turns the elapsed fraction into an interpolated fraction
1356a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * and then into an animated value (from the evaluator. The function is called mostly during
1357a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation updates, but it is also called when the <code>end()</code>
1358a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function is called, to set the final value on the property.
1359a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1360a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>Overrides of this method must call the superclass to perform the calculation
1361a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of the animated value.</p>
1362a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1363a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param fraction The elapsed fraction of the animation.
1364a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1365a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    void animateValue(float fraction) {
1366a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        fraction = mInterpolator.getInterpolation(fraction);
1367a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase        mCurrentFraction = fraction;
1368a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        int numValues = mValues.length;
1369a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        for (int i = 0; i < numValues; ++i) {
1370a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mValues[i].calculateValue(fraction);
1371a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1372a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners != null) {
1373a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            int numListeners = mUpdateListeners.size();
1374a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 0; i < numListeners; ++i) {
1375a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mUpdateListeners.get(i).onAnimationUpdate(this);
1376a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1377a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1378a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1379a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1380a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
1381a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public ValueAnimator clone() {
1382a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        final ValueAnimator anim = (ValueAnimator) super.clone();
1383a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners != null) {
1384d422dc358f0100106dc07d7b903201eb9b043b11Yigit Boyar            anim.mUpdateListeners = new ArrayList<AnimatorUpdateListener>(mUpdateListeners);
1385a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
13860d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase        anim.mSeekFraction = -1;
1387a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mPlayingBackwards = false;
1388f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        anim.mReversing = false;
1389a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mCurrentIteration = 0;
1390a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mInitialized = false;
1391a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mPlayingState = STOPPED;
1392a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mStartedDelay = false;
1393a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        PropertyValuesHolder[] oldValues = mValues;
1394a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (oldValues != null) {
1395a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            int numValues = oldValues.length;
1396a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            anim.mValues = new PropertyValuesHolder[numValues];
1397a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            anim.mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues);
1398a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 0; i < numValues; ++i) {
1399d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase                PropertyValuesHolder newValuesHolder = oldValues[i].clone();
1400d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase                anim.mValues[i] = newValuesHolder;
1401d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase                anim.mValuesMap.put(newValuesHolder.getPropertyName(), newValuesHolder);
1402a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1403a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1404a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return anim;
1405a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1406a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1407a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1408a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Implementors of this interface can add themselves as update listeners
1409a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * to an <code>ValueAnimator</code> instance to receive callbacks on every animation
1410a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frame, after the current frame's values have been calculated for that
1411a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <code>ValueAnimator</code>.
1412a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1413a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static interface AnimatorUpdateListener {
1414a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        /**
1415a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * <p>Notifies the occurrence of another frame of the animation.</p>
1416a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         *
1417a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * @param animation The animation which was repeated.
1418a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         */
1419a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        void onAnimationUpdate(ValueAnimator animation);
1420a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1421a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1422599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick
1423599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick    /**
1424599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     * Return the number of animations currently running.
1425599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     *
14269c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown     * Used by StrictMode internally to annotate violations.
14279c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown     * May be called on arbitrary threads!
1428599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     *
1429599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     * @hide
1430599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     */
1431599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick    public static int getCurrentAnimationsCount() {
14329c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        AnimationHandler handler = sAnimationHandler.get();
14339c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        return handler != null ? handler.mAnimations.size() : 0;
1434599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick    }
14358901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy
14368901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy    /**
14378901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy     * Clear all animations on this thread, without canceling or ending them.
14388901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy     * This should be used with caution.
14398901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy     *
14408901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy     * @hide
14418901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy     */
14428901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy    public static void clearAllAnimations() {
14439c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        AnimationHandler handler = sAnimationHandler.get();
14449c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        if (handler != null) {
14459c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown            handler.mAnimations.clear();
14469c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown            handler.mPendingAnimations.clear();
14479c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown            handler.mDelayedAnims.clear();
14489c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        }
14499c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown    }
14509c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown
145118772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy    private static AnimationHandler getOrCreateAnimationHandler() {
14529c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        AnimationHandler handler = sAnimationHandler.get();
14539c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        if (handler == null) {
14549c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown            handler = new AnimationHandler();
14559c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown            sAnimationHandler.set(handler);
14569c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        }
14579c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        return handler;
14588901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy    }
1459e9140a72b1059574046a624b471b2c3a35806496Chet Haase
1460e9140a72b1059574046a624b471b2c3a35806496Chet Haase    @Override
1461e9140a72b1059574046a624b471b2c3a35806496Chet Haase    public String toString() {
1462e9140a72b1059574046a624b471b2c3a35806496Chet Haase        String returnVal = "ValueAnimator@" + Integer.toHexString(hashCode());
1463e9140a72b1059574046a624b471b2c3a35806496Chet Haase        if (mValues != null) {
1464e9140a72b1059574046a624b471b2c3a35806496Chet Haase            for (int i = 0; i < mValues.length; ++i) {
1465e9140a72b1059574046a624b471b2c3a35806496Chet Haase                returnVal += "\n    " + mValues[i].toString();
1466e9140a72b1059574046a624b471b2c3a35806496Chet Haase            }
1467e9140a72b1059574046a624b471b2c3a35806496Chet Haase        }
1468e9140a72b1059574046a624b471b2c3a35806496Chet Haase        return returnVal;
1469e9140a72b1059574046a624b471b2c3a35806496Chet Haase    }
1470d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck
1471d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck    /**
1472d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <p>Whether or not the ValueAnimator is allowed to run asynchronously off of
1473d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * the UI thread. This is a hint that informs the ValueAnimator that it is
1474d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * OK to run the animation off-thread, however ValueAnimator may decide
1475d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * that it must run the animation on the UI thread anyway. For example if there
1476d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * is an {@link AnimatorUpdateListener} the animation will run on the UI thread,
1477d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * regardless of the value of this hint.</p>
1478d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *
1479d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <p>Regardless of whether or not the animation runs asynchronously, all
1480d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * listener callbacks will be called on the UI thread.</p>
1481d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *
1482d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <p>To be able to use this hint the following must be true:</p>
1483d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <ol>
1484d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <li>{@link #getAnimatedFraction()} is not needed (it will return undefined values).</li>
1485d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <li>The animator is immutable while {@link #isStarted()} is true. Requests
1486d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    to change values, duration, delay, etc... may be ignored.</li>
1487d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <li>Lifecycle callback events may be asynchronous. Events such as
1488d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    {@link Animator.AnimatorListener#onAnimationEnd(Animator)} or
1489d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    {@link Animator.AnimatorListener#onAnimationRepeat(Animator)} may end up delayed
1490d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    as they must be posted back to the UI thread, and any actions performed
1491d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    by those callbacks (such as starting new animations) will not happen
1492d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    in the same frame.</li>
1493d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <li>State change requests ({@link #cancel()}, {@link #end()}, {@link #reverse()}, etc...)
1494d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    may be asynchronous. It is guaranteed that all state changes that are
1495d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    performed on the UI thread in the same frame will be applied as a single
1496d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    atomic update, however that frame may be the current frame,
1497d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    the next frame, or some future frame. This will also impact the observed
1498d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    state of the Animator. For example, {@link #isStarted()} may still return true
1499d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    after a call to {@link #end()}. Using the lifecycle callbacks is preferred over
1500d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    queries to {@link #isStarted()}, {@link #isRunning()}, and {@link #isPaused()}
1501d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    for this reason.</li>
1502d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * </ol>
1503d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * @hide
1504d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     */
1505c01bd1167a1b08d59557f214ddc48cf24d3b8d0aJohn Reck    @Override
1506d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck    public void setAllowRunningAsynchronously(boolean mayRunAsync) {
1507d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck        // It is up to subclasses to support this, if they can.
1508d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck    }
1509599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick}
1510