ValueAnimator.java revision 3dbaae1ef4f221b3626810f4ba19eec068dd6304
1a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase/*
2a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Copyright (C) 2010 The Android Open Source Project
3a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *
4a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Licensed under the Apache License, Version 2.0 (the "License");
5a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * you may not use this file except in compliance with the License.
6a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * You may obtain a copy of the License at
7a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *
8a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *      http://www.apache.org/licenses/LICENSE-2.0
9a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *
10a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Unless required by applicable law or agreed to in writing, software
11a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * distributed under the License is distributed on an "AS IS" BASIS,
12a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * See the License for the specific language governing permissions and
14a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * limitations under the License.
15a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */
16a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
17a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haasepackage android.animation;
18a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
19c615c6fc9caca76cd96998f86e1f1e6393aeadbbTor Norbyeimport android.annotation.CallSuper;
20a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.os.Looper;
2118772ea228f3d292629c4f0b5f6392d047e0530dRomain Guyimport android.os.Trace;
222970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haaseimport android.util.AndroidRuntimeException;
23c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brownimport android.util.Log;
24a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.view.animation.AccelerateDecelerateInterpolator;
25a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.view.animation.AnimationUtils;
2627c1d4debb3848f5accd5673fffeeacad3e61648Chet Haaseimport android.view.animation.LinearInterpolator;
27a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
28a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport java.util.ArrayList;
29a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport java.util.HashMap;
30a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
31a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase/**
32a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This class provides a simple timing engine for running animations
33a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * which calculate animated values and set them on target objects.
34a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *
35a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>There is a single timing pulse that all animations use. It runs in a
36a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * custom handler to ensure that property changes happen on the UI thread.</p>
37a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *
38a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>By default, ValueAnimator uses non-linear time interpolation, via the
39a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link AccelerateDecelerateInterpolator} class, which accelerates into and decelerates
40a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * out of an animation. This behavior can be changed by calling
41e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase * {@link ValueAnimator#setInterpolator(TimeInterpolator)}.</p>
423aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez *
43d430753cba09acb07af8b313286f247c78a41a32Chet Haase * <p>Animators can be created from either code or resource files. Here is an example
44d430753cba09acb07af8b313286f247c78a41a32Chet Haase * of a ValueAnimator resource file:</p>
45d430753cba09acb07af8b313286f247c78a41a32Chet Haase *
46d430753cba09acb07af8b313286f247c78a41a32Chet Haase * {@sample development/samples/ApiDemos/res/anim/animator.xml ValueAnimatorResources}
47d430753cba09acb07af8b313286f247c78a41a32Chet Haase *
48d430753cba09acb07af8b313286f247c78a41a32Chet Haase * <p>It is also possible to use a combination of {@link PropertyValuesHolder} and
49d430753cba09acb07af8b313286f247c78a41a32Chet Haase * {@link Keyframe} resource tags to create a multi-step animation.
50d430753cba09acb07af8b313286f247c78a41a32Chet Haase * Note that you can specify explicit fractional values (from 0 to 1) for
51d430753cba09acb07af8b313286f247c78a41a32Chet Haase * each keyframe to determine when, in the overall duration, the animation should arrive at that
52d430753cba09acb07af8b313286f247c78a41a32Chet Haase * value. Alternatively, you can leave the fractions off and the keyframes will be equally
53d430753cba09acb07af8b313286f247c78a41a32Chet Haase * distributed within the total duration:</p>
54d430753cba09acb07af8b313286f247c78a41a32Chet Haase *
55d430753cba09acb07af8b313286f247c78a41a32Chet Haase * {@sample development/samples/ApiDemos/res/anim/value_animator_pvh_kf.xml
56d430753cba09acb07af8b313286f247c78a41a32Chet Haase * ValueAnimatorKeyframeResources}
57d430753cba09acb07af8b313286f247c78a41a32Chet Haase *
583aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <div class="special reference">
593aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <h3>Developer Guides</h3>
603aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <p>For more information about animating with {@code ValueAnimator}, read the
613aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <a href="{@docRoot}guide/topics/graphics/prop-animation.html#value-animator">Property
623aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * Animation</a> developer guide.</p>
633aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * </div>
64a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */
6518772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy@SuppressWarnings("unchecked")
663618d30f8ab6018025b11869676b309c3b4961cfDoris Liupublic class ValueAnimator extends Animator implements AnimationHandler.AnimationFrameCallback {
67c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    private static final String TAG = "ValueAnimator";
68c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    private static final boolean DEBUG = false;
69a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
70a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
71a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Internal constants
72a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
73d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase    private static float sDurationScale = 1.0f;
74a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
75a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
76a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Internal variables
77a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * NOTE: This object implements the clone() method, making a deep copy of any referenced
78a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * objects. As other non-trivial fields are added to this class, make sure to add logic
79a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * to clone() to make deep copies of them.
80a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
81a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
82c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    /**
83c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * The first time that the animation's animateFrame() method is called. This time is used to
84c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * determine elapsed time (and therefore the elapsed fraction) in subsequent calls
85c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * to animateFrame().
86c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     *
87c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * Whenever mStartTime is set, you must also update mStartTimeCommitted.
88c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     */
89051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase    long mStartTime;
90a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
91a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
92c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * When true, the start time has been firmly committed as a chosen reference point in
93c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * time by which the progress of the animation will be evaluated.  When false, the
94c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * start time may be updated when the first animation frame is committed so as
95c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * to compensate for jank that may have occurred between when the start time was
96c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * initialized and when the frame was actually drawn.
97c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     *
98c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * This flag is generally set to false during the first frame of the animation
99c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * when the animation playing state transitions from STOPPED to RUNNING or
100c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * resumes after having been paused.  This flag is set to true when the start time
101c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * is firmly committed and should not be further compensated for jank.
102c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     */
103c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    boolean mStartTimeCommitted;
104c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown
105c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    /**
106a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Set when setCurrentPlayTime() is called. If negative, animation is not currently seeked
107a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * to a value.
108a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1090d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase    float mSeekFraction = -1;
110a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1118aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    /**
1128aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * Set on the next frame after pause() is called, used to calculate a new startTime
1138aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * or delayStartTime which allows the animator to continue from the point at which
1148aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * it was paused. If negative, has not yet been set.
1158aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     */
1168aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    private long mPauseTime;
1178aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
1188aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    /**
1198aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * Set when an animator is resumed. This triggers logic in the next frame which
1208aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * actually resumes the animator.
1218aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     */
1228aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    private boolean mResumed = false;
1238aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
124a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The time interpolator to be used if none is set on the animation
125e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    private static final TimeInterpolator sDefaultInterpolator =
126e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase            new AccelerateDecelerateInterpolator();
127a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
128a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
129a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Used to indicate whether the animation is currently playing in reverse. This causes the
130a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * elapsed fraction to be inverted to calculate the appropriate values.
131a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
132a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private boolean mPlayingBackwards = false;
133a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
134a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
135f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * Flag to indicate whether this animator is playing in reverse mode, specifically
136f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * by being started or interrupted by a call to reverse(). This flag is different than
137f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * mPlayingBackwards, which indicates merely whether the current iteration of the
138f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * animator is playing in reverse. It is used in corner cases to determine proper end
139f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * behavior.
140f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     */
141f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase    private boolean mReversing;
142f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase
143f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase    /**
144a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This variable tracks the current iteration that is playing. When mCurrentIteration exceeds the
145a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * repeatCount (if repeatCount!=INFINITE), the animation ends
146a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
147a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private int mCurrentIteration = 0;
148a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
149a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
150a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     * Tracks current elapsed/eased fraction, for querying in getAnimatedFraction().
151a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     */
152a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    private float mCurrentFraction = 0f;
153a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase
154a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    /**
1553618d30f8ab6018025b11869676b309c3b4961cfDoris Liu     * Tracks the time (in milliseconds) when the last frame arrived.
156a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1573618d30f8ab6018025b11869676b309c3b4961cfDoris Liu    private long mLastFrameTime = 0;
158a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
159a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
160b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * Additional playing state to indicate whether an animator has been start()'d. There is
161b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * some lag between a call to start() and the first animation frame. We should still note
162b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * that the animation has been started, even if it's first animation frame has not yet
163b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * happened, and reflect that state in isRunning().
164b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * Note that delayed animations are different: they are not started until their first
165b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     * animation frame, which occurs after their delay elapses.
166b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase     */
1678b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    private boolean mRunning = false;
1688b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase
1698b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    /**
1708b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     * Additional playing state to indicate whether an animator has been start()'d, whether or
1718b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     * not there is a nonzero startDelay.
1728b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase     */
173b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase    private boolean mStarted = false;
174b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase
175b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase    /**
1768aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase     * Tracks whether we've notified listeners of the onAnimationStart() event. This can be
17717cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase     * complex to keep track of since we notify listeners at different times depending on
17817cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase     * startDelay and whether start() was called before end().
17917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase     */
18017cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase    private boolean mStartListenersCalled = false;
18117cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase
18217cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase    /**
183a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Flag that denotes whether the animation is set up and ready to go. Used to
184a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * set up animation that has not yet been started.
185a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
186a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    boolean mInitialized = false;
187a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1883dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu    /**
1893dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu     * Flag that tracks whether animation has been requested to end.
1903dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu     */
1913dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu    private boolean mAnimationEndRequested = false;
1923dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu
193a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    //
194a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // Backing variables
195a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    //
196a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
197a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // How long the animation should last in ms
198d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase    private long mUnscaledDuration = 300;
1993618d30f8ab6018025b11869676b309c3b4961cfDoris Liu    private long mDuration = (long)(mUnscaledDuration * sDurationScale);
200a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
201a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The amount of time in ms to delay starting the animation after start() is called
2023618d30f8ab6018025b11869676b309c3b4961cfDoris Liu    long mStartDelay = 0;
203d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase    private long mUnscaledStartDelay = 0;
204a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
205a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The number of times the animation will repeat. The default is 0, which means the animation
206a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // will play only once
207a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private int mRepeatCount = 0;
208a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
209a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
210a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The type of repetition that will occur when repeatMode is nonzero. RESTART means the
211a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation will start from the beginning on every new cycle. REVERSE means the animation
212a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will reverse directions on each iteration.
213a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
214a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private int mRepeatMode = RESTART;
215a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
216a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
217a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The time interpolator to be used. The elapsed fraction of the animation will be passed
218a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * through this interpolator to calculate the interpolated fraction, which is then used to
219a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * calculate the animated values.
220a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
221e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    private TimeInterpolator mInterpolator = sDefaultInterpolator;
222a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
223a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
224a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The set of listeners to be sent events through the life of an animation.
225a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
226d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck    ArrayList<AnimatorUpdateListener> mUpdateListeners = null;
227a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
228a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
229a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The property/value sets being animated.
230a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
231a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    PropertyValuesHolder[] mValues;
232a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
233a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
234a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * A hashmap of the PropertyValuesHolder objects. This map is used to lookup animated values
235a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * by property name during calls to getAnimatedValue(String).
236a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
237a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    HashMap<String, PropertyValuesHolder> mValuesMap;
238a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
239a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
240a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Public constants
241a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
242a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
243a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
244a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * When the animation reaches the end and <code>repeatCount</code> is INFINITE
245a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * or a positive value, the animation restarts from the beginning.
246a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
247a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static final int RESTART = 1;
248a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
249a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * When the animation reaches the end and <code>repeatCount</code> is INFINITE
250a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * or a positive value, the animation reverses direction on every iteration.
251a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
252a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static final int REVERSE = 2;
253a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
254a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This value used used with the {@link #setRepeatCount(int)} property to repeat
255a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the animation indefinitely.
256a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
257a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static final int INFINITE = -1;
258a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
259c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase    /**
260c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase     * @hide
261c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase     */
262c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase    public static void setDurationScale(float durationScale) {
263c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase        sDurationScale = durationScale;
264c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase    }
265c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase
266a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
267ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown     * @hide
268ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown     */
269ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown    public static float getDurationScale() {
270ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown        return sDurationScale;
271ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown    }
272ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown
273ff7e6ef4f18ff94a9836492ff3ccd1ba7f6804f3Jeff Brown    /**
274a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Creates a new ValueAnimator object. This default constructor is primarily for
2752794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * use internally; the factory methods which take parameters are more generally
276a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * useful.
277a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
278a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public ValueAnimator() {
279a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
280a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
281a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
2822794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between int values. A single
2832794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
2842794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
2852794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
2862794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
2872794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
288a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
2892794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
2902794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
29141f041d9986f8a5d45b6cb0b86e881c81a412168Chet Haase     */
2922794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofInt(int... values) {
2932794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
2942794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setIntValues(values);
2952794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
2962794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
2972794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
2982794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
2991ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * Constructs and returns a ValueAnimator that animates between color values. A single
3001ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * value implies that that value is the one being animated to. However, this is not typically
3011ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * useful in a ValueAnimator object because there is no way for the object to determine the
3021ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * starting value for the animation (unlike ObjectAnimator, which can derive that value
3031ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * from the target object and property being animated). Therefore, there should typically
3041ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * be two or more values.
3051ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     *
3061ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * @param values A set of values that the animation will animate between over time.
3071ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     * @return A ValueAnimator object that is set up to animate between the given values.
3081ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount     */
3091ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount    public static ValueAnimator ofArgb(int... values) {
3101ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount        ValueAnimator anim = new ValueAnimator();
3111ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount        anim.setIntValues(values);
3121ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount        anim.setEvaluator(ArgbEvaluator.getInstance());
3131ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount        return anim;
3141ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount    }
3151ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount
3161ffb280a7d2c70cc16d709c685f5d31fdb86b5e4George Mount    /**
3172794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between float values. A single
3182794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
3192794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
3202794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
3212794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
3222794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
3232794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3242794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
3252794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
3262794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
3272794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofFloat(float... values) {
3282794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
3292794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setFloatValues(values);
3302794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
3312794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
3322794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
3332794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
3342794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between the values
3352794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * specified in the PropertyValuesHolder objects.
3362794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3372794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of PropertyValuesHolder objects whose values will be animated
3382794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * between over time.
3392794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
3402794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
3412794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofPropertyValuesHolder(PropertyValuesHolder... values) {
3422794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
3432794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setValues(values);
3442794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
3452794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
3462794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
3472794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between Object values. A single
3482794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
3492794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
3502794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
3512794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
3522794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
3532794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3542794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>Since ValueAnimator does not know how to animate between arbitrary Objects, this
3552794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * factory method also takes a TypeEvaluator object that the ValueAnimator will use
3562794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * to perform that interpolation.
3572794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3582794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param evaluator A TypeEvaluator that will be called on each animation frame to
3592794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * provide the ncessry interpolation between the Object values to derive the animated
3602794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value.
3612794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
3622794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
3632794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
3642794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofObject(TypeEvaluator evaluator, Object... values) {
3652794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
3662794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setObjectValues(values);
3672794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setEvaluator(evaluator);
3682794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
3692794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
3702794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
3712794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
3722794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Sets int values that will be animated between. A single
3732794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
3742794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
3752794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
3762794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
3772794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
3782794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3792794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>If there are already multiple sets of values defined for this ValueAnimator via more
3802794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * than one PropertyValuesHolder object, this method will set the values for the first
3812794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * of those objects.</p>
3822794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3832794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
3842794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
3852794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public void setIntValues(int... values) {
3862794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (values == null || values.length == 0) {
3872794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            return;
3882794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
3892794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (mValues == null || mValues.length == 0) {
39018772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy            setValues(PropertyValuesHolder.ofInt("", values));
3912794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        } else {
3922794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            PropertyValuesHolder valuesHolder = mValues[0];
3932794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            valuesHolder.setIntValues(values);
3942794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
3952794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        // New property/values/target should cause re-initialization prior to starting
3962794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        mInitialized = false;
3972794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
3982794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
3992794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
4002794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Sets float values that will be animated between. A single
4012794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
4022794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
4032794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
4042794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
4052794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
4062794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4072794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>If there are already multiple sets of values defined for this ValueAnimator via more
4082794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * than one PropertyValuesHolder object, this method will set the values for the first
4092794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * of those objects.</p>
4102794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4112794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
4122794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
4132794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public void setFloatValues(float... values) {
4142794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (values == null || values.length == 0) {
4152794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            return;
41641f041d9986f8a5d45b6cb0b86e881c81a412168Chet Haase        }
4172794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (mValues == null || mValues.length == 0) {
41818772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy            setValues(PropertyValuesHolder.ofFloat("", values));
4192794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        } else {
4202794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            PropertyValuesHolder valuesHolder = mValues[0];
4212794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            valuesHolder.setFloatValues(values);
4222794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
4232794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        // New property/values/target should cause re-initialization prior to starting
4242794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        mInitialized = false;
4252794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
4262794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
4272794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
4282794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Sets the values to animate between for this animation. A single
4292794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
4302794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
4312794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
4322794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
4332794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
4342794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4352794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>If there are already multiple sets of values defined for this ValueAnimator via more
4362794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * than one PropertyValuesHolder object, this method will set the values for the first
4372794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * of those objects.</p>
4382794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4392794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>There should be a TypeEvaluator set on the ValueAnimator that knows how to interpolate
4402794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * between these value objects. ValueAnimator only knows how to interpolate between the
4412794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * primitive types specified in the other setValues() methods.</p>
4422794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4432794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values The set of values to animate between.
4442794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
4452794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public void setObjectValues(Object... values) {
4462794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (values == null || values.length == 0) {
4472794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            return;
4482794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
4492794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (mValues == null || mValues.length == 0) {
45018772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy            setValues(PropertyValuesHolder.ofObject("", null, values));
4512794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        } else {
4522794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            PropertyValuesHolder valuesHolder = mValues[0];
4532794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            valuesHolder.setObjectValues(values);
4542794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
4552794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        // New property/values/target should cause re-initialization prior to starting
4562794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        mInitialized = false;
457a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
458a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
459a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
460a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets the values, per property, being animated between. This function is called internally
461f76a50ce8fdc6aea22cabc77b2977a1a15a79630Ken Wakasa     * by the constructors of ValueAnimator that take a list of values. But a ValueAnimator can
462a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * be constructed without values and this method can be called to set the values manually
463a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * instead.
464a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
465a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param values The set of values, per property, being animated between.
466a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
467a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setValues(PropertyValuesHolder... values) {
468a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        int numValues = values.length;
469a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mValues = values;
470a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues);
471a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        for (int i = 0; i < numValues; ++i) {
47218772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy            PropertyValuesHolder valuesHolder = values[i];
473a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mValuesMap.put(valuesHolder.getPropertyName(), valuesHolder);
474a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
4750e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase        // New property/values/target should cause re-initialization prior to starting
4760e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase        mInitialized = false;
477a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
478a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
479a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
480a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Returns the values that this ValueAnimator animates between. These values are stored in
481a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * PropertyValuesHolder objects, even if the ValueAnimator was created with a simple list
482a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of value objects instead.
483a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
484a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return PropertyValuesHolder[] An array of PropertyValuesHolder objects which hold the
485a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * values, per property, that define the animation.
486a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
487a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public PropertyValuesHolder[] getValues() {
488a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mValues;
489a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
490a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
491a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
492a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This function is called immediately before processing the first animation
493a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frame of an animation. If there is a nonzero <code>startDelay</code>, the
494a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function is called after that delay ends.
495a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * It takes care of the final initialization steps for the
496a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation.
497a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
498a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *  <p>Overrides of this method should call the superclass method to ensure
499a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *  that internal mechanisms for the animation are set up correctly.</p>
500a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
501c615c6fc9caca76cd96998f86e1f1e6393aeadbbTor Norbye    @CallSuper
502a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    void initAnimation() {
503a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (!mInitialized) {
504a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            int numValues = mValues.length;
505a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 0; i < numValues; ++i) {
506a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mValues[i].init();
507a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
508a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mInitialized = true;
509a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
510a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
511a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
512a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
5132794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Sets the length of the animation. The default duration is 300 milliseconds.
514a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
51527c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase     * @param duration The length of the animation, in milliseconds. This value cannot
51627c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase     * be negative.
5172794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return ValueAnimator The object called with setDuration(). This return
5182794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value makes it easier to compose statements together that construct and then set the
5192794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * duration, as in <code>ValueAnimator.ofInt(0, 10).setDuration(500).start()</code>.
520a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
521c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    @Override
5222794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public ValueAnimator setDuration(long duration) {
52327c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase        if (duration < 0) {
52427c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase            throw new IllegalArgumentException("Animators cannot have negative duration: " +
52527c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase                    duration);
52627c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase        }
527d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase        mUnscaledDuration = duration;
5287a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown        updateScaledDuration();
5292794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return this;
530a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
531a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
5327a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown    private void updateScaledDuration() {
5337eaccbfbc25b772659536c096f6341927c38c470Doris Liu        mDuration = (long)(mUnscaledDuration * sDurationScale);
5347a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown    }
5357a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown
536a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
5372794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Gets the length of the animation. The default duration is 300 milliseconds.
538a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
539a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return The length of the animation, in milliseconds.
540a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
541c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    @Override
542a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public long getDuration() {
543d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase        return mUnscaledDuration;
544a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
545a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
546a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
5471309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu     * @hide
5481309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu     */
5491309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu    @Override
5501309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu    public long getTotalDuration() {
5511309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu        if (mRepeatCount == INFINITE) {
5521309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu            return DURATION_INFINITE;
5531309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu        } else {
5541309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu            return mUnscaledStartDelay + (mUnscaledDuration * (mRepeatCount + 1));
5551309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu        }
5561309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu    }
5571309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu
5581309914ebf21e705fc59d7d44014124d8a21a2d2Doris Liu    /**
559a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets the position of the animation to the specified point in time. This time should
560a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * be between 0 and the total duration of the animation, including any repetition. If
561a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the animation has not yet been started, then it will not advance forward after it is
562a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * set to this time; it will simply set the time to this value and perform any appropriate
563a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * actions based on that time. If the animation is already running, then setCurrentPlayTime()
564a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will set the current playing time to this value and continue playing from that point.
565a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
566a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param playTime The time, in milliseconds, to which the animation is advanced or rewound.
567a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
568a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setCurrentPlayTime(long playTime) {
5695a25e5bae223bbee56dab75e36d1d947c8c3cb11Chet Haase        float fraction = mUnscaledDuration > 0 ? (float) playTime / mUnscaledDuration : 1;
5700d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase        setCurrentFraction(fraction);
5710d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase    }
5720d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase
5730d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase    /**
5740d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * Sets the position of the animation to the specified fraction. This fraction should
5750d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * be between 0 and the total fraction of the animation, including any repetition. That is,
5760d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * a fraction of 0 will position the animation at the beginning, a value of 1 at the end,
577f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * and a value of 2 at the end of a reversing animator that repeats once. If
5780d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * the animation has not yet been started, then it will not advance forward after it is
5790d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * set to this fraction; it will simply set the fraction to this value and perform any
5800d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * appropriate actions based on that fraction. If the animation is already running, then
5810d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     * setCurrentFraction() will set the current fraction to this value and continue
5825a5afe010d2f8fb7e8f00858b8a305b4745c0469Eino-Ville Talvala     * playing from that point. {@link Animator.AnimatorListener} events are not called
583f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * due to changing the fraction; those events are only processed while the animation
584f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * is running.
5850d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     *
586f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * @param fraction The fraction to which the animation is advanced or rewound. Values
587f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * outside the range of 0 to the maximum fraction for the animator will be clamped to
588f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase     * the correct range.
5890d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase     */
5900d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase    public void setCurrentFraction(float fraction) {
591a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        initAnimation();
592f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        if (fraction < 0) {
593f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            fraction = 0;
594f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        }
595f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        int iteration = (int) fraction;
596f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        if (fraction == 1) {
597f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            iteration -= 1;
598f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        } else if (fraction > 1) {
599f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            if (iteration < (mRepeatCount + 1) || mRepeatCount == INFINITE) {
600f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                if (mRepeatMode == REVERSE) {
601f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                    mPlayingBackwards = (iteration % 2) != 0;
602f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                }
603f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                fraction = fraction % 1f;
604f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            } else {
605f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                fraction = 1;
606f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                iteration -= 1;
607f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            }
608f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        } else {
609f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            mPlayingBackwards = mReversing;
610f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        }
611f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        mCurrentIteration = iteration;
612f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        long seekTime = (long) (mDuration * fraction);
613f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        long currentTime = AnimationUtils.currentAnimationTimeMillis();
614f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        mStartTime = currentTime - seekTime;
615c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown        mStartTimeCommitted = true; // do not allow start time to be compensated for jank
6163618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        if (!mRunning) {
6170d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase            mSeekFraction = fraction;
618a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
619f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        if (mPlayingBackwards) {
620f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            fraction = 1f - fraction;
621f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        }
6220d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase        animateValue(fraction);
623a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
624a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
625a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
626a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Gets the current position of the animation in time, which is equal to the current
627a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * time minus the time that the animation started. An animation that is not yet started will
628a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * return a value of zero.
629a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
630a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return The current position in time of the animation.
631a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
632a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public long getCurrentPlayTime() {
6333618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        if (!mInitialized || !mStarted) {
634a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return 0;
635a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
636a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return AnimationUtils.currentAnimationTimeMillis() - mStartTime;
637a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
638a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
639a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
640a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, to delay starting the animation after
641a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link #start()} is called.
642a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
643a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return the number of milliseconds to delay running the animation
644a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
645c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    @Override
646a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public long getStartDelay() {
647d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase        return mUnscaledStartDelay;
648a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
649a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
650a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
651a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, to delay starting the animation after
652a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link #start()} is called.
653a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
654a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param startDelay The amount of the delay, in milliseconds
655a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
656c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    @Override
657a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setStartDelay(long startDelay) {
658d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase        this.mStartDelay = (long)(startDelay * sDurationScale);
659d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase        mUnscaledStartDelay = startDelay;
660a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
661a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
662a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
663a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, between each frame of the animation. This is a
664a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * requested time that the animation will attempt to honor, but the actual delay between
665a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frames may be different, depending on system load and capabilities. This is a static
666a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function because the same delay will be applied to all animations, since they are all
667a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * run off of a single timing loop.
668a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
66996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     * The frame delay may be ignored when the animation system uses an external timing
67096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     * source, such as the display refresh rate (vsync), to govern animations.
67196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     *
672a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return the requested time between frames, in milliseconds
673a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
674a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static long getFrameDelay() {
6753618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        return AnimationHandler.getInstance().getFrameDelay();
676a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
677a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
678a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
679a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, between each frame of the animation. This is a
680a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * requested time that the animation will attempt to honor, but the actual delay between
681a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frames may be different, depending on system load and capabilities. This is a static
682a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function because the same delay will be applied to all animations, since they are all
683a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * run off of a single timing loop.
684a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
68596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     * The frame delay may be ignored when the animation system uses an external timing
68696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     * source, such as the display refresh rate (vsync), to govern animations.
68796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown     *
688a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param frameDelay the requested time between frames, in milliseconds
689a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
690a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static void setFrameDelay(long frameDelay) {
6913618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        AnimationHandler.getInstance().setFrameDelay(frameDelay);
692a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
693a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
694a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
695a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The most recent value calculated by this <code>ValueAnimator</code> when there is just one
696a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * property being animated. This value is only sensible while the animation is running. The main
697a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * purpose for this read-only property is to retrieve the value from the <code>ValueAnimator</code>
698a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * during a call to {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which
699a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is called during each animation frame, immediately after the value is calculated.
700a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
701a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return animatedValue The value most recently calculated by this <code>ValueAnimator</code> for
702a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the single property being animated. If there are several properties being animated
703a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * (specified by several PropertyValuesHolder objects in the constructor), this function
704a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * returns the animated value for the first of those objects.
705a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
706a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public Object getAnimatedValue() {
707a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mValues != null && mValues.length > 0) {
708a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return mValues[0].getAnimatedValue();
709a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
710a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        // Shouldn't get here; should always have values unless ValueAnimator was set up wrong
711a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return null;
712a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
713a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
714a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
715a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The most recent value calculated by this <code>ValueAnimator</code> for <code>propertyName</code>.
716a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The main purpose for this read-only property is to retrieve the value from the
717a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <code>ValueAnimator</code> during a call to
718a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which
719a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is called during each animation frame, immediately after the value is calculated.
720a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
721a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return animatedValue The value most recently calculated for the named property
722a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * by this <code>ValueAnimator</code>.
723a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
724a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public Object getAnimatedValue(String propertyName) {
725a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        PropertyValuesHolder valuesHolder = mValuesMap.get(propertyName);
726a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (valuesHolder != null) {
727a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return valuesHolder.getAnimatedValue();
728a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        } else {
729a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // At least avoid crashing if called with bogus propertyName
730a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return null;
731a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
732a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
733a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
734a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
735a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets how many times the animation should be repeated. If the repeat
736a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * count is 0, the animation is never repeated. If the repeat count is
737a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * greater than 0 or {@link #INFINITE}, the repeat mode will be taken
738a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * into account. The repeat count is 0 by default.
739a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
740a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param value the number of times the animation should be repeated
741a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
742a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setRepeatCount(int value) {
743a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mRepeatCount = value;
744a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
745a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
746a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Defines how many times the animation should repeat. The default value
747a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is 0.
748a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
749a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return the number of times the animation should repeat, or {@link #INFINITE}
750a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
751a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public int getRepeatCount() {
752a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mRepeatCount;
753a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
754a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
755a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
756a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Defines what this animation should do when it reaches the end. This
757a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * setting is applied only when the repeat count is either greater than
758a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * 0 or {@link #INFINITE}. Defaults to {@link #RESTART}.
759a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
760a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param value {@link #RESTART} or {@link #REVERSE}
761a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
762a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setRepeatMode(int value) {
763a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mRepeatMode = value;
764a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
765a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
766a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
767a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Defines what this animation should do when it reaches the end.
768a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
769a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return either one of {@link #REVERSE} or {@link #RESTART}
770a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
771a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public int getRepeatMode() {
772a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mRepeatMode;
773a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
774a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
775a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
776a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Adds a listener to the set of listeners that are sent update events through the life of
777a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * an animation. This method is called on all listeners for every frame of the animation,
778a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * after the values for the animation have been calculated.
779a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
780a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param listener the listener to be added to the current set of listeners for this animation.
781a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
782a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void addUpdateListener(AnimatorUpdateListener listener) {
783a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners == null) {
784a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mUpdateListeners = new ArrayList<AnimatorUpdateListener>();
785a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
786a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mUpdateListeners.add(listener);
787a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
788a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
789a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
7903060421045d4d9e411797f91bb509824b03e33fbJim Miller     * Removes all listeners from the set listening to frame updates for this animation.
7913060421045d4d9e411797f91bb509824b03e33fbJim Miller     */
7923060421045d4d9e411797f91bb509824b03e33fbJim Miller    public void removeAllUpdateListeners() {
7933060421045d4d9e411797f91bb509824b03e33fbJim Miller        if (mUpdateListeners == null) {
7943060421045d4d9e411797f91bb509824b03e33fbJim Miller            return;
7953060421045d4d9e411797f91bb509824b03e33fbJim Miller        }
7963060421045d4d9e411797f91bb509824b03e33fbJim Miller        mUpdateListeners.clear();
7973060421045d4d9e411797f91bb509824b03e33fbJim Miller        mUpdateListeners = null;
7983060421045d4d9e411797f91bb509824b03e33fbJim Miller    }
7993060421045d4d9e411797f91bb509824b03e33fbJim Miller
8003060421045d4d9e411797f91bb509824b03e33fbJim Miller    /**
801a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Removes a listener from the set listening to frame updates for this animation.
802a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
803a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param listener the listener to be removed from the current set of update listeners
804a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * for this animation.
805a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
806a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void removeUpdateListener(AnimatorUpdateListener listener) {
807a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners == null) {
808a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return;
809a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
810a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mUpdateListeners.remove(listener);
811a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners.size() == 0) {
812a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mUpdateListeners = null;
813a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
814a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
815a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
816a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
817a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
818a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The time interpolator used in calculating the elapsed fraction of this animation. The
819a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * interpolator determines whether the animation runs with linear or non-linear motion,
820a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * such as acceleration and deceleration. The default value is
821a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link android.view.animation.AccelerateDecelerateInterpolator}
822a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
82327c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase     * @param value the interpolator to be used by this animation. A value of <code>null</code>
82427c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase     * will result in linear interpolation.
825a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
826a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
827e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    public void setInterpolator(TimeInterpolator value) {
828a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (value != null) {
829a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mInterpolator = value;
83027c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase        } else {
83127c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase            mInterpolator = new LinearInterpolator();
832a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
833a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
834a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
835a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
836a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Returns the timing interpolator that this ValueAnimator uses.
837a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
838a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return The timing interpolator for this ValueAnimator.
839a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
840430742f09063574271e6c4091de13b9b9e762514Chet Haase    @Override
841e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    public TimeInterpolator getInterpolator() {
842a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mInterpolator;
843a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
844a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
845a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
846a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The type evaluator to be used when calculating the animated values of this animation.
847b2ab04ffb6894f399d5c9ceb15f64eb17b654426Chet Haase     * The system will automatically assign a float or int evaluator based on the type
848a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of <code>startValue</code> and <code>endValue</code> in the constructor. But if these values
849a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * are not one of these primitive types, or if different evaluation is desired (such as is
850a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * necessary with int values that represent colors), a custom evaluator needs to be assigned.
85153ee3316bcb3590ff156b3fd7108903c0817c35dChet Haase     * For example, when running an animation on color values, the {@link ArgbEvaluator}
852a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * should be used to get correct RGB color interpolation.
853a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
854a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>If this ValueAnimator has only one set of values being animated between, this evaluator
855a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will be used for that set. If there are several sets of values being animated, which is
856fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase     * the case if PropertyValuesHolder objects were set on the ValueAnimator, then the evaluator
857a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is assigned just to the first PropertyValuesHolder object.</p>
858a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
859a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param value the evaluator to be used this animation
860a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
861a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setEvaluator(TypeEvaluator value) {
862a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (value != null && mValues != null && mValues.length > 0) {
863a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mValues[0].setEvaluator(value);
864a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
865a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
866a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
86717cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase    private void notifyStartListeners() {
86817cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        if (mListeners != null && !mStartListenersCalled) {
86917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            ArrayList<AnimatorListener> tmpListeners =
87017cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                    (ArrayList<AnimatorListener>) mListeners.clone();
87117cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            int numListeners = tmpListeners.size();
87217cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            for (int i = 0; i < numListeners; ++i) {
87317cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                tmpListeners.get(i).onAnimationStart(this);
87417cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            }
87517cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        }
87617cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        mStartListenersCalled = true;
87717cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase    }
87817cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase
879a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
880a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Start the animation playing. This version of start() takes a boolean flag that indicates
881a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * whether the animation should play in reverse. The flag is usually false, but may be set
8822970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * to true if called from the reverse() method.
8832970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     *
8842970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * <p>The animation started by calling this method will be run on the thread that called
8852970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * this method. This thread should have a Looper on it (a runtime exception will be thrown if
8862970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * this is not the case). Also, if the animation will animate
8872970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * properties of objects in the view hierarchy, then the calling thread should be the UI
8882970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase     * thread for that view hierarchy.</p>
889a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
890a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param playBackwards Whether the ValueAnimator should start playing in reverse.
891a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
892a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private void start(boolean playBackwards) {
8932970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        if (Looper.myLooper() == null) {
8942970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase            throw new AndroidRuntimeException("Animators may only be run on Looper threads");
8953060421045d4d9e411797f91bb509824b03e33fbJim Miller        }
896f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        mReversing = playBackwards;
8972970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        mPlayingBackwards = playBackwards;
898f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        if (playBackwards && mSeekFraction != -1) {
899f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            if (mSeekFraction == 0 && mCurrentIteration == 0) {
900f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                // special case: reversing from seek-to-0 should act as if not seeked at all
901f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                mSeekFraction = 0;
902f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            } else if (mRepeatCount == INFINITE) {
903f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                mSeekFraction = 1 - (mSeekFraction % 1);
904f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            } else {
905f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                mSeekFraction = 1 + mRepeatCount - (mCurrentIteration + mSeekFraction);
906f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            }
907f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            mCurrentIteration = (int) mSeekFraction;
908f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            mSeekFraction = mSeekFraction % 1;
909f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        }
910f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        if (mCurrentIteration > 0 && mRepeatMode == REVERSE &&
911f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                (mCurrentIteration < (mRepeatCount + 1) || mRepeatCount == INFINITE)) {
912f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            // if we were seeked to some other iteration in a reversing animator,
913f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            // figure out the correct direction to start playing based on the iteration
914f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            if (playBackwards) {
915f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                mPlayingBackwards = (mCurrentIteration % 2) == 0;
916f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            } else {
917f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                mPlayingBackwards = (mCurrentIteration % 2) != 0;
918f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            }
919f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        }
9208b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        mStarted = true;
9218aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        mPaused = false;
9223618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        mRunning = false;
9233dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu        mAnimationEndRequested = false;
9247a08fe0e09c9bd5b66049738617cc9972651bf5bJeff Brown        updateScaledDuration(); // in case the scale factor has changed since creation time
9253618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        AnimationHandler animationHandler = AnimationHandler.getInstance();
9263618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        animationHandler.addAnimationFrameCallback(this, mStartDelay);
927a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
928a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
929a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
930a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void start() {
931a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        start(false);
932a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
933a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
934a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
935a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void cancel() {
9363618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        if (Looper.myLooper() == null) {
9373618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            throw new AndroidRuntimeException("Animators may only be run on Looper threads");
9383618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        }
9393dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu
9403dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu        // If end has already been requested, through a previous end() or cancel() call, no-op
9413dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu        // until animation starts again.
9423dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu        if (mAnimationEndRequested) {
9433dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu            return;
9443dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu        }
9453dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu
9462970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        // Only cancel if the animation is actually running or has been started and is about
9472970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        // to run
9483618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        // Only notify listeners if the animator has actually started
9493618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        if ((mStarted || mRunning) && mListeners != null) {
9503618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            if (!mRunning) {
9513618d30f8ab6018025b11869676b309c3b4961cfDoris Liu                // If it's not yet running, then start listeners weren't called. Call them now.
9523618d30f8ab6018025b11869676b309c3b4961cfDoris Liu                notifyStartListeners();
9533618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            }
9543618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            ArrayList<AnimatorListener> tmpListeners =
9553618d30f8ab6018025b11869676b309c3b4961cfDoris Liu                    (ArrayList<AnimatorListener>) mListeners.clone();
9563618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            for (AnimatorListener listener : tmpListeners) {
9573618d30f8ab6018025b11869676b309c3b4961cfDoris Liu                listener.onAnimationCancel(this);
9587dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase            }
9592970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase        }
9603618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        endAnimation();
9613618d30f8ab6018025b11869676b309c3b4961cfDoris Liu
962a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
963a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
964a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
965a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void end() {
9663618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        if (Looper.myLooper() == null) {
9673618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            throw new AndroidRuntimeException("Animators may only be run on Looper threads");
9683618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        }
9693618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        if (!mRunning) {
970a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // Special case if the animation has not yet started; get it ready for ending
9713618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            startAnimation();
97217cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            mStarted = true;
973add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase        } else if (!mInitialized) {
974add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase            initAnimation();
975e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase        }
9764dd176864310e1d9519bf6b88918913e9927984fChet Haase        animateValue(mPlayingBackwards ? 0f : 1f);
9773618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        endAnimation();
978a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
979a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
980a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
9818aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    public void resume() {
9828aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        if (mPaused) {
9838aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            mResumed = true;
9848aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        }
9858aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        super.resume();
9868aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    }
9878aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
9888aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    @Override
9898aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    public void pause() {
9908aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        boolean previouslyPaused = mPaused;
9918aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        super.pause();
9928aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        if (!previouslyPaused && mPaused) {
9938aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            mPauseTime = -1;
9948aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            mResumed = false;
9958aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        }
9968aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    }
9978aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase
9988aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase    @Override
999a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public boolean isRunning() {
10003618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        return mRunning;
10018b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    }
10028b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase
10038b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    @Override
10048b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase    public boolean isStarted() {
10058b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        return mStarted;
1006a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1007a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1008a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1009a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Plays the ValueAnimator in reverse. If the animation is already running,
1010a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * it will stop itself and play backwards from the point reached when reverse was called.
1011a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * If the animation is not currently running, then it will start from the end and
1012a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * play backwards. This behavior is only set for the current animation; future playing
1013a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of the animation will use the default behavior of playing forward.
1014a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
10157bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui    @Override
1016a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void reverse() {
1017a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mPlayingBackwards = !mPlayingBackwards;
10183618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        if (mRunning) {
1019a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            long currentTime = AnimationUtils.currentAnimationTimeMillis();
1020a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            long currentPlayTime = currentTime - mStartTime;
1021a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            long timeLeft = mDuration - currentPlayTime;
1022a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mStartTime = currentTime - timeLeft;
1023c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            mStartTimeCommitted = true; // do not allow start time to be compensated for jank
1024f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            mReversing = !mReversing;
1025f43fb2a57f152b5760d8792fec26f36d46b23817Chet Haase        } else if (mStarted) {
1026f43fb2a57f152b5760d8792fec26f36d46b23817Chet Haase            end();
1027a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        } else {
1028a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            start(true);
1029a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1030a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1031a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1032a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
10337bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui     * @hide
10347bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui     */
10357bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui    @Override
10367bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui    public boolean canReverse() {
10377bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui        return true;
10387bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui    }
10397bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui
10407bc6a3f023ca3e1dde91fc97b6036dee3ba538a2ztenghui    /**
1041a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Called internally to end an animation by removing it from the animations list. Must be
1042a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * called on the UI thread.
1043a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
10443dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu    private void endAnimation() {
10453dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu        if (mAnimationEndRequested) {
10463dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu            return;
10473dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu        }
10483618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        AnimationHandler handler = AnimationHandler.getInstance();
10493618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        handler.removeCallback(this);
10503dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu
10513dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu        mAnimationEndRequested = true;
10528aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        mPaused = false;
105317cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        if ((mStarted || mRunning) && mListeners != null) {
105417cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            if (!mRunning) {
105517cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                // If it's not yet running, then start listeners weren't called. Call them now.
105617cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase                notifyStartListeners();
105717cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase             }
1058a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            ArrayList<AnimatorListener> tmpListeners =
1059a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    (ArrayList<AnimatorListener>) mListeners.clone();
10607c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            int numListeners = tmpListeners.size();
10617c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            for (int i = 0; i < numListeners; ++i) {
10627c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                tmpListeners.get(i).onAnimationEnd(this);
1063a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1064a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
10658b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase        mRunning = false;
1066b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase        mStarted = false;
106717cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase        mStartListenersCalled = false;
1068caf46486f54cdc899e383dfc776ec33a81b089a1Chet Haase        mPlayingBackwards = false;
1069f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        mReversing = false;
1070f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        mCurrentIteration = 0;
10719b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase        if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
10729b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase            Trace.asyncTraceEnd(Trace.TRACE_TAG_VIEW, getNameForTrace(),
10739b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase                    System.identityHashCode(this));
10749b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase        }
1075a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1076a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1077a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1078a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Called internally to start an animation by adding it to the active animations list. Must be
1079a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * called on the UI thread.
1080a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
10813618d30f8ab6018025b11869676b309c3b4961cfDoris Liu    private void startAnimation() {
10829b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase        if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
10839b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase            Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW, getNameForTrace(),
10849b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase                    System.identityHashCode(this));
10859b80ca81a8cc33067d858c52c7acbef9d8bdaf6aChet Haase        }
1086a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        initAnimation();
10873618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        mRunning = true;
10883618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        if (mListeners != null) {
108917cf42cb85c22b50ecfa8d21efc992f99d20fc45Chet Haase            notifyStartListeners();
1090a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1091a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1092a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1093a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1094fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase     * Returns the name of this animator for debugging purposes.
1095fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase     */
1096fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase    String getNameForTrace() {
1097fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase        return "animator";
1098fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase    }
1099fdd3ad7018ebb054c0288b8cd92739703a973181Chet Haase
1100a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1101c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * Applies an adjustment to the animation to compensate for jank between when
1102c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     * the animation first ran and when the frame was drawn.
11033618d30f8ab6018025b11869676b309c3b4961cfDoris Liu     * @hide
1104c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown     */
11053618d30f8ab6018025b11869676b309c3b4961cfDoris Liu    public void commitAnimationFrame(long frameTime) {
1106c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown        if (!mStartTimeCommitted) {
1107c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            mStartTimeCommitted = true;
11083618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            long adjustment = frameTime - mLastFrameTime;
11093618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            if (adjustment > 0) {
1110c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                mStartTime += adjustment;
1111c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                if (DEBUG) {
1112c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                    Log.d(TAG, "Adjusted start time by " + adjustment + " ms: " + toString());
1113c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                }
1114c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            }
1115c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown        }
1116c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    }
1117c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown
1118c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown    /**
1119a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This internal function processes a single animation frame for a given animation. The
1120a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * currentTime parameter is the timing pulse sent by the handler, used to calculate the
1121a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * elapsed duration, and therefore
1122a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the elapsed fraction, of the animation. The return value indicates whether the animation
1123a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * should be ended (which happens when the elapsed time of the animation exceeds the
1124a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation's duration, including the repeatCount).
1125a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1126a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param currentTime The current time, as tracked by the static timing handler
1127a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return true if the animation's duration, including any repetitions due to
11283618d30f8ab6018025b11869676b309c3b4961cfDoris Liu     * <code>repeatCount</code> has been exceeded and the animation should be ended.
1129a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
11303618d30f8ab6018025b11869676b309c3b4961cfDoris Liu    boolean animateBasedOnTime(long currentTime) {
1131a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        boolean done = false;
11323618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        if (mRunning) {
113370d4ba15b1f0c1133c5aabc86de828b41e482fffChet Haase            float fraction = mDuration > 0 ? (float)(currentTime - mStartTime) / mDuration : 1f;
1134f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            if (mDuration == 0 && mRepeatCount != INFINITE) {
1135f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                // Skip to the end
1136f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                mCurrentIteration = mRepeatCount;
1137f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                if (!mReversing) {
1138f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                    mPlayingBackwards = false;
1139f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                }
1140f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase            }
1141a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (fraction >= 1f) {
1142d15e94f0309a91d5a75d03a9ae165121e7f24907Chet Haase                if (mCurrentIteration < mRepeatCount || mRepeatCount == INFINITE) {
1143a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    // Time to repeat
1144a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    if (mListeners != null) {
11457c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                        int numListeners = mListeners.size();
11467c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                        for (int i = 0; i < numListeners; ++i) {
11477c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                            mListeners.get(i).onAnimationRepeat(this);
1148a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                        }
1149a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    }
1150a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    if (mRepeatMode == REVERSE) {
115118772ea228f3d292629c4f0b5f6392d047e0530dRomain Guy                        mPlayingBackwards = !mPlayingBackwards;
1152a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    }
1153f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase                    mCurrentIteration += (int) fraction;
1154730666858692ea396f5ad779654b5d86ff90b6caChet Haase                    fraction = fraction % 1f;
1155a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    mStartTime += mDuration;
1156c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                    // Note: We do not need to update the value of mStartTimeCommitted here
1157c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                    // since we just added a duration offset.
1158a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                } else {
1159a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    done = true;
1160a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    fraction = Math.min(fraction, 1.0f);
1161a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                }
1162a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1163a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (mPlayingBackwards) {
1164a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                fraction = 1f - fraction;
1165a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1166a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            animateValue(fraction);
1167a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1168a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return done;
1169a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1170a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1171a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
117220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     * Processes a frame of the animation, adjusting the start time if needed.
117320c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     *
117420c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     * @param frameTime The frame time.
117520c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     * @return true if the animation has ended.
11763618d30f8ab6018025b11869676b309c3b4961cfDoris Liu     * @hide
117720c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown     */
11783618d30f8ab6018025b11869676b309c3b4961cfDoris Liu    public final void doAnimationFrame(long frameTime) {
11793618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        mLastFrameTime = frameTime;
11803618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        AnimationHandler handler = AnimationHandler.getInstance();
11813618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        if (!mRunning) {
11823618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            // First frame
11833618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            handler.addOneShotCommitCallback(this);
11843618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            startAnimation();
11850d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase            if (mSeekFraction < 0) {
118620c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown                mStartTime = frameTime;
118720c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            } else {
11880d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase                long seekTime = (long) (mDuration * mSeekFraction);
11890d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase                mStartTime = frameTime - seekTime;
11900d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase                mSeekFraction = -1;
119120c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown            }
1192c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown            mStartTimeCommitted = false; // allow start time to be compensated for jank
119320c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        }
11948aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        if (mPaused) {
11958aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            if (mPauseTime < 0) {
11968aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                mPauseTime = frameTime;
11978aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            }
11983618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            return;
11998aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        } else if (mResumed) {
12008aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            mResumed = false;
12018aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            if (mPauseTime > 0) {
12028aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                // Offset by the duration that the animation was paused
12038aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase                mStartTime += (frameTime - mPauseTime);
1204c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown                mStartTimeCommitted = false; // allow start time to be compensated for jank
12058aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase            }
12063618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            handler.addOneShotCommitCallback(this);
12078aa1ffb0ed292891030992c65df4e5dc8bd37524Chet Haase        }
120820c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        // The frame time might be before the start time during the first frame of
120920c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        // an animation.  The "current time" must always be on or after the start
121020c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        // time to avoid animating frames at negative time intervals.  In practice, this
121120c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        // is very rare and only happens when seeking backwards.
121220c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown        final long currentTime = Math.max(frameTime, mStartTime);
12133618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        boolean finished = animateBasedOnTime(currentTime);
12143618d30f8ab6018025b11869676b309c3b4961cfDoris Liu
12153618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        if (finished) {
12163618d30f8ab6018025b11869676b309c3b4961cfDoris Liu            endAnimation();
12173618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        }
121820c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown    }
121920c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown
122020c4f87b2916d05e860d11568d7db6b2d340e909Jeff Brown    /**
1221a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     * Returns the current animation fraction, which is the elapsed/interpolated fraction used in
1222a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     * the most recent frame update on the animation.
1223a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     *
1224a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     * @return Elapsed/interpolated fraction of the animation.
1225a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase     */
1226a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    public float getAnimatedFraction() {
1227a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase        return mCurrentFraction;
1228a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    }
1229a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase
1230a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase    /**
1231a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This method is called with the elapsed fraction of the animation during every
1232a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation frame. This function turns the elapsed fraction into an interpolated fraction
1233a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * and then into an animated value (from the evaluator. The function is called mostly during
1234a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation updates, but it is also called when the <code>end()</code>
1235a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function is called, to set the final value on the property.
1236a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1237a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>Overrides of this method must call the superclass to perform the calculation
1238a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of the animated value.</p>
1239a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1240a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param fraction The elapsed fraction of the animation.
1241a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1242c615c6fc9caca76cd96998f86e1f1e6393aeadbbTor Norbye    @CallSuper
1243a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    void animateValue(float fraction) {
1244a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        fraction = mInterpolator.getInterpolation(fraction);
1245a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase        mCurrentFraction = fraction;
1246a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        int numValues = mValues.length;
1247a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        for (int i = 0; i < numValues; ++i) {
1248a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mValues[i].calculateValue(fraction);
1249a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1250a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners != null) {
1251a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            int numListeners = mUpdateListeners.size();
1252a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 0; i < numListeners; ++i) {
1253a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mUpdateListeners.get(i).onAnimationUpdate(this);
1254a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1255a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1256a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1257a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1258a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
1259a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public ValueAnimator clone() {
1260a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        final ValueAnimator anim = (ValueAnimator) super.clone();
1261a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners != null) {
1262d422dc358f0100106dc07d7b903201eb9b043b11Yigit Boyar            anim.mUpdateListeners = new ArrayList<AnimatorUpdateListener>(mUpdateListeners);
1263a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
12640d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase        anim.mSeekFraction = -1;
1265a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mPlayingBackwards = false;
1266f4e3bab9253bba2c0086c35f4e5a1f7e41324876Chet Haase        anim.mReversing = false;
1267a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mCurrentIteration = 0;
1268a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mInitialized = false;
126926e9a19900bae56b012425a114685d42dfa2fde1ztenghui        anim.mStarted = false;
127026e9a19900bae56b012425a114685d42dfa2fde1ztenghui        anim.mRunning = false;
127126e9a19900bae56b012425a114685d42dfa2fde1ztenghui        anim.mPaused = false;
127226e9a19900bae56b012425a114685d42dfa2fde1ztenghui        anim.mResumed = false;
127326e9a19900bae56b012425a114685d42dfa2fde1ztenghui        anim.mStartListenersCalled = false;
1274e1b5c2b48a5cf1dd3712dde35b59bc18851b4018ztenghui        anim.mStartTime = 0;
1275e1b5c2b48a5cf1dd3712dde35b59bc18851b4018ztenghui        anim.mStartTimeCommitted = false;
12763dbaae1ef4f221b3626810f4ba19eec068dd6304Doris Liu        anim.mAnimationEndRequested = false;
1277e1b5c2b48a5cf1dd3712dde35b59bc18851b4018ztenghui        anim.mPauseTime = 0;
12783618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        anim.mLastFrameTime = 0;
1279e1b5c2b48a5cf1dd3712dde35b59bc18851b4018ztenghui        anim.mCurrentFraction = 0;
128026e9a19900bae56b012425a114685d42dfa2fde1ztenghui
1281a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        PropertyValuesHolder[] oldValues = mValues;
1282a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (oldValues != null) {
1283a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            int numValues = oldValues.length;
1284a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            anim.mValues = new PropertyValuesHolder[numValues];
1285a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            anim.mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues);
1286a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 0; i < numValues; ++i) {
1287d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase                PropertyValuesHolder newValuesHolder = oldValues[i].clone();
1288d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase                anim.mValues[i] = newValuesHolder;
1289d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase                anim.mValuesMap.put(newValuesHolder.getPropertyName(), newValuesHolder);
1290a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1291a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1292a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return anim;
1293a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1294a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1295a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1296a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Implementors of this interface can add themselves as update listeners
1297a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * to an <code>ValueAnimator</code> instance to receive callbacks on every animation
1298a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frame, after the current frame's values have been calculated for that
1299a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <code>ValueAnimator</code>.
1300a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1301a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static interface AnimatorUpdateListener {
1302a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        /**
1303a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * <p>Notifies the occurrence of another frame of the animation.</p>
1304a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         *
1305a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * @param animation The animation which was repeated.
1306a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         */
1307a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        void onAnimationUpdate(ValueAnimator animation);
1308a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1309a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1310599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick
1311599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick    /**
1312599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     * Return the number of animations currently running.
1313599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     *
13149c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown     * Used by StrictMode internally to annotate violations.
13159c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown     * May be called on arbitrary threads!
1316599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     *
1317599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     * @hide
1318599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     */
1319599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick    public static int getCurrentAnimationsCount() {
13203618d30f8ab6018025b11869676b309c3b4961cfDoris Liu        return AnimationHandler.getAnimationCount();
13218901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy    }
1322e9140a72b1059574046a624b471b2c3a35806496Chet Haase
1323e9140a72b1059574046a624b471b2c3a35806496Chet Haase    @Override
1324e9140a72b1059574046a624b471b2c3a35806496Chet Haase    public String toString() {
1325e9140a72b1059574046a624b471b2c3a35806496Chet Haase        String returnVal = "ValueAnimator@" + Integer.toHexString(hashCode());
1326e9140a72b1059574046a624b471b2c3a35806496Chet Haase        if (mValues != null) {
1327e9140a72b1059574046a624b471b2c3a35806496Chet Haase            for (int i = 0; i < mValues.length; ++i) {
1328e9140a72b1059574046a624b471b2c3a35806496Chet Haase                returnVal += "\n    " + mValues[i].toString();
1329e9140a72b1059574046a624b471b2c3a35806496Chet Haase            }
1330e9140a72b1059574046a624b471b2c3a35806496Chet Haase        }
1331e9140a72b1059574046a624b471b2c3a35806496Chet Haase        return returnVal;
1332e9140a72b1059574046a624b471b2c3a35806496Chet Haase    }
1333d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck
1334d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck    /**
1335d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <p>Whether or not the ValueAnimator is allowed to run asynchronously off of
1336d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * the UI thread. This is a hint that informs the ValueAnimator that it is
1337d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * OK to run the animation off-thread, however ValueAnimator may decide
1338d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * that it must run the animation on the UI thread anyway. For example if there
1339d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * is an {@link AnimatorUpdateListener} the animation will run on the UI thread,
1340d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * regardless of the value of this hint.</p>
1341d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *
1342d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <p>Regardless of whether or not the animation runs asynchronously, all
1343d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * listener callbacks will be called on the UI thread.</p>
1344d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *
1345d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <p>To be able to use this hint the following must be true:</p>
1346d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <ol>
1347d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <li>{@link #getAnimatedFraction()} is not needed (it will return undefined values).</li>
1348d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <li>The animator is immutable while {@link #isStarted()} is true. Requests
1349d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    to change values, duration, delay, etc... may be ignored.</li>
1350d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <li>Lifecycle callback events may be asynchronous. Events such as
1351d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    {@link Animator.AnimatorListener#onAnimationEnd(Animator)} or
1352d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    {@link Animator.AnimatorListener#onAnimationRepeat(Animator)} may end up delayed
1353d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    as they must be posted back to the UI thread, and any actions performed
1354d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    by those callbacks (such as starting new animations) will not happen
1355d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    in the same frame.</li>
1356d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * <li>State change requests ({@link #cancel()}, {@link #end()}, {@link #reverse()}, etc...)
1357d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    may be asynchronous. It is guaranteed that all state changes that are
1358d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    performed on the UI thread in the same frame will be applied as a single
1359d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    atomic update, however that frame may be the current frame,
1360d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    the next frame, or some future frame. This will also impact the observed
1361d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    state of the Animator. For example, {@link #isStarted()} may still return true
1362d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    after a call to {@link #end()}. Using the lifecycle callbacks is preferred over
1363d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    queries to {@link #isStarted()}, {@link #isRunning()}, and {@link #isPaused()}
1364d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     *    for this reason.</li>
1365d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * </ol>
1366d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     * @hide
1367d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck     */
1368c01bd1167a1b08d59557f214ddc48cf24d3b8d0aJohn Reck    @Override
1369d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck    public void setAllowRunningAsynchronously(boolean mayRunAsync) {
1370d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck        // It is up to subclasses to support this, if they can.
1371d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck    }
1372599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick}
1373