ValueAnimator.java revision f43fb2a57f152b5760d8792fec26f36d46b23817
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
19a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.os.Looper;
2018772ea228f3d292629c4f0b5f6392d047e0530dRomain Guyimport android.os.Trace;
212970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haaseimport android.util.AndroidRuntimeException;
2296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brownimport android.view.Choreographer;
23a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.view.animation.AccelerateDecelerateInterpolator;
24a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.view.animation.AnimationUtils;
2527c1d4debb3848f5accd5673fffeeacad3e61648Chet Haaseimport android.view.animation.LinearInterpolator;
26a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
27a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport java.util.ArrayList;
28a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport java.util.HashMap;
29a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
30a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase/**
31a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This class provides a simple timing engine for running animations
32a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * which calculate animated values and set them on target objects.
33a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *
34a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>There is a single timing pulse that all animations use. It runs in a
35a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * custom handler to ensure that property changes happen on the UI thread.</p>
36a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *
37a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>By default, ValueAnimator uses non-linear time interpolation, via the
38a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link AccelerateDecelerateInterpolator} class, which accelerates into and decelerates
39a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * out of an animation. This behavior can be changed by calling
40e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase * {@link ValueAnimator#setInterpolator(TimeInterpolator)}.</p>
413aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez *
423aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <div class="special reference">
433aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <h3>Developer Guides</h3>
443aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <p>For more information about animating with {@code ValueAnimator}, read the
453aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <a href="{@docRoot}guide/topics/graphics/prop-animation.html#value-animator">Property
463aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * Animation</a> developer guide.</p>
473aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * </div>
48a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */
4918772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy@SuppressWarnings("unchecked")
502794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haasepublic class ValueAnimator extends Animator {
51a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
52a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
53a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Internal constants
54a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
55d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase    private static float sDurationScale = 1.0f;
56a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
57a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
58a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Values used with internal variable mPlayingState to indicate the current state of an
59a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation.
60a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
61051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    static final int STOPPED    = 0; // Not yet playing
62051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    static final int RUNNING    = 1; // Playing normally
63051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    static final int SEEKED     = 2; // Seeked to some time value
64a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
65a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
66a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Internal variables
67a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * NOTE: This object implements the clone() method, making a deep copy of any referenced
68a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * objects. As other non-trivial fields are added to this class, make sure to add logic
69a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * to clone() to make deep copies of them.
70a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
71a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
72a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The first time that the animation's animateFrame() method is called. This time is used to
73a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // determine elapsed time (and therefore the elapsed fraction) in subsequent calls
74a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // to animateFrame()
75051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    long mStartTime;
76a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
77a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
78a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Set when setCurrentPlayTime() is called. If negative, animation is not currently seeked
79a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * to a value.
80a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
81051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    long mSeekTime = -1;
82a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
838aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    /**
848aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * Set on the next frame after pause() is called, used to calculate a new startTime
858aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * or delayStartTime which allows the animator to continue from the point at which
868aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * it was paused. If negative, has not yet been set.
878aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     */
888aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    private long mPauseTime;
898aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
908aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    /**
918aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * Set when an animator is resumed. This triggers logic in the next frame which
928aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * actually resumes the animator.
938aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     */
948aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    private boolean mResumed = false;
958aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
968aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
97a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The static sAnimationHandler processes the internal timing loop on which all animations
98a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // are based
99be19e030a14c8e398e8af97fa898ea80187704dfChet Haase    /**
100be19e030a14c8e398e8af97fa898ea80187704dfChet Haase     * @hide
101be19e030a14c8e398e8af97fa898ea80187704dfChet Haase     */
102be19e030a14c8e398e8af97fa898ea80187704dfChet Haase    protected static ThreadLocal<AnimationHandler> sAnimationHandler =
103e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase            new ThreadLocal<AnimationHandler>();
104e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase
105a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The time interpolator to be used if none is set on the animation
106e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    private static final TimeInterpolator sDefaultInterpolator =
107e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase            new AccelerateDecelerateInterpolator();
108a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
109a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
110a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Used to indicate whether the animation is currently playing in reverse. This causes the
111a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * elapsed fraction to be inverted to calculate the appropriate values.
112a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
113a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private boolean mPlayingBackwards = false;
114a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
115a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
116a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This variable tracks the current iteration that is playing. When mCurrentIteration exceeds the
117a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * repeatCount (if repeatCount!=INFINITE), the animation ends
118a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
119a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private int mCurrentIteration = 0;
120a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
121a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
122a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     * Tracks current elapsed/eased fraction, for querying in getAnimatedFraction().
123a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     */
124a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    private float mCurrentFraction = 0f;
125a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase
126a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    /**
127a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Tracks whether a startDelay'd animation has begun playing through the startDelay.
128a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
129a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private boolean mStartedDelay = false;
130a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
131a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
132a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Tracks the time at which the animation began playing through its startDelay. This is
133a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * different from the mStartTime variable, which is used to track when the animation became
134a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * active (which is when the startDelay expired and the animation was added to the active
135a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animations list).
136a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
137a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private long mDelayStartTime;
138a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
139a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
140a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Flag that represents the current state of the animation. Used to figure out when to start
141a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * an animation (if state == STOPPED). Also used to end an animation that
142a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * has been cancel()'d or end()'d since the last animation frame. Possible values are
143e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase     * STOPPED, RUNNING, SEEKED.
144a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
145051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    int mPlayingState = STOPPED;
146a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
147a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
148b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * Additional playing state to indicate whether an animator has been start()'d. There is
149b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * some lag between a call to start() and the first animation frame. We should still note
150b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * that the animation has been started, even if it's first animation frame has not yet
151b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * happened, and reflect that state in isRunning().
152b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * Note that delayed animations are different: they are not started until their first
153b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * animation frame, which occurs after their delay elapses.
154b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     */
1558b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    private boolean mRunning = false;
1568b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase
1578b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    /**
1588b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     * Additional playing state to indicate whether an animator has been start()'d, whether or
1598b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     * not there is a nonzero startDelay.
1608b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     */
161b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase    private boolean mStarted = false;
162b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase
163b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase    /**
1648aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * Tracks whether we've notified listeners of the onAnimationStart() event. This can be
16517cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase     * complex to keep track of since we notify listeners at different times depending on
16617cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase     * startDelay and whether start() was called before end().
16717cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase     */
16817cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase    private boolean mStartListenersCalled = false;
16917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase
17017cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase    /**
171a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Flag that denotes whether the animation is set up and ready to go. Used to
172a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * set up animation that has not yet been started.
173a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
174a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    boolean mInitialized = false;
175a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
176a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    //
177a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // Backing variables
178a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    //
179a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
180a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // How long the animation should last in ms
181c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase    private long mDuration = (long)(300 * sDurationScale);
182d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase    private long mUnscaledDuration = 300;
183a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
184a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The amount of time in ms to delay starting the animation after start() is called
185a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private long mStartDelay = 0;
186d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase    private long mUnscaledStartDelay = 0;
187a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
188a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The number of times the animation will repeat. The default is 0, which means the animation
189a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // will play only once
190a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private int mRepeatCount = 0;
191a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
192a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
193a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The type of repetition that will occur when repeatMode is nonzero. RESTART means the
194a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation will start from the beginning on every new cycle. REVERSE means the animation
195a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will reverse directions on each iteration.
196a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
197a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private int mRepeatMode = RESTART;
198a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
199a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
200a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The time interpolator to be used. The elapsed fraction of the animation will be passed
201a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * through this interpolator to calculate the interpolated fraction, which is then used to
202a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * calculate the animated values.
203a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
204e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    private TimeInterpolator mInterpolator = sDefaultInterpolator;
205a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
206a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
207a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The set of listeners to be sent events through the life of an animation.
208a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
209a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private ArrayList<AnimatorUpdateListener> mUpdateListeners = null;
210a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
211a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
212a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The property/value sets being animated.
213a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
214a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    PropertyValuesHolder[] mValues;
215a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
216a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
217a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * A hashmap of the PropertyValuesHolder objects. This map is used to lookup animated values
218a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * by property name during calls to getAnimatedValue(String).
219a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
220a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    HashMap<String, PropertyValuesHolder> mValuesMap;
221a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
222a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
223a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Public constants
224a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
225a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
226a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
227a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * When the animation reaches the end and <code>repeatCount</code> is INFINITE
228a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * or a positive value, the animation restarts from the beginning.
229a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
230a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static final int RESTART = 1;
231a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
232a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * When the animation reaches the end and <code>repeatCount</code> is INFINITE
233a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * or a positive value, the animation reverses direction on every iteration.
234a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
235a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static final int REVERSE = 2;
236a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
237a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This value used used with the {@link #setRepeatCount(int)} property to repeat
238a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the animation indefinitely.
239a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
240a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static final int INFINITE = -1;
241a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
242c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase
243c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase    /**
244c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase     * @hide
245c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase     */
246c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase    public static void setDurationScale(float durationScale) {
247c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase        sDurationScale = durationScale;
248c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase    }
249c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase
250a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
251ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown     * @hide
252ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown     */
253ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown    public static float getDurationScale() {
254ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown        return sDurationScale;
255ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown    }
256ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown
257ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown    /**
258a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Creates a new ValueAnimator object. This default constructor is primarily for
2592794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * use internally; the factory methods which take parameters are more generally
260a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * useful.
261a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
262a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public ValueAnimator() {
263a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
264a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
265a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
2662794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between int values. A single
2672794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
2682794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
2692794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
2702794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
2712794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
272a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
2732794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
2742794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
27541f041d9986f8a5d45b6cb0b86e881c81a412168Chet Haase     */
2762794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofInt(int... values) {
2772794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
2782794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setIntValues(values);
2792794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
2802794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
2812794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
2822794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
2832794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between float values. A single
2842794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
2852794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
2862794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
2872794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
2882794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
2892794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
2902794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
2912794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
2922794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
2932794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofFloat(float... values) {
2942794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
2952794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setFloatValues(values);
2962794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
2972794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
2982794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
2992794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
3002794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between the values
3012794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * specified in the PropertyValuesHolder objects.
3022794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3032794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of PropertyValuesHolder objects whose values will be animated
3042794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * between over time.
3052794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
3062794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
3072794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofPropertyValuesHolder(PropertyValuesHolder... values) {
3082794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
3092794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setValues(values);
3102794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
3112794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
3122794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
3132794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between Object values. A single
3142794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
3152794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
3162794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
3172794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
3182794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
3192794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3202794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>Since ValueAnimator does not know how to animate between arbitrary Objects, this
3212794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * factory method also takes a TypeEvaluator object that the ValueAnimator will use
3222794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * to perform that interpolation.
3232794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3242794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param evaluator A TypeEvaluator that will be called on each animation frame to
3252794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * provide the ncessry interpolation between the Object values to derive the animated
3262794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value.
3272794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
3282794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
3292794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
3302794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofObject(TypeEvaluator evaluator, Object... values) {
3312794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
3322794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setObjectValues(values);
3332794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setEvaluator(evaluator);
3342794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
3352794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
3362794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
3372794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
3382794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Sets int values that will be animated between. A single
3392794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
3402794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
3412794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
3422794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
3432794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
3442794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3452794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>If there are already multiple sets of values defined for this ValueAnimator via more
3462794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * than one PropertyValuesHolder object, this method will set the values for the first
3472794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * of those objects.</p>
3482794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3492794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
3502794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
3512794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public void setIntValues(int... values) {
3522794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (values == null || values.length == 0) {
3532794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            return;
3542794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
3552794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (mValues == null || mValues.length == 0) {
35618772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy            setValues(PropertyValuesHolder.ofInt("", values));
3572794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        } else {
3582794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            PropertyValuesHolder valuesHolder = mValues[0];
3592794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            valuesHolder.setIntValues(values);
3602794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
3612794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        // New property/values/target should cause re-initialization prior to starting
3622794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        mInitialized = false;
3632794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
3642794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
3652794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
3662794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Sets float 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 setFloatValues(float... values) {
3802794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (values == null || values.length == 0) {
3812794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            return;
38241f041d9986f8a5d45b6cb0b86e881c81a412168Chet Haase        }
3832794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (mValues == null || mValues.length == 0) {
38418772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy            setValues(PropertyValuesHolder.ofFloat("", values));
3852794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        } else {
3862794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            PropertyValuesHolder valuesHolder = mValues[0];
3872794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            valuesHolder.setFloatValues(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 the values to animate between for this animation. 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     * <p>There should be a TypeEvaluator set on the ValueAnimator that knows how to interpolate
4062794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * between these value objects. ValueAnimator only knows how to interpolate between the
4072794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * primitive types specified in the other setValues() methods.</p>
4082794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4092794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values The set of values to animate between.
4102794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
4112794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public void setObjectValues(Object... values) {
4122794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (values == null || values.length == 0) {
4132794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            return;
4142794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
4152794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (mValues == null || mValues.length == 0) {
41618772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy            setValues(PropertyValuesHolder.ofObject("", null, values));
4172794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        } else {
4182794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            PropertyValuesHolder valuesHolder = mValues[0];
4192794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            valuesHolder.setObjectValues(values);
4202794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
4212794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        // New property/values/target should cause re-initialization prior to starting
4222794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        mInitialized = false;
423a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
424a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
425a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
426a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets the values, per property, being animated between. This function is called internally
427f76a50ce8fdc6aea22cabc77b2977a1a15a79630Ken Wakasa     * by the constructors of ValueAnimator that take a list of values. But a ValueAnimator can
428a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * be constructed without values and this method can be called to set the values manually
429a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * instead.
430a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
431a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param values The set of values, per property, being animated between.
432a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
433a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setValues(PropertyValuesHolder... values) {
434a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        int numValues = values.length;
435a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mValues = values;
436a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues);
437a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        for (int i = 0; i < numValues; ++i) {
43818772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy            PropertyValuesHolder valuesHolder = values[i];
439a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mValuesMap.put(valuesHolder.getPropertyName(), valuesHolder);
440a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
4410e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase        // New property/values/target should cause re-initialization prior to starting
4420e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase        mInitialized = false;
443a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
444a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
445a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
446a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Returns the values that this ValueAnimator animates between. These values are stored in
447a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * PropertyValuesHolder objects, even if the ValueAnimator was created with a simple list
448a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of value objects instead.
449a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
450a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return PropertyValuesHolder[] An array of PropertyValuesHolder objects which hold the
451a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * values, per property, that define the animation.
452a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
453a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public PropertyValuesHolder[] getValues() {
454a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mValues;
455a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
456a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
457a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
458a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This function is called immediately before processing the first animation
459a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frame of an animation. If there is a nonzero <code>startDelay</code>, the
460a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function is called after that delay ends.
461a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * It takes care of the final initialization steps for the
462a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation.
463a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
464a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *  <p>Overrides of this method should call the superclass method to ensure
465a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *  that internal mechanisms for the animation are set up correctly.</p>
466a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
467a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    void initAnimation() {
468a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (!mInitialized) {
469a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            int numValues = mValues.length;
470a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 0; i < numValues; ++i) {
471a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mValues[i].init();
472a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
473a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mInitialized = true;
474a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
475a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
476a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
477a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
478a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
4792794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Sets the length of the animation. The default duration is 300 milliseconds.
480a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
48127c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase     * @param duration The length of the animation, in milliseconds. This value cannot
48227c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase     * be negative.
4832794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return ValueAnimator The object called with setDuration(). This return
4842794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value makes it easier to compose statements together that construct and then set the
4852794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * duration, as in <code>ValueAnimator.ofInt(0, 10).setDuration(500).start()</code>.
486a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
4872794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public ValueAnimator setDuration(long duration) {
48827c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase        if (duration < 0) {
48927c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase            throw new IllegalArgumentException("Animators cannot have negative duration: " +
49027c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase                    duration);
49127c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase        }
492d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase        mUnscaledDuration = duration;
493d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase        mDuration = (long)(duration * sDurationScale);
4942794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return this;
495a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
496a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
497a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
4982794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Gets the length of the animation. The default duration is 300 milliseconds.
499a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
500a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return The length of the animation, in milliseconds.
501a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
502a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public long getDuration() {
503d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase        return mUnscaledDuration;
504a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
505a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
506a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
507a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets the position of the animation to the specified point in time. This time should
508a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * be between 0 and the total duration of the animation, including any repetition. If
509a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the animation has not yet been started, then it will not advance forward after it is
510a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * set to this time; it will simply set the time to this value and perform any appropriate
511a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * actions based on that time. If the animation is already running, then setCurrentPlayTime()
512a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will set the current playing time to this value and continue playing from that point.
513a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
514a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param playTime The time, in milliseconds, to which the animation is advanced or rewound.
515a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
516a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setCurrentPlayTime(long playTime) {
517a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        initAnimation();
518a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        long currentTime = AnimationUtils.currentAnimationTimeMillis();
519a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mPlayingState != RUNNING) {
520a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mSeekTime = playTime;
521a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mPlayingState = SEEKED;
522a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
523a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mStartTime = currentTime - playTime;
52420c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        doAnimationFrame(currentTime);
525a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
526a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
527a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
528a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Gets the current position of the animation in time, which is equal to the current
529a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * time minus the time that the animation started. An animation that is not yet started will
530a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * return a value of zero.
531a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
532a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return The current position in time of the animation.
533a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
534a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public long getCurrentPlayTime() {
535a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (!mInitialized || mPlayingState == STOPPED) {
536a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return 0;
537a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
538a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return AnimationUtils.currentAnimationTimeMillis() - mStartTime;
539a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
540a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
541a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
542a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This custom, static handler handles the timing pulse that is shared by
543a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * all active animations. This approach ensures that the setting of animation
544a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * values will happen on the UI thread and that all animations will share
545a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the same times for calculating their values, which makes synchronizing
546a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animations possible.
547a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
54820c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     * The handler uses the Choreographer for executing periodic callbacks.
549be19e030a14c8e398e8af97fa898ea80187704dfChet Haase     *
550be19e030a14c8e398e8af97fa898ea80187704dfChet Haase     * @hide
551a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
55218772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy    @SuppressWarnings("unchecked")
553be19e030a14c8e398e8af97fa898ea80187704dfChet Haase    protected static class AnimationHandler implements Runnable {
5549c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        // The per-thread list of all active animations
555be19e030a14c8e398e8af97fa898ea80187704dfChet Haase        /** @hide */
556be19e030a14c8e398e8af97fa898ea80187704dfChet Haase        protected final ArrayList<ValueAnimator> mAnimations = new ArrayList<ValueAnimator>();
5579c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown
5582936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase        // Used in doAnimationFrame() to avoid concurrent modifications of mAnimations
5592936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase        private final ArrayList<ValueAnimator> mTmpAnimations = new ArrayList<ValueAnimator>();
5602936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase
5619c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        // The per-thread set of animations to be started on the next animation frame
562be19e030a14c8e398e8af97fa898ea80187704dfChet Haase        /** @hide */
563be19e030a14c8e398e8af97fa898ea80187704dfChet Haase        protected final ArrayList<ValueAnimator> mPendingAnimations = new ArrayList<ValueAnimator>();
5649c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown
5659c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        /**
5669c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown         * Internal per-thread collections used to avoid set collisions as animations start and end
5679c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown         * while being processed.
568be19e030a14c8e398e8af97fa898ea80187704dfChet Haase         * @hide
5699c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown         */
570be19e030a14c8e398e8af97fa898ea80187704dfChet Haase        protected final ArrayList<ValueAnimator> mDelayedAnims = new ArrayList<ValueAnimator>();
5719c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        private final ArrayList<ValueAnimator> mEndingAnims = new ArrayList<ValueAnimator>();
5729c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        private final ArrayList<ValueAnimator> mReadyAnims = new ArrayList<ValueAnimator>();
5739c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown
57496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        private final Choreographer mChoreographer;
5754a06c8008b2edd6677f9a411af79b0a4971b87feJeff Brown        private boolean mAnimationScheduled;
57696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown
57796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        private AnimationHandler() {
57896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            mChoreographer = Choreographer.getInstance();
57996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        }
58096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown
581a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        /**
58220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown         * Start animating on the next frame.
583a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         */
58420c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        public void start() {
58520c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            scheduleAnimation();
58696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        }
587a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
58820c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        private void doAnimationFrame(long frameTime) {
58996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // mPendingAnimations holds any animations that have requested to be started
59096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // We're going to clear mPendingAnimations, but starting animation may
59196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // cause more to be added to the pending list (for example, if one animation
59296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // starting triggers another starting). So we loop until mPendingAnimations
59396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // is empty.
59496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            while (mPendingAnimations.size() > 0) {
59596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                ArrayList<ValueAnimator> pendingCopy =
59696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                        (ArrayList<ValueAnimator>) mPendingAnimations.clone();
59796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                mPendingAnimations.clear();
59896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                int count = pendingCopy.size();
59996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                for (int i = 0; i < count; ++i) {
60096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    ValueAnimator anim = pendingCopy.get(i);
60196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    // If the animation has a startDelay, place it on the delayed list
60296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    if (anim.mStartDelay == 0) {
60396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                        anim.startAnimation(this);
60496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    } else {
60596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                        mDelayedAnims.add(anim);
606a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    }
60796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                }
60896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            }
609c6ffab32415a58bbb010dcd115684f9dbc249710Chet Haase            // Next, process animations currently sitting on the delayed queue, adding
61096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // them to the active animations if they are ready
61196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            int numDelayedAnims = mDelayedAnims.size();
61296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            for (int i = 0; i < numDelayedAnims; ++i) {
61396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                ValueAnimator anim = mDelayedAnims.get(i);
61420c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown                if (anim.delayedAnimationFrame(frameTime)) {
61596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    mReadyAnims.add(anim);
61696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                }
61796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            }
61896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            int numReadyAnims = mReadyAnims.size();
61996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            if (numReadyAnims > 0) {
62096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                for (int i = 0; i < numReadyAnims; ++i) {
62196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    ValueAnimator anim = mReadyAnims.get(i);
62296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    anim.startAnimation(this);
62396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    anim.mRunning = true;
62496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    mDelayedAnims.remove(anim);
62596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                }
62696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                mReadyAnims.clear();
62796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            }
62896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown
62996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // Now process all active animations. The return value from animationFrame()
63096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // tells the handler whether it should now be ended
63196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            int numAnims = mAnimations.size();
6322936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase            for (int i = 0; i < numAnims; ++i) {
6332936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase                mTmpAnimations.add(mAnimations.get(i));
6342936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase            }
6352936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase            for (int i = 0; i < numAnims; ++i) {
6362936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase                ValueAnimator anim = mTmpAnimations.get(i);
6372936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase                if (mAnimations.contains(anim) && anim.doAnimationFrame(frameTime)) {
63896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    mEndingAnims.add(anim);
63996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                }
64096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            }
6412936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase            mTmpAnimations.clear();
64296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            if (mEndingAnims.size() > 0) {
6432936fc0244ab2010696b3b1d723d6bbbc693916eChet Haase                for (int i = 0; i < mEndingAnims.size(); ++i) {
64496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                    mEndingAnims.get(i).endAnimation(this);
64596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                }
64696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown                mEndingAnims.clear();
64796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            }
64896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown
64996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // If there are still active or delayed animations, schedule a future call to
65096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown            // onAnimate to process the next frame of the animations.
65120c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            if (!mAnimations.isEmpty() || !mDelayedAnims.isEmpty()) {
65220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown                scheduleAnimation();
653a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
654a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
65596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown
6564a06c8008b2edd6677f9a411af79b0a4971b87feJeff Brown        // Called by the Choreographer.
65796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        @Override
6584a06c8008b2edd6677f9a411af79b0a4971b87feJeff Brown        public void run() {
6594a06c8008b2edd6677f9a411af79b0a4971b87feJeff Brown            mAnimationScheduled = false;
66020c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            doAnimationFrame(mChoreographer.getFrameTime());
66120c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        }
66220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown
66320c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        private void scheduleAnimation() {
66420c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            if (!mAnimationScheduled) {
66520c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown                mChoreographer.postCallback(Choreographer.CALLBACK_ANIMATION, this, null);
66620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown                mAnimationScheduled = true;
66720c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            }
66896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        }
669a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
670a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
671a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
672a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, to delay starting the animation after
673a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link #start()} is called.
674a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
675a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return the number of milliseconds to delay running the animation
676a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
677a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public long getStartDelay() {
678d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase        return mUnscaledStartDelay;
679a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
680a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
681a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
682a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, to delay starting the animation after
683a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link #start()} is called.
684a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
685a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param startDelay The amount of the delay, in milliseconds
686a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
687a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setStartDelay(long startDelay) {
688d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase        this.mStartDelay = (long)(startDelay * sDurationScale);
689d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase        mUnscaledStartDelay = startDelay;
690a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
691a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
692a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
693a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, between each frame of the animation. This is a
694a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * requested time that the animation will attempt to honor, but the actual delay between
695a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frames may be different, depending on system load and capabilities. This is a static
696a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function because the same delay will be applied to all animations, since they are all
697a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * run off of a single timing loop.
698a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
69996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     * The frame delay may be ignored when the animation system uses an external timing
70096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     * source, such as the display refresh rate (vsync), to govern animations.
70196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     *
702a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return the requested time between frames, in milliseconds
703a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
704a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static long getFrameDelay() {
70596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        return Choreographer.getFrameDelay();
706a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
707a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
708a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
709a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, between each frame of the animation. This is a
710a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * requested time that the animation will attempt to honor, but the actual delay between
711a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frames may be different, depending on system load and capabilities. This is a static
712a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function because the same delay will be applied to all animations, since they are all
713a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * run off of a single timing loop.
714a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
71596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     * The frame delay may be ignored when the animation system uses an external timing
71696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     * source, such as the display refresh rate (vsync), to govern animations.
71796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     *
718a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param frameDelay the requested time between frames, in milliseconds
719a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
720a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static void setFrameDelay(long frameDelay) {
72196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown        Choreographer.setFrameDelay(frameDelay);
722a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
723a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
724a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
725a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The most recent value calculated by this <code>ValueAnimator</code> when there is just one
726a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * property being animated. This value is only sensible while the animation is running. The main
727a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * purpose for this read-only property is to retrieve the value from the <code>ValueAnimator</code>
728a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * during a call to {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which
729a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is called during each animation frame, immediately after the value is calculated.
730a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
731a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return animatedValue The value most recently calculated by this <code>ValueAnimator</code> for
732a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the single property being animated. If there are several properties being animated
733a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * (specified by several PropertyValuesHolder objects in the constructor), this function
734a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * returns the animated value for the first of those objects.
735a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
736a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public Object getAnimatedValue() {
737a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mValues != null && mValues.length > 0) {
738a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return mValues[0].getAnimatedValue();
739a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
740a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        // Shouldn't get here; should always have values unless ValueAnimator was set up wrong
741a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return null;
742a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
743a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
744a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
745a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The most recent value calculated by this <code>ValueAnimator</code> for <code>propertyName</code>.
746a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The main purpose for this read-only property is to retrieve the value from the
747a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <code>ValueAnimator</code> during a call to
748a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which
749a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is called during each animation frame, immediately after the value is calculated.
750a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
751a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return animatedValue The value most recently calculated for the named property
752a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * by this <code>ValueAnimator</code>.
753a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
754a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public Object getAnimatedValue(String propertyName) {
755a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        PropertyValuesHolder valuesHolder = mValuesMap.get(propertyName);
756a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (valuesHolder != null) {
757a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return valuesHolder.getAnimatedValue();
758a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        } else {
759a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // At least avoid crashing if called with bogus propertyName
760a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return null;
761a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
762a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
763a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
764a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
765a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets how many times the animation should be repeated. If the repeat
766a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * count is 0, the animation is never repeated. If the repeat count is
767a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * greater than 0 or {@link #INFINITE}, the repeat mode will be taken
768a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * into account. The repeat count is 0 by default.
769a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
770a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param value the number of times the animation should be repeated
771a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
772a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setRepeatCount(int value) {
773a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mRepeatCount = value;
774a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
775a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
776a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Defines how many times the animation should repeat. The default value
777a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is 0.
778a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
779a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return the number of times the animation should repeat, or {@link #INFINITE}
780a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
781a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public int getRepeatCount() {
782a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mRepeatCount;
783a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
784a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
785a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
786a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Defines what this animation should do when it reaches the end. This
787a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * setting is applied only when the repeat count is either greater than
788a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * 0 or {@link #INFINITE}. Defaults to {@link #RESTART}.
789a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
790a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param value {@link #RESTART} or {@link #REVERSE}
791a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
792a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setRepeatMode(int value) {
793a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mRepeatMode = value;
794a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
795a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
796a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
797a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Defines what this animation should do when it reaches the end.
798a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
799a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return either one of {@link #REVERSE} or {@link #RESTART}
800a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
801a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public int getRepeatMode() {
802a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mRepeatMode;
803a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
804a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
805a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
806a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Adds a listener to the set of listeners that are sent update events through the life of
807a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * an animation. This method is called on all listeners for every frame of the animation,
808a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * after the values for the animation have been calculated.
809a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
810a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param listener the listener to be added to the current set of listeners for this animation.
811a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
812a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void addUpdateListener(AnimatorUpdateListener listener) {
813a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners == null) {
814a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mUpdateListeners = new ArrayList<AnimatorUpdateListener>();
815a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
816a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mUpdateListeners.add(listener);
817a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
818a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
819a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
8203060421045d4d9e411797f91bb509824b03e33fbJim Miller     * Removes all listeners from the set listening to frame updates for this animation.
8213060421045d4d9e411797f91bb509824b03e33fbJim Miller     */
8223060421045d4d9e411797f91bb509824b03e33fbJim Miller    public void removeAllUpdateListeners() {
8233060421045d4d9e411797f91bb509824b03e33fbJim Miller        if (mUpdateListeners == null) {
8243060421045d4d9e411797f91bb509824b03e33fbJim Miller            return;
8253060421045d4d9e411797f91bb509824b03e33fbJim Miller        }
8263060421045d4d9e411797f91bb509824b03e33fbJim Miller        mUpdateListeners.clear();
8273060421045d4d9e411797f91bb509824b03e33fbJim Miller        mUpdateListeners = null;
8283060421045d4d9e411797f91bb509824b03e33fbJim Miller    }
8293060421045d4d9e411797f91bb509824b03e33fbJim Miller
8303060421045d4d9e411797f91bb509824b03e33fbJim Miller    /**
831a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Removes a listener from the set listening to frame updates for this animation.
832a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
833a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param listener the listener to be removed from the current set of update listeners
834a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * for this animation.
835a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
836a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void removeUpdateListener(AnimatorUpdateListener listener) {
837a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners == null) {
838a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return;
839a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
840a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mUpdateListeners.remove(listener);
841a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners.size() == 0) {
842a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mUpdateListeners = null;
843a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
844a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
845a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
846a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
847a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
848a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The time interpolator used in calculating the elapsed fraction of this animation. The
849a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * interpolator determines whether the animation runs with linear or non-linear motion,
850a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * such as acceleration and deceleration. The default value is
851a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link android.view.animation.AccelerateDecelerateInterpolator}
852a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
85327c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase     * @param value the interpolator to be used by this animation. A value of <code>null</code>
85427c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase     * will result in linear interpolation.
855a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
856a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
857e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    public void setInterpolator(TimeInterpolator value) {
858a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (value != null) {
859a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mInterpolator = value;
86027c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase        } else {
86127c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase            mInterpolator = new LinearInterpolator();
862a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
863a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
864a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
865a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
866a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Returns the timing interpolator that this ValueAnimator uses.
867a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
868a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return The timing interpolator for this ValueAnimator.
869a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
870430742f09063574271e6c4091de13b9b9e762514Chet Haase    @Override
871e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    public TimeInterpolator getInterpolator() {
872a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mInterpolator;
873a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
874a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
875a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
876a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The type evaluator to be used when calculating the animated values of this animation.
877b2ab04ffb6894f399d5c9ceb15f64eb17b654426Chet Haase     * The system will automatically assign a float or int evaluator based on the type
878a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of <code>startValue</code> and <code>endValue</code> in the constructor. But if these values
879a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * are not one of these primitive types, or if different evaluation is desired (such as is
880a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * necessary with int values that represent colors), a custom evaluator needs to be assigned.
88153ee3316bcb3590ff156b3fd7108903c0817c35dChet Haase     * For example, when running an animation on color values, the {@link ArgbEvaluator}
882a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * should be used to get correct RGB color interpolation.
883a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
884a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>If this ValueAnimator has only one set of values being animated between, this evaluator
885a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will be used for that set. If there are several sets of values being animated, which is
886fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase     * the case if PropertyValuesHolder objects were set on the ValueAnimator, then the evaluator
887a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is assigned just to the first PropertyValuesHolder object.</p>
888a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
889a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param value the evaluator to be used this animation
890a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
891a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setEvaluator(TypeEvaluator value) {
892a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (value != null && mValues != null && mValues.length > 0) {
893a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mValues[0].setEvaluator(value);
894a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
895a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
896a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
89717cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase    private void notifyStartListeners() {
89817cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        if (mListeners != null && !mStartListenersCalled) {
89917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            ArrayList<AnimatorListener> tmpListeners =
90017cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                    (ArrayList<AnimatorListener>) mListeners.clone();
90117cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            int numListeners = tmpListeners.size();
90217cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            for (int i = 0; i < numListeners; ++i) {
90317cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                tmpListeners.get(i).onAnimationStart(this);
90417cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            }
90517cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        }
90617cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        mStartListenersCalled = true;
90717cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase    }
90817cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase
909a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
910a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Start the animation playing. This version of start() takes a boolean flag that indicates
911a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * whether the animation should play in reverse. The flag is usually false, but may be set
9122970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * to true if called from the reverse() method.
9132970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     *
9142970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * <p>The animation started by calling this method will be run on the thread that called
9152970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * this method. This thread should have a Looper on it (a runtime exception will be thrown if
9162970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * this is not the case). Also, if the animation will animate
9172970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * properties of objects in the view hierarchy, then the calling thread should be the UI
9182970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * thread for that view hierarchy.</p>
919a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
920a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param playBackwards Whether the ValueAnimator should start playing in reverse.
921a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
922a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private void start(boolean playBackwards) {
9232970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        if (Looper.myLooper() == null) {
9242970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            throw new AndroidRuntimeException("Animators may only be run on Looper threads");
9253060421045d4d9e411797f91bb509824b03e33fbJim Miller        }
9262970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        mPlayingBackwards = playBackwards;
927add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase        mCurrentIteration = 0;
928add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase        mPlayingState = STOPPED;
9298b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        mStarted = true;
930add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase        mStartedDelay = false;
9318aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        mPaused = false;
9329c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        AnimationHandler animationHandler = getOrCreateAnimationHandler();
9339c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        animationHandler.mPendingAnimations.add(this);
9342970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        if (mStartDelay == 0) {
935add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase            // This sets the initial value of the animation, prior to actually starting it running
93659bbef0cd781f4933fd8a0a85b6067f36e529e02Jeff Brown            setCurrentPlayTime(0);
937154f14508a11627d5a995b6fe2a14a83d794a6feChet Haase            mPlayingState = STOPPED;
9388b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase            mRunning = true;
93917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            notifyStartListeners();
940a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
94120c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        animationHandler.start();
942a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
943a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
944a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
945a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void start() {
946a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        start(false);
947a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
948a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
949a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
950a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void cancel() {
9512970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        // Only cancel if the animation is actually running or has been started and is about
9522970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        // to run
9539c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        AnimationHandler handler = getOrCreateAnimationHandler();
9549c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        if (mPlayingState != STOPPED
9559c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown                || handler.mPendingAnimations.contains(this)
9569c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown                || handler.mDelayedAnims.contains(this)) {
957b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase            // Only notify listeners if the animator has actually started
95817cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            if ((mStarted || mRunning) && mListeners != null) {
95917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                if (!mRunning) {
96017cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                    // If it's not yet running, then start listeners weren't called. Call them now.
96117cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                    notifyStartListeners();
96217cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                }
9637dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                ArrayList<AnimatorListener> tmpListeners =
9647dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                        (ArrayList<AnimatorListener>) mListeners.clone();
9657dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                for (AnimatorListener listener : tmpListeners) {
9667dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                    listener.onAnimationCancel(this);
9677dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase                }
9687dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase            }
9699c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown            endAnimation(handler);
9702970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        }
971a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
972a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
973a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
974a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void end() {
9759c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        AnimationHandler handler = getOrCreateAnimationHandler();
9769c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        if (!handler.mAnimations.contains(this) && !handler.mPendingAnimations.contains(this)) {
977a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // Special case if the animation has not yet started; get it ready for ending
978a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mStartedDelay = false;
9799c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown            startAnimation(handler);
98017cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            mStarted = true;
981add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase        } else if (!mInitialized) {
982add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase            initAnimation();
983e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase        }
9844dd176864310e1d9519bf6b88918913e9927984fChet Haase        animateValue(mPlayingBackwards ? 0f : 1f);
9859c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        endAnimation(handler);
986a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
987a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
988a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
9898aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    public void resume() {
9908aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        if (mPaused) {
9918aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            mResumed = true;
9928aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        }
9938aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        super.resume();
9948aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    }
9958aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
9968aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    @Override
9978aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    public void pause() {
9988aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        boolean previouslyPaused = mPaused;
9998aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        super.pause();
10008aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        if (!previouslyPaused && mPaused) {
10018aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            mPauseTime = -1;
10028aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            mResumed = false;
10038aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        }
10048aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    }
10058aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
10068aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    @Override
1007a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public boolean isRunning() {
10088b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        return (mPlayingState == RUNNING || mRunning);
10098b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    }
10108b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase
10118b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    @Override
10128b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    public boolean isStarted() {
10138b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        return mStarted;
1014a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1015a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1016a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1017a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Plays the ValueAnimator in reverse. If the animation is already running,
1018a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * it will stop itself and play backwards from the point reached when reverse was called.
1019a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * If the animation is not currently running, then it will start from the end and
1020a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * play backwards. This behavior is only set for the current animation; future playing
1021a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of the animation will use the default behavior of playing forward.
1022a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1023a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void reverse() {
1024a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mPlayingBackwards = !mPlayingBackwards;
1025a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mPlayingState == RUNNING) {
1026a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            long currentTime = AnimationUtils.currentAnimationTimeMillis();
1027a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            long currentPlayTime = currentTime - mStartTime;
1028a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            long timeLeft = mDuration - currentPlayTime;
1029a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mStartTime = currentTime - timeLeft;
1030f43fb2a57f152b5760d8792fec26f36d46b23817Chet Haase        } else if (mStarted) {
1031f43fb2a57f152b5760d8792fec26f36d46b23817Chet Haase            end();
1032a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        } else {
1033a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            start(true);
1034a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1035a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1036a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1037a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1038a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Called internally to end an animation by removing it from the animations list. Must be
1039a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * called on the UI thread.
1040a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
10419c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown    private void endAnimation(AnimationHandler handler) {
10429c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        handler.mAnimations.remove(this);
10439c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        handler.mPendingAnimations.remove(this);
10449c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        handler.mDelayedAnims.remove(this);
1045a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mPlayingState = STOPPED;
10468aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        mPaused = false;
104717cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        if ((mStarted || mRunning) && mListeners != null) {
104817cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            if (!mRunning) {
104917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                // If it's not yet running, then start listeners weren't called. Call them now.
105017cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                notifyStartListeners();
105117cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase             }
1052a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            ArrayList<AnimatorListener> tmpListeners =
1053a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    (ArrayList<AnimatorListener>) mListeners.clone();
10547c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            int numListeners = tmpListeners.size();
10557c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            for (int i = 0; i < numListeners; ++i) {
10567c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                tmpListeners.get(i).onAnimationEnd(this);
1057a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1058a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
10598b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        mRunning = false;
1060b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase        mStarted = false;
106117cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        mStartListenersCalled = false;
1062caf46486f54cdc899e383dfc776ec33a81b089a1Chet Haase        mPlayingBackwards = false;
10639b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase        if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
10649b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase            Trace.asyncTraceEnd(Trace.TRACE_TAG_VIEW, getNameForTrace(),
10659b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase                    System.identityHashCode(this));
10669b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase        }
1067a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1068a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1069a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1070a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Called internally to start an animation by adding it to the active animations list. Must be
1071a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * called on the UI thread.
1072a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
10739c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown    private void startAnimation(AnimationHandler handler) {
10749b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase        if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
10759b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase            Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW, getNameForTrace(),
10769b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase                    System.identityHashCode(this));
10779b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase        }
1078a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        initAnimation();
10799c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        handler.mAnimations.add(this);
1080b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase        if (mStartDelay > 0 && mListeners != null) {
1081b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase            // Listeners were already notified in start() if startDelay is 0; this is
1082b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase            // just for delayed animations
108317cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            notifyStartListeners();
1084a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1085a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1086a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1087a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1088fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase     * Returns the name of this animator for debugging purposes.
1089fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase     */
1090fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase    String getNameForTrace() {
1091fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase        return "animator";
1092fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase    }
1093fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase
1094fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase
1095fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase    /**
1096a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Internal function called to process an animation frame on an animation that is currently
1097a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * sleeping through its <code>startDelay</code> phase. The return value indicates whether it
1098a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * should be woken up and put on the active animations queue.
1099a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1100a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param currentTime The current animation time, used to calculate whether the animation
1101a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * has exceeded its <code>startDelay</code> and should be started.
1102a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return True if the animation's <code>startDelay</code> has been exceeded and the animation
1103a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * should be added to the set of active animations.
1104a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1105a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private boolean delayedAnimationFrame(long currentTime) {
1106a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (!mStartedDelay) {
1107a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mStartedDelay = true;
1108a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mDelayStartTime = currentTime;
1109a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        } else {
11108aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            if (mPaused) {
11118aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                if (mPauseTime < 0) {
11128aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                    mPauseTime = currentTime;
11138aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                }
11148aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                return false;
11158aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            } else if (mResumed) {
11168aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                mResumed = false;
11178aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                if (mPauseTime > 0) {
11188aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                    // Offset by the duration that the animation was paused
11198aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                    mDelayStartTime += (currentTime - mPauseTime);
11208aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                }
11218aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            }
1122a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            long deltaTime = currentTime - mDelayStartTime;
1123a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (deltaTime > mStartDelay) {
1124a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                // startDelay ended - start the anim and record the
1125a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                // mStartTime appropriately
1126a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mStartTime = currentTime - (deltaTime - mStartDelay);
1127a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mPlayingState = RUNNING;
1128a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                return true;
1129a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1130a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1131a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return false;
1132a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1133a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1134a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1135a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This internal function processes a single animation frame for a given animation. The
1136a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * currentTime parameter is the timing pulse sent by the handler, used to calculate the
1137a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * elapsed duration, and therefore
1138a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the elapsed fraction, of the animation. The return value indicates whether the animation
1139a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * should be ended (which happens when the elapsed time of the animation exceeds the
1140a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation's duration, including the repeatCount).
1141a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1142a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param currentTime The current time, as tracked by the static timing handler
1143a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return true if the animation's duration, including any repetitions due to
11448aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * <code>repeatCount</code>, has been exceeded and the animation should be ended.
1145a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1146051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    boolean animationFrame(long currentTime) {
1147a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        boolean done = false;
1148a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        switch (mPlayingState) {
1149a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        case RUNNING:
1150a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        case SEEKED:
115170d4ba15b1f0c1133c5aabc86de828b41e482fffChet Haase            float fraction = mDuration > 0 ? (float)(currentTime - mStartTime) / mDuration : 1f;
1152a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (fraction >= 1f) {
1153a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                if (mCurrentIteration < mRepeatCount || mRepeatCount == INFINITE) {
1154a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    // Time to repeat
1155a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    if (mListeners != null) {
11567c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                        int numListeners = mListeners.size();
11577c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                        for (int i = 0; i < numListeners; ++i) {
11587c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                            mListeners.get(i).onAnimationRepeat(this);
1159a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                        }
1160a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    }
1161a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    if (mRepeatMode == REVERSE) {
116218772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy                        mPlayingBackwards = !mPlayingBackwards;
1163a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    }
1164730666858692ea396f5ad779654b5d86ff90b6caChet Haase                    mCurrentIteration += (int)fraction;
1165730666858692ea396f5ad779654b5d86ff90b6caChet Haase                    fraction = fraction % 1f;
1166a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    mStartTime += mDuration;
1167a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                } else {
1168a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    done = true;
1169a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    fraction = Math.min(fraction, 1.0f);
1170a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                }
1171a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1172a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (mPlayingBackwards) {
1173a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                fraction = 1f - fraction;
1174a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1175a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            animateValue(fraction);
1176a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            break;
1177a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1178a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1179a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return done;
1180a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1181a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1182a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
118320c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     * Processes a frame of the animation, adjusting the start time if needed.
118420c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     *
118520c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     * @param frameTime The frame time.
118620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     * @return true if the animation has ended.
118720c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     */
118820c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown    final boolean doAnimationFrame(long frameTime) {
118920c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        if (mPlayingState == STOPPED) {
119020c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            mPlayingState = RUNNING;
119120c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            if (mSeekTime < 0) {
119220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown                mStartTime = frameTime;
119320c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            } else {
119420c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown                mStartTime = frameTime - mSeekTime;
119520c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown                // Now that we're playing, reset the seek time
119620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown                mSeekTime = -1;
119720c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            }
119820c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        }
11998aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        if (mPaused) {
12008aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            if (mPauseTime < 0) {
12018aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                mPauseTime = frameTime;
12028aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            }
12038aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            return false;
12048aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        } else if (mResumed) {
12058aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            mResumed = false;
12068aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            if (mPauseTime > 0) {
12078aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                // Offset by the duration that the animation was paused
12088aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                mStartTime += (frameTime - mPauseTime);
12098aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            }
12108aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        }
121120c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        // The frame time might be before the start time during the first frame of
121220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        // an animation.  The "current time" must always be on or after the start
121320c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        // time to avoid animating frames at negative time intervals.  In practice, this
121420c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        // is very rare and only happens when seeking backwards.
121520c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        final long currentTime = Math.max(frameTime, mStartTime);
121620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        return animationFrame(currentTime);
121720c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown    }
121820c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown
121920c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown    /**
1220a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     * Returns the current animation fraction, which is the elapsed/interpolated fraction used in
1221a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     * the most recent frame update on the animation.
1222a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     *
1223a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     * @return Elapsed/interpolated fraction of the animation.
1224a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     */
1225a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    public float getAnimatedFraction() {
1226a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase        return mCurrentFraction;
1227a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    }
1228a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase
1229a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    /**
1230a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This method is called with the elapsed fraction of the animation during every
1231a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation frame. This function turns the elapsed fraction into an interpolated fraction
1232a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * and then into an animated value (from the evaluator. The function is called mostly during
1233a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation updates, but it is also called when the <code>end()</code>
1234a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function is called, to set the final value on the property.
1235a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1236a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>Overrides of this method must call the superclass to perform the calculation
1237a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of the animated value.</p>
1238a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1239a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param fraction The elapsed fraction of the animation.
1240a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1241a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    void animateValue(float fraction) {
1242a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        fraction = mInterpolator.getInterpolation(fraction);
1243a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase        mCurrentFraction = fraction;
1244a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        int numValues = mValues.length;
1245a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        for (int i = 0; i < numValues; ++i) {
1246a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mValues[i].calculateValue(fraction);
1247a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1248a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners != null) {
1249a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            int numListeners = mUpdateListeners.size();
1250a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 0; i < numListeners; ++i) {
1251a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mUpdateListeners.get(i).onAnimationUpdate(this);
1252a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1253a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1254a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1255a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1256a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
1257a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public ValueAnimator clone() {
1258a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        final ValueAnimator anim = (ValueAnimator) super.clone();
1259a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners != null) {
1260a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            ArrayList<AnimatorUpdateListener> oldListeners = mUpdateListeners;
1261a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            anim.mUpdateListeners = new ArrayList<AnimatorUpdateListener>();
1262a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            int numListeners = oldListeners.size();
1263a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 0; i < numListeners; ++i) {
1264a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                anim.mUpdateListeners.add(oldListeners.get(i));
1265a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1266a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1267a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mSeekTime = -1;
1268a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mPlayingBackwards = false;
1269a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mCurrentIteration = 0;
1270a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mInitialized = false;
1271a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mPlayingState = STOPPED;
1272a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mStartedDelay = false;
1273a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        PropertyValuesHolder[] oldValues = mValues;
1274a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (oldValues != null) {
1275a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            int numValues = oldValues.length;
1276a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            anim.mValues = new PropertyValuesHolder[numValues];
1277a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            anim.mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues);
1278a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 0; i < numValues; ++i) {
1279d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase                PropertyValuesHolder newValuesHolder = oldValues[i].clone();
1280d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase                anim.mValues[i] = newValuesHolder;
1281d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase                anim.mValuesMap.put(newValuesHolder.getPropertyName(), newValuesHolder);
1282a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1283a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1284a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return anim;
1285a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1286a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1287a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1288a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Implementors of this interface can add themselves as update listeners
1289a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * to an <code>ValueAnimator</code> instance to receive callbacks on every animation
1290a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frame, after the current frame's values have been calculated for that
1291a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <code>ValueAnimator</code>.
1292a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1293a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static interface AnimatorUpdateListener {
1294a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        /**
1295a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * <p>Notifies the occurrence of another frame of the animation.</p>
1296a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         *
1297a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * @param animation The animation which was repeated.
1298a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         */
1299a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        void onAnimationUpdate(ValueAnimator animation);
1300a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1301a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1302599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick
1303599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick    /**
1304599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     * Return the number of animations currently running.
1305599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     *
13069c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown     * Used by StrictMode internally to annotate violations.
13079c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown     * May be called on arbitrary threads!
1308599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     *
1309599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     * @hide
1310599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     */
1311599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick    public static int getCurrentAnimationsCount() {
13129c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        AnimationHandler handler = sAnimationHandler.get();
13139c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        return handler != null ? handler.mAnimations.size() : 0;
1314599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick    }
13158901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy
13168901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy    /**
13178901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy     * Clear all animations on this thread, without canceling or ending them.
13188901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy     * This should be used with caution.
13198901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy     *
13208901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy     * @hide
13218901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy     */
13228901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy    public static void clearAllAnimations() {
13239c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        AnimationHandler handler = sAnimationHandler.get();
13249c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        if (handler != null) {
13259c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown            handler.mAnimations.clear();
13269c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown            handler.mPendingAnimations.clear();
13279c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown            handler.mDelayedAnims.clear();
13289c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        }
13299c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown    }
13309c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown
133118772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy    private static AnimationHandler getOrCreateAnimationHandler() {
13329c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        AnimationHandler handler = sAnimationHandler.get();
13339c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        if (handler == null) {
13349c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown            handler = new AnimationHandler();
13359c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown            sAnimationHandler.set(handler);
13369c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        }
13379c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown        return handler;
13388901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy    }
1339e9140a72b1059574046a624b471b2c3a35806496Chet Haase
1340e9140a72b1059574046a624b471b2c3a35806496Chet Haase    @Override
1341e9140a72b1059574046a624b471b2c3a35806496Chet Haase    public String toString() {
1342e9140a72b1059574046a624b471b2c3a35806496Chet Haase        String returnVal = "ValueAnimator@" + Integer.toHexString(hashCode());
1343e9140a72b1059574046a624b471b2c3a35806496Chet Haase        if (mValues != null) {
1344e9140a72b1059574046a624b471b2c3a35806496Chet Haase            for (int i = 0; i < mValues.length; ++i) {
1345e9140a72b1059574046a624b471b2c3a35806496Chet Haase                returnVal += "\n    " + mValues[i].toString();
1346e9140a72b1059574046a624b471b2c3a35806496Chet Haase            }
1347e9140a72b1059574046a624b471b2c3a35806496Chet Haase        }
1348e9140a72b1059574046a624b471b2c3a35806496Chet Haase        return returnVal;
1349e9140a72b1059574046a624b471b2c3a35806496Chet Haase    }
1350599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick}
1351