ValueAnimator.java revision e3bc4e6f102fbef760fe0a59dd807363571b0867
1a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase/*
2a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Copyright (C) 2010 The Android Open Source Project
3a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *
4a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Licensed under the Apache License, Version 2.0 (the "License");
5a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * you may not use this file except in compliance with the License.
6a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * You may obtain a copy of the License at
7a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *
8a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *      http://www.apache.org/licenses/LICENSE-2.0
9a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *
10a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Unless required by applicable law or agreed to in writing, software
11a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * distributed under the License is distributed on an "AS IS" BASIS,
12a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * See the License for the specific language governing permissions and
14a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * limitations under the License.
15a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */
16a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
17a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haasepackage android.animation;
18a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
19a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.os.Handler;
20a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.os.Looper;
21a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.os.Message;
22a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.view.animation.AccelerateDecelerateInterpolator;
23a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.view.animation.AnimationUtils;
24a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
25a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport java.util.ArrayList;
26a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport java.util.HashMap;
27a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
28a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase/**
29a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This class provides a simple timing engine for running animations
30a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * which calculate animated values and set them on target objects.
31a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *
32a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>There is a single timing pulse that all animations use. It runs in a
33a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * custom handler to ensure that property changes happen on the UI thread.</p>
34a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase *
35a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>By default, ValueAnimator uses non-linear time interpolation, via the
36a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link AccelerateDecelerateInterpolator} class, which accelerates into and decelerates
37a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * out of an animation. This behavior can be changed by calling
38e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase * {@link ValueAnimator#setInterpolator(TimeInterpolator)}.</p>
39a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */
402794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haasepublic class ValueAnimator extends Animator {
41a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
42a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
43a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Internal constants
44a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
45a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
46a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /*
47a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The default amount of time in ms between animation frames
48a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
49608fc3cbed6062f29cd512c08aacb8c1632a8851Chet Haase    private static final long DEFAULT_FRAME_DELAY = 10;
50a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
51a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
52a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Messages sent to timing handler: START is sent when an animation first begins, FRAME is sent
53a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * by the handler to itself to process the next animation frame
54a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
55a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private static final int ANIMATION_START = 0;
56a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private static final int ANIMATION_FRAME = 1;
57a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
58a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
59a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Values used with internal variable mPlayingState to indicate the current state of an
60a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation.
61a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
62a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private static final int STOPPED    = 0; // Not yet playing
63a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private static final int RUNNING    = 1; // Playing normally
64a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private static final int CANCELED   = 2; // cancel() called - need to end it
65a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private static final int ENDED      = 3; // end() called - need to end it
66a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private static final int SEEKED     = 4; // Seeked to some time value
67a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
68a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
69a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Internal variables
70a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * NOTE: This object implements the clone() method, making a deep copy of any referenced
71a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * objects. As other non-trivial fields are added to this class, make sure to add logic
72a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * to clone() to make deep copies of them.
73a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
74a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
75a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The first time that the animation's animateFrame() method is called. This time is used to
76a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // determine elapsed time (and therefore the elapsed fraction) in subsequent calls
77a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // to animateFrame()
78a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private long mStartTime;
79a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
80a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
81a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Set when setCurrentPlayTime() is called. If negative, animation is not currently seeked
82a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * to a value.
83a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
84a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private long mSeekTime = -1;
85a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
86e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase    // TODO: We access the following ThreadLocal variables often, some of them on every update.
87e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase    // If ThreadLocal access is significantly expensive, we may want to put all of these
88e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase    // fields into a structure sot hat we just access ThreadLocal once to get the reference
89e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase    // to that structure, then access the structure directly for each field.
90e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase
91a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The static sAnimationHandler processes the internal timing loop on which all animations
92a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // are based
93e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase    private static ThreadLocal<AnimationHandler> sAnimationHandler =
94e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase            new ThreadLocal<AnimationHandler>();
95e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase
96e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase    // The per-thread list of all active animations
97e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase    private static final ThreadLocal<ArrayList<ValueAnimator>> sAnimations =
98e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase            new ThreadLocal<ArrayList<ValueAnimator>>() {
99e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                @Override
100e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                protected ArrayList<ValueAnimator> initialValue() {
101e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                    return new ArrayList<ValueAnimator>();
102e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                }
103e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase            };
104e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase
105e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase    // The per-thread set of animations to be started on the next animation frame
106e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase    private static final ThreadLocal<ArrayList<ValueAnimator>> sPendingAnimations =
107e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase            new ThreadLocal<ArrayList<ValueAnimator>>() {
108e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                @Override
109e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                protected ArrayList<ValueAnimator> initialValue() {
110e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                    return new ArrayList<ValueAnimator>();
111e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                }
112e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase            };
113e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase
114e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase    /**
115e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase     * Internal per-thread collections used to avoid set collisions as animations start and end
116e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase     * while being processed.
117e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase     */
118e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase    private static final ThreadLocal<ArrayList<ValueAnimator>> sDelayedAnims =
119e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase            new ThreadLocal<ArrayList<ValueAnimator>>() {
120e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                @Override
121e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                protected ArrayList<ValueAnimator> initialValue() {
122e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                    return new ArrayList<ValueAnimator>();
123e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                }
124e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase            };
125a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
126e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase    private static final ThreadLocal<ArrayList<ValueAnimator>> sEndingAnims =
127e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase            new ThreadLocal<ArrayList<ValueAnimator>>() {
128e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                @Override
129e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                protected ArrayList<ValueAnimator> initialValue() {
130e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                    return new ArrayList<ValueAnimator>();
131e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                }
132e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase            };
133a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
134e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase    private static final ThreadLocal<ArrayList<ValueAnimator>> sReadyAnims =
135e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase            new ThreadLocal<ArrayList<ValueAnimator>>() {
136e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                @Override
137e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                protected ArrayList<ValueAnimator> initialValue() {
138e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                    return new ArrayList<ValueAnimator>();
139e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                }
140e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase            };
141a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
142a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The time interpolator to be used if none is set on the animation
143e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    private static final TimeInterpolator sDefaultInterpolator =
144e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase            new AccelerateDecelerateInterpolator();
145a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
146a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // type evaluators for the three primitive types handled by this implementation
147a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private static final TypeEvaluator sIntEvaluator = new IntEvaluator();
148a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private static final TypeEvaluator sFloatEvaluator = new FloatEvaluator();
149a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private static final TypeEvaluator sDoubleEvaluator = new DoubleEvaluator();
150a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
151a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
152a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Used to indicate whether the animation is currently playing in reverse. This causes the
153a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * elapsed fraction to be inverted to calculate the appropriate values.
154a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
155a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private boolean mPlayingBackwards = false;
156a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
157a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
158a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This variable tracks the current iteration that is playing. When mCurrentIteration exceeds the
159a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * repeatCount (if repeatCount!=INFINITE), the animation ends
160a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
161a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private int mCurrentIteration = 0;
162a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
163a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
164a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Tracks whether a startDelay'd animation has begun playing through the startDelay.
165a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
166a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private boolean mStartedDelay = false;
167a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
168a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
169a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Tracks the time at which the animation began playing through its startDelay. This is
170a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * different from the mStartTime variable, which is used to track when the animation became
171a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * active (which is when the startDelay expired and the animation was added to the active
172a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animations list).
173a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
174a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private long mDelayStartTime;
175a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
176a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
177a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Flag that represents the current state of the animation. Used to figure out when to start
178a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * an animation (if state == STOPPED). Also used to end an animation that
179a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * has been cancel()'d or end()'d since the last animation frame. Possible values are
180a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * STOPPED, RUNNING, ENDED, CANCELED.
181a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
182a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private int mPlayingState = STOPPED;
183a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
184a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
185a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Flag that denotes whether the animation is set up and ready to go. Used to
186a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * set up animation that has not yet been started.
187a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
188a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    boolean mInitialized = false;
189a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
190a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    //
191a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // Backing variables
192a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    //
193a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
194a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // How long the animation should last in ms
1952794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    private long mDuration = 300;
196a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
197a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The amount of time in ms to delay starting the animation after start() is called
198a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private long mStartDelay = 0;
199a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
200a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The number of milliseconds between animation frames
201a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private static long sFrameDelay = DEFAULT_FRAME_DELAY;
202a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
203a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // The number of times the animation will repeat. The default is 0, which means the animation
204a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    // will play only once
205a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private int mRepeatCount = 0;
206a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
207a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
208a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The type of repetition that will occur when repeatMode is nonzero. RESTART means the
209a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation will start from the beginning on every new cycle. REVERSE means the animation
210a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will reverse directions on each iteration.
211a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
212a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private int mRepeatMode = RESTART;
213a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
214a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
215a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The time interpolator to be used. The elapsed fraction of the animation will be passed
216a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * through this interpolator to calculate the interpolated fraction, which is then used to
217a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * calculate the animated values.
218a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
219e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    private TimeInterpolator mInterpolator = sDefaultInterpolator;
220a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
221a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
222a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The set of listeners to be sent events through the life of an animation.
223a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
224a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private ArrayList<AnimatorUpdateListener> mUpdateListeners = null;
225a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
226a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
227a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The property/value sets being animated.
228a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
229a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    PropertyValuesHolder[] mValues;
230a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
231a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
232a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * A hashmap of the PropertyValuesHolder objects. This map is used to lookup animated values
233a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * by property name during calls to getAnimatedValue(String).
234a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
235a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    HashMap<String, PropertyValuesHolder> mValuesMap;
236a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
237a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
238a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Public constants
239a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
240a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
241a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
242a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * When the animation reaches the end and <code>repeatCount</code> is INFINITE
243a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * or a positive value, the animation restarts from the beginning.
244a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
245a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static final int RESTART = 1;
246a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
247a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * When the animation reaches the end and <code>repeatCount</code> is INFINITE
248a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * or a positive value, the animation reverses direction on every iteration.
249a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
250a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static final int REVERSE = 2;
251a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
252a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This value used used with the {@link #setRepeatCount(int)} property to repeat
253a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the animation indefinitely.
254a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
255a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static final int INFINITE = -1;
256a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
257a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
258a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Creates a new ValueAnimator object. This default constructor is primarily for
2592794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * use internally; the factory methods which take parameters are more generally
260a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * useful.
261a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
262a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public ValueAnimator() {
263a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
264a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
265a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
2662794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between int values. A single
2672794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
2682794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
2692794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
2702794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
2712794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
272a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
2732794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
2742794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
27541f041d9986f8a5d45b6cb0b86e881c81a412168Chet Haase     */
2762794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofInt(int... values) {
2772794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
2782794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setIntValues(values);
2792794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
2802794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
2812794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
2822794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
2832794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between float values. A single
2842794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
2852794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
2862794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
2872794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
2882794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
2892794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
2902794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
2912794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
2922794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
2932794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofFloat(float... values) {
2942794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
2952794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setFloatValues(values);
2962794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
2972794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
2982794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
2992794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
3002794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between double values. A single
3012794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
3022794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
3032794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
3042794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
3052794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
3062794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
3072794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
3082794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
3092794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
3102794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ValueAnimator ofDouble(double... values) {
3112794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
3122794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setDoubleValues(values);
3132794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
3142794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
3152794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
3162794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
3172794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns a ValueAnimator that animates between long 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 ofLong(long... values) {
3282794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ValueAnimator anim = new ValueAnimator();
3292794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setLongValues(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) {
3902794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            setValues(new PropertyValuesHolder[]{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) {
4182794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            setValues(new PropertyValuesHolder[]{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 long values that will be animated between. 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     * @param values A set of values that the animation will animate between over time.
4402794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
4412794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public void setLongValues(long... values) {
4422794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (values == null || values.length == 0) {
4432794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            return;
4442794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
4452794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (mValues == null || mValues.length == 0) {
4462794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            setValues(new PropertyValuesHolder[]{PropertyValuesHolder.ofLong("", values)});
4472794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        } else {
4482794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            PropertyValuesHolder valuesHolder = mValues[0];
4492794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            valuesHolder.setLongValues(values);
4502794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
4512794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        // New property/values/target should cause re-initialization prior to starting
4522794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        mInitialized = false;
4532794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
4542794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
4552794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
4562794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Sets double values that will be animated between. A single
4572794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
4582794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
4592794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
4602794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
4612794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
4622794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4632794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>If there are already multiple sets of values defined for this ValueAnimator via more
4642794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * than one PropertyValuesHolder object, this method will set the values for the first
4652794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * of those objects.</p>
4662794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4672794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
4682794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
4692794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public void setDoubleValues(double... values) {
4702794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (values == null || values.length == 0) {
4712794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            return;
4722794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
4732794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (mValues == null || mValues.length == 0) {
4742794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            setValues(new PropertyValuesHolder[]{PropertyValuesHolder.ofDouble("", values)});
4752794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        } else {
4762794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            PropertyValuesHolder valuesHolder = mValues[0];
4772794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            valuesHolder.setDoubleValues(values);
4782794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
4792794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        // New property/values/target should cause re-initialization prior to starting
4802794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        mInitialized = false;
4812794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
4822794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
4832794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
4842794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Sets the values to animate between for this animation. A single
4852794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
4862794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
4872794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
4882794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
4892794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
4902794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4912794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>If there are already multiple sets of values defined for this ValueAnimator via more
4922794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * than one PropertyValuesHolder object, this method will set the values for the first
4932794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * of those objects.</p>
4942794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4952794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <p>There should be a TypeEvaluator set on the ValueAnimator that knows how to interpolate
4962794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * between these value objects. ValueAnimator only knows how to interpolate between the
4972794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * primitive types specified in the other setValues() methods.</p>
4982794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
4992794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values The set of values to animate between.
5002794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
5012794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public void setObjectValues(Object... values) {
5022794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (values == null || values.length == 0) {
5032794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            return;
5042794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
5052794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (mValues == null || mValues.length == 0) {
5062794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            setValues(new PropertyValuesHolder[]{PropertyValuesHolder.ofObject("",
5072794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase                    (TypeEvaluator)null, values)});
5082794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        } else {
5092794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            PropertyValuesHolder valuesHolder = mValues[0];
5102794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            valuesHolder.setObjectValues(values);
5112794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
5122794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        // New property/values/target should cause re-initialization prior to starting
5132794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        mInitialized = false;
514a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
515a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
516a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
517a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets the values, per property, being animated between. This function is called internally
518a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * by the constructors of ValueAnimator that take a list of values. But an ValueAnimator can
519a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * be constructed without values and this method can be called to set the values manually
520a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * instead.
521a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
522a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param values The set of values, per property, being animated between.
523a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
524a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setValues(PropertyValuesHolder... values) {
525a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        int numValues = values.length;
526a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mValues = values;
527a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues);
528a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        for (int i = 0; i < numValues; ++i) {
529a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            PropertyValuesHolder valuesHolder = (PropertyValuesHolder) values[i];
530a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mValuesMap.put(valuesHolder.getPropertyName(), valuesHolder);
531a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
5320e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase        // New property/values/target should cause re-initialization prior to starting
5330e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase        mInitialized = false;
534a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
535a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
536a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
537a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Returns the values that this ValueAnimator animates between. These values are stored in
538a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * PropertyValuesHolder objects, even if the ValueAnimator was created with a simple list
539a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of value objects instead.
540a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
541a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return PropertyValuesHolder[] An array of PropertyValuesHolder objects which hold the
542a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * values, per property, that define the animation.
543a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
544a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public PropertyValuesHolder[] getValues() {
545a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mValues;
546a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
547a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
548a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
549a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This function is called immediately before processing the first animation
550a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frame of an animation. If there is a nonzero <code>startDelay</code>, the
551a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function is called after that delay ends.
552a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * It takes care of the final initialization steps for the
553a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation.
554a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
555a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *  <p>Overrides of this method should call the superclass method to ensure
556a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *  that internal mechanisms for the animation are set up correctly.</p>
557a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
558a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    void initAnimation() {
559a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (!mInitialized) {
560a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            int numValues = mValues.length;
561a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 0; i < numValues; ++i) {
562a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mValues[i].init();
563a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
564a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mInitialized = true;
565a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
566a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
567a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
568a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
569a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
5702794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Sets the length of the animation. The default duration is 300 milliseconds.
571a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
572a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param duration The length of the animation, in milliseconds.
5732794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return ValueAnimator The object called with setDuration(). This return
5742794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value makes it easier to compose statements together that construct and then set the
5752794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * duration, as in <code>ValueAnimator.ofInt(0, 10).setDuration(500).start()</code>.
576a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
5772794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public ValueAnimator setDuration(long duration) {
578a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mDuration = duration;
5792794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return this;
580a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
581a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
582a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
5832794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Gets the length of the animation. The default duration is 300 milliseconds.
584a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
585a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return The length of the animation, in milliseconds.
586a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
587a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public long getDuration() {
588a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mDuration;
589a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
590a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
591a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
592a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets the position of the animation to the specified point in time. This time should
593a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * be between 0 and the total duration of the animation, including any repetition. If
594a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the animation has not yet been started, then it will not advance forward after it is
595a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * set to this time; it will simply set the time to this value and perform any appropriate
596a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * actions based on that time. If the animation is already running, then setCurrentPlayTime()
597a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will set the current playing time to this value and continue playing from that point.
598a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
599a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param playTime The time, in milliseconds, to which the animation is advanced or rewound.
600a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
601a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setCurrentPlayTime(long playTime) {
602a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        initAnimation();
603a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        long currentTime = AnimationUtils.currentAnimationTimeMillis();
604a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mPlayingState != RUNNING) {
605a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mSeekTime = playTime;
606a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mPlayingState = SEEKED;
607a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
608a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mStartTime = currentTime - playTime;
609a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        animationFrame(currentTime);
610a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
611a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
612a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
613a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Gets the current position of the animation in time, which is equal to the current
614a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * time minus the time that the animation started. An animation that is not yet started will
615a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * return a value of zero.
616a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
617a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return The current position in time of the animation.
618a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
619a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public long getCurrentPlayTime() {
620a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (!mInitialized || mPlayingState == STOPPED) {
621a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return 0;
622a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
623a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return AnimationUtils.currentAnimationTimeMillis() - mStartTime;
624a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
625a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
626a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
627a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This custom, static handler handles the timing pulse that is shared by
628a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * all active animations. This approach ensures that the setting of animation
629a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * values will happen on the UI thread and that all animations will share
630a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the same times for calculating their values, which makes synchronizing
631a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animations possible.
632a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
633a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
634a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private static class AnimationHandler extends Handler {
635a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        /**
636a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * There are only two messages that we care about: ANIMATION_START and
637a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * ANIMATION_FRAME. The START message is sent when an animation's start()
638a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * method is called. It cannot start synchronously when start() is called
639a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * because the call may be on the wrong thread, and it would also not be
640a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * synchronized with other animations because it would not start on a common
641a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * timing pulse. So each animation sends a START message to the handler, which
642a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * causes the handler to place the animation on the active animations queue and
643a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * start processing frames for that animation.
644a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * The FRAME message is the one that is sent over and over while there are any
645a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * active animations to process.
646a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         */
647a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        @Override
648a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        public void handleMessage(Message msg) {
649a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            boolean callAgain = true;
650e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase            ArrayList<ValueAnimator> animations = sAnimations.get();
651e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase            ArrayList<ValueAnimator> delayedAnims = sDelayedAnims.get();
652a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            switch (msg.what) {
653a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                // TODO: should we avoid sending frame message when starting if we
654a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                // were already running?
655a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                case ANIMATION_START:
656e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                    ArrayList<ValueAnimator> pendingAnimations = sPendingAnimations.get();
657e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                    if (animations.size() > 0 || delayedAnims.size() > 0) {
658a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                        callAgain = false;
659a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    }
660a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    // pendingAnims holds any animations that have requested to be started
661a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    // We're going to clear sPendingAnimations, but starting animation may
662a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    // cause more to be added to the pending list (for example, if one animation
663a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    // starting triggers another starting). So we loop until sPendingAnimations
664a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    // is empty.
665e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                    while (pendingAnimations.size() > 0) {
666a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                        ArrayList<ValueAnimator> pendingCopy =
667e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                                (ArrayList<ValueAnimator>) pendingAnimations.clone();
668e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                        pendingAnimations.clear();
669a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                        int count = pendingCopy.size();
670a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                        for (int i = 0; i < count; ++i) {
671a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                            ValueAnimator anim = pendingCopy.get(i);
672a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                            // If the animation has a startDelay, place it on the delayed list
673a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                            if (anim.mStartDelay == 0 || anim.mPlayingState == ENDED ||
674a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                                    anim.mPlayingState == CANCELED) {
675a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                                anim.startAnimation();
676a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                            } else {
677e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                                delayedAnims.add(anim);
678a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                            }
679a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                        }
680a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    }
681a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    // fall through to process first frame of new animations
682a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                case ANIMATION_FRAME:
683a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    // currentTime holds the common time for all animations processed
684a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    // during this frame
685a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    long currentTime = AnimationUtils.currentAnimationTimeMillis();
686e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                    ArrayList<ValueAnimator> readyAnims = sReadyAnims.get();
687e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                    ArrayList<ValueAnimator> endingAnims = sEndingAnims.get();
688a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
689a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    // First, process animations currently sitting on the delayed queue, adding
690a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    // them to the active animations if they are ready
691e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                    int numDelayedAnims = delayedAnims.size();
692a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    for (int i = 0; i < numDelayedAnims; ++i) {
693e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                        ValueAnimator anim = delayedAnims.get(i);
694a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                        if (anim.delayedAnimationFrame(currentTime)) {
695e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                            readyAnims.add(anim);
696a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                        }
697a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    }
698e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                    int numReadyAnims = readyAnims.size();
699a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    if (numReadyAnims > 0) {
700a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                        for (int i = 0; i < numReadyAnims; ++i) {
701e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                            ValueAnimator anim = readyAnims.get(i);
702a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                            anim.startAnimation();
703e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                            delayedAnims.remove(anim);
704a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                        }
705e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                        readyAnims.clear();
706a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    }
707a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
708a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    // Now process all active animations. The return value from animationFrame()
709a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    // tells the handler whether it should now be ended
710e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                    int numAnims = animations.size();
711a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    for (int i = 0; i < numAnims; ++i) {
712e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                        ValueAnimator anim = animations.get(i);
713a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                        if (anim.animationFrame(currentTime)) {
714e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                            endingAnims.add(anim);
715a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                        }
716a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    }
717e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                    if (endingAnims.size() > 0) {
718e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                        for (int i = 0; i < endingAnims.size(); ++i) {
719e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                            endingAnims.get(i).endAnimation();
720a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                        }
721e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                        endingAnims.clear();
722a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    }
723a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
724a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    // If there are still active or delayed animations, call the handler again
725a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    // after the frameDelay
726e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                    if (callAgain && (!animations.isEmpty() || !delayedAnims.isEmpty())) {
72751b3227c2e08143c2e3dde9d51ae5b6fff36693cChet Haase                        sendEmptyMessageDelayed(ANIMATION_FRAME, Math.max(0, sFrameDelay -
72851b3227c2e08143c2e3dde9d51ae5b6fff36693cChet Haase                            (AnimationUtils.currentAnimationTimeMillis() - currentTime)));
729a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    }
730a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    break;
731a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
732a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
733a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
734a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
735a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
736a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, to delay starting the animation after
737a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link #start()} is called.
738a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
739a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return the number of milliseconds to delay running the animation
740a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
741a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public long getStartDelay() {
742a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mStartDelay;
743a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
744a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
745a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
746a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, to delay starting the animation after
747a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link #start()} is called.
748a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
749a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param startDelay The amount of the delay, in milliseconds
750a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
751a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setStartDelay(long startDelay) {
752a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        this.mStartDelay = startDelay;
753a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
754a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
755a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
756a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, between each frame of the animation. This is a
757a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * requested time that the animation will attempt to honor, but the actual delay between
758a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frames may be different, depending on system load and capabilities. This is a static
759a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function because the same delay will be applied to all animations, since they are all
760a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * run off of a single timing loop.
761a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
762a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return the requested time between frames, in milliseconds
763a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
764a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static long getFrameDelay() {
765a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return sFrameDelay;
766a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
767a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
768a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
769a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The amount of time, in milliseconds, between each frame of the animation. This is a
770a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * requested time that the animation will attempt to honor, but the actual delay between
771a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frames may be different, depending on system load and capabilities. This is a static
772a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function because the same delay will be applied to all animations, since they are all
773a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * run off of a single timing loop.
774a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
775a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param frameDelay the requested time between frames, in milliseconds
776a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
777a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static void setFrameDelay(long frameDelay) {
778a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        sFrameDelay = frameDelay;
779a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
780a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
781a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
782a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The most recent value calculated by this <code>ValueAnimator</code> when there is just one
783a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * property being animated. This value is only sensible while the animation is running. The main
784a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * purpose for this read-only property is to retrieve the value from the <code>ValueAnimator</code>
785a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * during a call to {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which
786a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is called during each animation frame, immediately after the value is calculated.
787a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
788a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return animatedValue The value most recently calculated by this <code>ValueAnimator</code> for
789a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the single property being animated. If there are several properties being animated
790a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * (specified by several PropertyValuesHolder objects in the constructor), this function
791a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * returns the animated value for the first of those objects.
792a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
793a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public Object getAnimatedValue() {
794a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mValues != null && mValues.length > 0) {
795a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return mValues[0].getAnimatedValue();
796a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
797a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        // Shouldn't get here; should always have values unless ValueAnimator was set up wrong
798a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return null;
799a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
800a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
801a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
802a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The most recent value calculated by this <code>ValueAnimator</code> for <code>propertyName</code>.
803a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The main purpose for this read-only property is to retrieve the value from the
804a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <code>ValueAnimator</code> during a call to
805a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which
806a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is called during each animation frame, immediately after the value is calculated.
807a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
808a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return animatedValue The value most recently calculated for the named property
809a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * by this <code>ValueAnimator</code>.
810a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
811a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public Object getAnimatedValue(String propertyName) {
812a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        PropertyValuesHolder valuesHolder = mValuesMap.get(propertyName);
813a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (valuesHolder != null) {
814a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return valuesHolder.getAnimatedValue();
815a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        } else {
816a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // At least avoid crashing if called with bogus propertyName
817a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return null;
818a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
819a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
820a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
821a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
822a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Sets how many times the animation should be repeated. If the repeat
823a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * count is 0, the animation is never repeated. If the repeat count is
824a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * greater than 0 or {@link #INFINITE}, the repeat mode will be taken
825a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * into account. The repeat count is 0 by default.
826a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
827a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param value the number of times the animation should be repeated
828a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
829a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setRepeatCount(int value) {
830a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mRepeatCount = value;
831a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
832a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
833a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Defines how many times the animation should repeat. The default value
834a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is 0.
835a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
836a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return the number of times the animation should repeat, or {@link #INFINITE}
837a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
838a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public int getRepeatCount() {
839a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mRepeatCount;
840a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
841a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
842a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
843a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Defines what this animation should do when it reaches the end. This
844a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * setting is applied only when the repeat count is either greater than
845a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * 0 or {@link #INFINITE}. Defaults to {@link #RESTART}.
846a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
847a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param value {@link #RESTART} or {@link #REVERSE}
848a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
849a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setRepeatMode(int value) {
850a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mRepeatMode = value;
851a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
852a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
853a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
854a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Defines what this animation should do when it reaches the end.
855a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
856a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return either one of {@link #REVERSE} or {@link #RESTART}
857a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
858a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public int getRepeatMode() {
859a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mRepeatMode;
860a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
861a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
862a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
863a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Adds a listener to the set of listeners that are sent update events through the life of
864a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * an animation. This method is called on all listeners for every frame of the animation,
865a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * after the values for the animation have been calculated.
866a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
867a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param listener the listener to be added to the current set of listeners for this animation.
868a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
869a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void addUpdateListener(AnimatorUpdateListener listener) {
870a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners == null) {
871a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mUpdateListeners = new ArrayList<AnimatorUpdateListener>();
872a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
873a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mUpdateListeners.add(listener);
874a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
875a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
876a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
8773060421045d4d9e411797f91bb509824b03e33fbJim Miller     * Removes all listeners from the set listening to frame updates for this animation.
8783060421045d4d9e411797f91bb509824b03e33fbJim Miller     */
8793060421045d4d9e411797f91bb509824b03e33fbJim Miller    public void removeAllUpdateListeners() {
8803060421045d4d9e411797f91bb509824b03e33fbJim Miller        if (mUpdateListeners == null) {
8813060421045d4d9e411797f91bb509824b03e33fbJim Miller            return;
8823060421045d4d9e411797f91bb509824b03e33fbJim Miller        }
8833060421045d4d9e411797f91bb509824b03e33fbJim Miller        mUpdateListeners.clear();
8843060421045d4d9e411797f91bb509824b03e33fbJim Miller        mUpdateListeners = null;
8853060421045d4d9e411797f91bb509824b03e33fbJim Miller    }
8863060421045d4d9e411797f91bb509824b03e33fbJim Miller
8873060421045d4d9e411797f91bb509824b03e33fbJim Miller    /**
888a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Removes a listener from the set listening to frame updates for this animation.
889a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
890a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param listener the listener to be removed from the current set of update listeners
891a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * for this animation.
892a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
893a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void removeUpdateListener(AnimatorUpdateListener listener) {
894a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners == null) {
895a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return;
896a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
897a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mUpdateListeners.remove(listener);
898a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners.size() == 0) {
899a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mUpdateListeners = null;
900a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
901a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
902a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
903a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
904a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
905a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The time interpolator used in calculating the elapsed fraction of this animation. The
906a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * interpolator determines whether the animation runs with linear or non-linear motion,
907a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * such as acceleration and deceleration. The default value is
908a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * {@link android.view.animation.AccelerateDecelerateInterpolator}
909a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
910a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param value the interpolator to be used by this animation
911a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
912a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
913e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    public void setInterpolator(TimeInterpolator value) {
914a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (value != null) {
915a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mInterpolator = value;
916a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
917a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
918a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
919a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
920a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Returns the timing interpolator that this ValueAnimator uses.
921a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
922a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return The timing interpolator for this ValueAnimator.
923a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
924e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    public TimeInterpolator getInterpolator() {
925a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return mInterpolator;
926a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
927a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
928a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
929a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The type evaluator to be used when calculating the animated values of this animation.
930a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * The system will automatically assign a float, int, or double evaluator based on the type
931a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of <code>startValue</code> and <code>endValue</code> in the constructor. But if these values
932a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * are not one of these primitive types, or if different evaluation is desired (such as is
933a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * necessary with int values that represent colors), a custom evaluator needs to be assigned.
934a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * For example, when running an animation on color values, the {@link RGBEvaluator}
935a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * should be used to get correct RGB color interpolation.
936a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
937a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>If this ValueAnimator has only one set of values being animated between, this evaluator
938a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * will be used for that set. If there are several sets of values being animated, which is
939a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the case if PropertyValuesHOlder objects were set on the ValueAnimator, then the evaluator
940a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * is assigned just to the first PropertyValuesHolder object.</p>
941a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
942a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param value the evaluator to be used this animation
943a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
944a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void setEvaluator(TypeEvaluator value) {
945a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (value != null && mValues != null && mValues.length > 0) {
946a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mValues[0].setEvaluator(value);
947a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
948a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
949a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
950a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
951a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Start the animation playing. This version of start() takes a boolean flag that indicates
952a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * whether the animation should play in reverse. The flag is usually false, but may be set
953a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * to true if called from the reverse() method/
954a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
955a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param playBackwards Whether the ValueAnimator should start playing in reverse.
956a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
957a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private void start(boolean playBackwards) {
958a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mPlayingBackwards = playBackwards;
9593060421045d4d9e411797f91bb509824b03e33fbJim Miller        Looper looper = Looper.getMainLooper();
9603060421045d4d9e411797f91bb509824b03e33fbJim Miller        final boolean isUiThread;
9613060421045d4d9e411797f91bb509824b03e33fbJim Miller        if (looper != null) {
9623060421045d4d9e411797f91bb509824b03e33fbJim Miller            isUiThread = Thread.currentThread() == looper.getThread();
9633060421045d4d9e411797f91bb509824b03e33fbJim Miller        } else {
9643060421045d4d9e411797f91bb509824b03e33fbJim Miller            // ignore check if we don't have a Looper (this isn't an Activity)
9653060421045d4d9e411797f91bb509824b03e33fbJim Miller            isUiThread = true;
9663060421045d4d9e411797f91bb509824b03e33fbJim Miller        }
9673060421045d4d9e411797f91bb509824b03e33fbJim Miller        if ((mStartDelay == 0) && isUiThread) {
968b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase            if (mListeners != null) {
969b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase                ArrayList<AnimatorListener> tmpListeners =
970b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase                        (ArrayList<AnimatorListener>) mListeners.clone();
971b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase                for (AnimatorListener listener : tmpListeners) {
972b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase                    listener.onAnimationStart(this);
973b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase                }
974b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase            }
975a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // This sets the initial value of the animation, prior to actually starting it running
976a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            setCurrentPlayTime(getCurrentPlayTime());
977a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
9785c13d89c1332fcc499379b9064b891187b75ca32Chet Haase        mCurrentIteration = 0;
979a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mPlayingState = STOPPED;
980a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mStartedDelay = false;
981e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase        sPendingAnimations.get().add(this);
982e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase        AnimationHandler animationHandler = sAnimationHandler.get();
983e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase        if (animationHandler == null) {
984e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase            animationHandler = new AnimationHandler();
985e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase            sAnimationHandler.set(animationHandler);
986a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
987e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase        animationHandler.sendEmptyMessage(ANIMATION_START);
988a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
989a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
990a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
991a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void start() {
992a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        start(false);
993a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
994a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
995a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
996a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void cancel() {
997a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mListeners != null) {
998a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            ArrayList<AnimatorListener> tmpListeners =
999a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    (ArrayList<AnimatorListener>) mListeners.clone();
1000a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (AnimatorListener listener : tmpListeners) {
1001a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                listener.onAnimationCancel(this);
1002a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1003a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1004a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        // Just set the CANCELED flag - this causes the animation to end the next time a frame
1005a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        // is processed.
1006a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mPlayingState = CANCELED;
1007a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1008a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1009a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
1010a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void end() {
1011e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase        if (!sAnimations.get().contains(this) && !sPendingAnimations.get().contains(this)) {
1012a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // Special case if the animation has not yet started; get it ready for ending
1013a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mStartedDelay = false;
1014e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase            sPendingAnimations.get().add(this);
1015e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase            AnimationHandler animationHandler = sAnimationHandler.get();
1016e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase            if (animationHandler == null) {
1017e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                animationHandler = new AnimationHandler();
1018e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase                sAnimationHandler.set(animationHandler);
1019a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1020e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase            animationHandler.sendEmptyMessage(ANIMATION_START);
1021a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1022a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        // Just set the ENDED flag - this causes the animation to end the next time a frame
1023a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        // is processed.
1024a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mPlayingState = ENDED;
1025a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1026a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1027a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
1028a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public boolean isRunning() {
1029a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        // ENDED or CANCELED indicate that it has been ended or canceled, but not processed yet
1030a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return (mPlayingState == RUNNING || mPlayingState == ENDED || mPlayingState == CANCELED);
1031a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1032a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1033a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1034a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Plays the ValueAnimator in reverse. If the animation is already running,
1035a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * it will stop itself and play backwards from the point reached when reverse was called.
1036a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * If the animation is not currently running, then it will start from the end and
1037a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * play backwards. This behavior is only set for the current animation; future playing
1038a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of the animation will use the default behavior of playing forward.
1039a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1040a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public void reverse() {
1041a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mPlayingBackwards = !mPlayingBackwards;
1042a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mPlayingState == RUNNING) {
1043a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            long currentTime = AnimationUtils.currentAnimationTimeMillis();
1044a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            long currentPlayTime = currentTime - mStartTime;
1045a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            long timeLeft = mDuration - currentPlayTime;
1046a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mStartTime = currentTime - timeLeft;
1047a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        } else {
1048a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            start(true);
1049a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1050a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1051a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1052a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1053a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Called internally to end an animation by removing it from the animations list. Must be
1054a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * called on the UI thread.
1055a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1056a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private void endAnimation() {
1057e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase        sAnimations.get().remove(this);
1058a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        mPlayingState = STOPPED;
1059a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mListeners != null) {
1060a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            ArrayList<AnimatorListener> tmpListeners =
1061a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    (ArrayList<AnimatorListener>) mListeners.clone();
1062a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (AnimatorListener listener : tmpListeners) {
1063a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                listener.onAnimationEnd(this);
1064a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1065a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1066a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1067a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1068a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1069a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Called internally to start an animation by adding it to the active animations list. Must be
1070a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * called on the UI thread.
1071a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1072a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private void startAnimation() {
1073a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        initAnimation();
1074e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase        sAnimations.get().add(this);
1075b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase        if (mStartDelay > 0 && mListeners != null) {
1076b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase            // Listeners were already notified in start() if startDelay is 0; this is
1077b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase            // just for delayed animations
1078a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            ArrayList<AnimatorListener> tmpListeners =
1079a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    (ArrayList<AnimatorListener>) mListeners.clone();
1080a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (AnimatorListener listener : tmpListeners) {
1081a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                listener.onAnimationStart(this);
1082a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1083a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1084a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1085a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1086a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1087a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Internal function called to process an animation frame on an animation that is currently
1088a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * sleeping through its <code>startDelay</code> phase. The return value indicates whether it
1089a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * should be woken up and put on the active animations queue.
1090a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1091a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param currentTime The current animation time, used to calculate whether the animation
1092a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * has exceeded its <code>startDelay</code> and should be started.
1093a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return True if the animation's <code>startDelay</code> has been exceeded and the animation
1094a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * should be added to the set of active animations.
1095a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1096a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private boolean delayedAnimationFrame(long currentTime) {
1097a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mPlayingState == CANCELED || mPlayingState == ENDED) {
1098a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // end the delay, process an animation frame to actually cancel it
1099a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            return true;
1100a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1101a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (!mStartedDelay) {
1102a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mStartedDelay = true;
1103a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mDelayStartTime = currentTime;
1104a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        } else {
1105a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            long deltaTime = currentTime - mDelayStartTime;
1106a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (deltaTime > mStartDelay) {
1107a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                // startDelay ended - start the anim and record the
1108a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                // mStartTime appropriately
1109a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mStartTime = currentTime - (deltaTime - mStartDelay);
1110a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mPlayingState = RUNNING;
1111a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                return true;
1112a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1113a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1114a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return false;
1115a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1116a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1117a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1118a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This internal function processes a single animation frame for a given animation. The
1119a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * currentTime parameter is the timing pulse sent by the handler, used to calculate the
1120a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * elapsed duration, and therefore
1121a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * the elapsed fraction, of the animation. The return value indicates whether the animation
1122a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * should be ended (which happens when the elapsed time of the animation exceeds the
1123a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation's duration, including the repeatCount).
1124a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1125a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param currentTime The current time, as tracked by the static timing handler
1126a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @return true if the animation's duration, including any repetitions due to
1127a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <code>repeatCount</code> has been exceeded and the animation should be ended.
1128a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1129a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    private boolean animationFrame(long currentTime) {
1130a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        boolean done = false;
1131a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1132a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mPlayingState == STOPPED) {
1133a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mPlayingState = RUNNING;
1134a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (mSeekTime < 0) {
1135a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mStartTime = currentTime;
1136a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            } else {
1137a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mStartTime = currentTime - mSeekTime;
1138a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                // Now that we're playing, reset the seek time
1139a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mSeekTime = -1;
1140a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1141a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1142a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        switch (mPlayingState) {
1143a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        case RUNNING:
1144a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        case SEEKED:
1145a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            float fraction = (float)(currentTime - mStartTime) / mDuration;
1146a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (fraction >= 1f) {
1147a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                if (mCurrentIteration < mRepeatCount || mRepeatCount == INFINITE) {
1148a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    // Time to repeat
1149a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    if (mListeners != null) {
1150a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                        for (AnimatorListener listener : mListeners) {
1151a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                            listener.onAnimationRepeat(this);
1152a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                        }
1153a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    }
1154a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    ++mCurrentIteration;
1155a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    if (mRepeatMode == REVERSE) {
1156a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                        mPlayingBackwards = mPlayingBackwards ? false : true;
1157a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    }
1158a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    // TODO: doesn't account for fraction going Wayyyyy over 1, like 2+
1159a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    fraction = fraction - 1f;
1160a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    mStartTime += mDuration;
1161a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                } else {
1162a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    done = true;
1163a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                    fraction = Math.min(fraction, 1.0f);
1164a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                }
1165a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1166a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (mPlayingBackwards) {
1167a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                fraction = 1f - fraction;
1168a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1169a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            animateValue(fraction);
1170a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            break;
1171a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        case ENDED:
1172a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // The final value set on the target varies, depending on whether the animation
1173a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // was supposed to repeat an odd number of times
1174a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            if (mRepeatCount > 0 && (mRepeatCount & 0x01) == 1) {
1175a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                animateValue(0f);
1176a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            } else {
1177a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                animateValue(1f);
1178a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1179a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            // Fall through to set done flag
1180a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        case CANCELED:
1181a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            done = true;
1182a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mPlayingState = STOPPED;
1183a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            break;
1184a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1185a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1186a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return done;
1187a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1188a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1189a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1190a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * This method is called with the elapsed fraction of the animation during every
1191a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation frame. This function turns the elapsed fraction into an interpolated fraction
1192a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * and then into an animated value (from the evaluator. The function is called mostly during
1193a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * animation updates, but it is also called when the <code>end()</code>
1194a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * function is called, to set the final value on the property.
1195a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1196a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>Overrides of this method must call the superclass to perform the calculation
1197a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * of the animated value.</p>
1198a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     *
1199a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * @param fraction The elapsed fraction of the animation.
1200a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1201a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    void animateValue(float fraction) {
1202a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        fraction = mInterpolator.getInterpolation(fraction);
1203a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        int numValues = mValues.length;
1204a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        for (int i = 0; i < numValues; ++i) {
1205a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            mValues[i].calculateValue(fraction);
1206a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1207a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners != null) {
1208a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            int numListeners = mUpdateListeners.size();
1209a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 0; i < numListeners; ++i) {
1210a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                mUpdateListeners.get(i).onAnimationUpdate(this);
1211a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1212a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1213a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1214a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1215a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    @Override
1216a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public ValueAnimator clone() {
1217a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        final ValueAnimator anim = (ValueAnimator) super.clone();
1218a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (mUpdateListeners != null) {
1219a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            ArrayList<AnimatorUpdateListener> oldListeners = mUpdateListeners;
1220a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            anim.mUpdateListeners = new ArrayList<AnimatorUpdateListener>();
1221a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            int numListeners = oldListeners.size();
1222a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 0; i < numListeners; ++i) {
1223a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                anim.mUpdateListeners.add(oldListeners.get(i));
1224a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1225a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1226a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mSeekTime = -1;
1227a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mPlayingBackwards = false;
1228a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mCurrentIteration = 0;
1229a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mInitialized = false;
1230a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mPlayingState = STOPPED;
1231a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        anim.mStartedDelay = false;
1232a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        PropertyValuesHolder[] oldValues = mValues;
1233a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        if (oldValues != null) {
1234a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            int numValues = oldValues.length;
1235a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            anim.mValues = new PropertyValuesHolder[numValues];
1236a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 0; i < numValues; ++i) {
1237a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                anim.mValues[i] = oldValues[i].clone();
1238a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1239a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            anim.mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues);
1240a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            for (int i = 0; i < numValues; ++i) {
1241a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                PropertyValuesHolder valuesHolder = mValues[i];
1242a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase                anim.mValuesMap.put(valuesHolder.getPropertyName(), valuesHolder);
1243a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            }
1244a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        }
1245a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        return anim;
1246a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1247a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1248a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    /**
1249a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Implementors of this interface can add themselves as update listeners
1250a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * to an <code>ValueAnimator</code> instance to receive callbacks on every animation
1251a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * frame, after the current frame's values have been calculated for that
1252a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <code>ValueAnimator</code>.
1253a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     */
1254a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public static interface AnimatorUpdateListener {
1255a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        /**
1256a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * <p>Notifies the occurrence of another frame of the animation.</p>
1257a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         *
1258a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         * @param animation The animation which was repeated.
1259a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase         */
1260a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        void onAnimationUpdate(ValueAnimator animation);
1261a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase
1262a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    }
1263599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick
1264599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick    /**
1265599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     * Return the number of animations currently running.
1266599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     *
1267599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     * Used by StrictMode internally to annotate violations.  Only
1268599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     * called on the main thread.
1269599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     *
1270599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     * @hide
1271599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick     */
1272599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick    public static int getCurrentAnimationsCount() {
1273e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase        return sAnimations.get().size();
1274599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick    }
1275599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick}
1276