Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.animation; |
| 18 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 19 | import android.os.Looper; |
Romain Guy | 18772ea | 2013-04-10 18:31:22 -0700 | [diff] [blame] | 20 | import android.os.Trace; |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 21 | import android.util.AndroidRuntimeException; |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 22 | import android.view.Choreographer; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 23 | import android.view.animation.AccelerateDecelerateInterpolator; |
| 24 | import android.view.animation.AnimationUtils; |
Chet Haase | 27c1d4d | 2010-12-16 07:58:28 -0800 | [diff] [blame] | 25 | import android.view.animation.LinearInterpolator; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 26 | |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.HashMap; |
| 29 | |
| 30 | /** |
| 31 | * This class provides a simple timing engine for running animations |
| 32 | * which calculate animated values and set them on target objects. |
| 33 | * |
| 34 | * <p>There is a single timing pulse that all animations use. It runs in a |
| 35 | * custom handler to ensure that property changes happen on the UI thread.</p> |
| 36 | * |
| 37 | * <p>By default, ValueAnimator uses non-linear time interpolation, via the |
| 38 | * {@link AccelerateDecelerateInterpolator} class, which accelerates into and decelerates |
| 39 | * out of an animation. This behavior can be changed by calling |
Chet Haase | e0ee2e9 | 2010-10-07 09:06:18 -0700 | [diff] [blame] | 40 | * {@link ValueAnimator#setInterpolator(TimeInterpolator)}.</p> |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 41 | * |
| 42 | * <div class="special reference"> |
| 43 | * <h3>Developer Guides</h3> |
| 44 | * <p>For more information about animating with {@code ValueAnimator}, read the |
| 45 | * <a href="{@docRoot}guide/topics/graphics/prop-animation.html#value-animator">Property |
| 46 | * Animation</a> developer guide.</p> |
| 47 | * </div> |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 48 | */ |
Romain Guy | 18772ea | 2013-04-10 18:31:22 -0700 | [diff] [blame] | 49 | @SuppressWarnings("unchecked") |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 50 | public class ValueAnimator extends Animator { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Internal constants |
| 54 | */ |
Chet Haase | d21a9fe | 2012-02-01 14:14:17 -0800 | [diff] [blame] | 55 | private static float sDurationScale = 1.0f; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 56 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 57 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 58 | * Values used with internal variable mPlayingState to indicate the current state of an |
| 59 | * animation. |
| 60 | */ |
Chet Haase | 051d35e | 2010-12-14 07:20:58 -0800 | [diff] [blame] | 61 | static final int STOPPED = 0; // Not yet playing |
| 62 | static final int RUNNING = 1; // Playing normally |
| 63 | static final int SEEKED = 2; // Seeked to some time value |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Internal variables |
| 67 | * NOTE: This object implements the clone() method, making a deep copy of any referenced |
| 68 | * objects. As other non-trivial fields are added to this class, make sure to add logic |
| 69 | * to clone() to make deep copies of them. |
| 70 | */ |
| 71 | |
| 72 | // The first time that the animation's animateFrame() method is called. This time is used to |
| 73 | // determine elapsed time (and therefore the elapsed fraction) in subsequent calls |
| 74 | // to animateFrame() |
Chet Haase | 051d35e | 2010-12-14 07:20:58 -0800 | [diff] [blame] | 75 | long mStartTime; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Set when setCurrentPlayTime() is called. If negative, animation is not currently seeked |
| 79 | * to a value. |
| 80 | */ |
Chet Haase | 051d35e | 2010-12-14 07:20:58 -0800 | [diff] [blame] | 81 | long mSeekTime = -1; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 82 | |
| 83 | // The static sAnimationHandler processes the internal timing loop on which all animations |
| 84 | // are based |
Chet Haase | be19e03 | 2013-03-15 17:08:55 -0700 | [diff] [blame] | 85 | /** |
| 86 | * @hide |
| 87 | */ |
| 88 | protected static ThreadLocal<AnimationHandler> sAnimationHandler = |
Chet Haase | e3bc4e6 | 2010-10-27 13:18:54 -0700 | [diff] [blame] | 89 | new ThreadLocal<AnimationHandler>(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 90 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 91 | // The time interpolator to be used if none is set on the animation |
Chet Haase | e0ee2e9 | 2010-10-07 09:06:18 -0700 | [diff] [blame] | 92 | private static final TimeInterpolator sDefaultInterpolator = |
| 93 | new AccelerateDecelerateInterpolator(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 94 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Used to indicate whether the animation is currently playing in reverse. This causes the |
| 97 | * elapsed fraction to be inverted to calculate the appropriate values. |
| 98 | */ |
| 99 | private boolean mPlayingBackwards = false; |
| 100 | |
| 101 | /** |
| 102 | * This variable tracks the current iteration that is playing. When mCurrentIteration exceeds the |
| 103 | * repeatCount (if repeatCount!=INFINITE), the animation ends |
| 104 | */ |
| 105 | private int mCurrentIteration = 0; |
| 106 | |
| 107 | /** |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 108 | * Tracks current elapsed/eased fraction, for querying in getAnimatedFraction(). |
| 109 | */ |
| 110 | private float mCurrentFraction = 0f; |
| 111 | |
| 112 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 113 | * Tracks whether a startDelay'd animation has begun playing through the startDelay. |
| 114 | */ |
| 115 | private boolean mStartedDelay = false; |
| 116 | |
| 117 | /** |
| 118 | * Tracks the time at which the animation began playing through its startDelay. This is |
| 119 | * different from the mStartTime variable, which is used to track when the animation became |
| 120 | * active (which is when the startDelay expired and the animation was added to the active |
| 121 | * animations list). |
| 122 | */ |
| 123 | private long mDelayStartTime; |
| 124 | |
| 125 | /** |
| 126 | * Flag that represents the current state of the animation. Used to figure out when to start |
| 127 | * an animation (if state == STOPPED). Also used to end an animation that |
| 128 | * has been cancel()'d or end()'d since the last animation frame. Possible values are |
Chet Haase | e2ab7cc | 2010-12-06 16:10:07 -0800 | [diff] [blame] | 129 | * STOPPED, RUNNING, SEEKED. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 130 | */ |
Chet Haase | 051d35e | 2010-12-14 07:20:58 -0800 | [diff] [blame] | 131 | int mPlayingState = STOPPED; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 132 | |
| 133 | /** |
Chet Haase | b8f574a | 2011-08-03 14:10:06 -0700 | [diff] [blame] | 134 | * Additional playing state to indicate whether an animator has been start()'d. There is |
| 135 | * some lag between a call to start() and the first animation frame. We should still note |
| 136 | * that the animation has been started, even if it's first animation frame has not yet |
| 137 | * happened, and reflect that state in isRunning(). |
| 138 | * Note that delayed animations are different: they are not started until their first |
| 139 | * animation frame, which occurs after their delay elapses. |
| 140 | */ |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 141 | private boolean mRunning = false; |
| 142 | |
| 143 | /** |
| 144 | * Additional playing state to indicate whether an animator has been start()'d, whether or |
| 145 | * not there is a nonzero startDelay. |
| 146 | */ |
Chet Haase | b8f574a | 2011-08-03 14:10:06 -0700 | [diff] [blame] | 147 | private boolean mStarted = false; |
| 148 | |
| 149 | /** |
Chet Haase | 17cf42c | 2012-04-17 13:18:14 -0700 | [diff] [blame] | 150 | * Tracks whether we've notified listeners of the onAnimationSTart() event. This can be |
| 151 | * complex to keep track of since we notify listeners at different times depending on |
| 152 | * startDelay and whether start() was called before end(). |
| 153 | */ |
| 154 | private boolean mStartListenersCalled = false; |
| 155 | |
| 156 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 157 | * Flag that denotes whether the animation is set up and ready to go. Used to |
| 158 | * set up animation that has not yet been started. |
| 159 | */ |
| 160 | boolean mInitialized = false; |
| 161 | |
| 162 | // |
| 163 | // Backing variables |
| 164 | // |
| 165 | |
| 166 | // How long the animation should last in ms |
Chet Haase | c38fa1f | 2012-02-01 16:37:46 -0800 | [diff] [blame] | 167 | private long mDuration = (long)(300 * sDurationScale); |
Chet Haase | d21a9fe | 2012-02-01 14:14:17 -0800 | [diff] [blame] | 168 | private long mUnscaledDuration = 300; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 169 | |
| 170 | // The amount of time in ms to delay starting the animation after start() is called |
| 171 | private long mStartDelay = 0; |
Chet Haase | d21a9fe | 2012-02-01 14:14:17 -0800 | [diff] [blame] | 172 | private long mUnscaledStartDelay = 0; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 173 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 174 | // The number of times the animation will repeat. The default is 0, which means the animation |
| 175 | // will play only once |
| 176 | private int mRepeatCount = 0; |
| 177 | |
| 178 | /** |
| 179 | * The type of repetition that will occur when repeatMode is nonzero. RESTART means the |
| 180 | * animation will start from the beginning on every new cycle. REVERSE means the animation |
| 181 | * will reverse directions on each iteration. |
| 182 | */ |
| 183 | private int mRepeatMode = RESTART; |
| 184 | |
| 185 | /** |
| 186 | * The time interpolator to be used. The elapsed fraction of the animation will be passed |
| 187 | * through this interpolator to calculate the interpolated fraction, which is then used to |
| 188 | * calculate the animated values. |
| 189 | */ |
Chet Haase | e0ee2e9 | 2010-10-07 09:06:18 -0700 | [diff] [blame] | 190 | private TimeInterpolator mInterpolator = sDefaultInterpolator; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 191 | |
| 192 | /** |
| 193 | * The set of listeners to be sent events through the life of an animation. |
| 194 | */ |
| 195 | private ArrayList<AnimatorUpdateListener> mUpdateListeners = null; |
| 196 | |
| 197 | /** |
| 198 | * The property/value sets being animated. |
| 199 | */ |
| 200 | PropertyValuesHolder[] mValues; |
| 201 | |
| 202 | /** |
| 203 | * A hashmap of the PropertyValuesHolder objects. This map is used to lookup animated values |
| 204 | * by property name during calls to getAnimatedValue(String). |
| 205 | */ |
| 206 | HashMap<String, PropertyValuesHolder> mValuesMap; |
| 207 | |
| 208 | /** |
| 209 | * Public constants |
| 210 | */ |
| 211 | |
| 212 | /** |
| 213 | * When the animation reaches the end and <code>repeatCount</code> is INFINITE |
| 214 | * or a positive value, the animation restarts from the beginning. |
| 215 | */ |
| 216 | public static final int RESTART = 1; |
| 217 | /** |
| 218 | * When the animation reaches the end and <code>repeatCount</code> is INFINITE |
| 219 | * or a positive value, the animation reverses direction on every iteration. |
| 220 | */ |
| 221 | public static final int REVERSE = 2; |
| 222 | /** |
| 223 | * This value used used with the {@link #setRepeatCount(int)} property to repeat |
| 224 | * the animation indefinitely. |
| 225 | */ |
| 226 | public static final int INFINITE = -1; |
| 227 | |
Chet Haase | c38fa1f | 2012-02-01 16:37:46 -0800 | [diff] [blame] | 228 | |
| 229 | /** |
| 230 | * @hide |
| 231 | */ |
| 232 | public static void setDurationScale(float durationScale) { |
| 233 | sDurationScale = durationScale; |
| 234 | } |
| 235 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 236 | /** |
Jeff Brown | ff7e6ef | 2012-08-15 02:05:18 -0700 | [diff] [blame] | 237 | * @hide |
| 238 | */ |
| 239 | public static float getDurationScale() { |
| 240 | return sDurationScale; |
| 241 | } |
| 242 | |
| 243 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 244 | * Creates a new ValueAnimator object. This default constructor is primarily for |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 245 | * use internally; the factory methods which take parameters are more generally |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 246 | * useful. |
| 247 | */ |
| 248 | public ValueAnimator() { |
| 249 | } |
| 250 | |
| 251 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 252 | * Constructs and returns a ValueAnimator that animates between int values. A single |
| 253 | * value implies that that value is the one being animated to. However, this is not typically |
| 254 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 255 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 256 | * from the target object and property being animated). Therefore, there should typically |
| 257 | * be two or more values. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 258 | * |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 259 | * @param values A set of values that the animation will animate between over time. |
| 260 | * @return A ValueAnimator object that is set up to animate between the given values. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 261 | */ |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 262 | public static ValueAnimator ofInt(int... values) { |
| 263 | ValueAnimator anim = new ValueAnimator(); |
| 264 | anim.setIntValues(values); |
| 265 | return anim; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Constructs and returns a ValueAnimator that animates between float values. A single |
| 270 | * value implies that that value is the one being animated to. However, this is not typically |
| 271 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 272 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 273 | * from the target object and property being animated). Therefore, there should typically |
| 274 | * be two or more values. |
| 275 | * |
| 276 | * @param values A set of values that the animation will animate between over time. |
| 277 | * @return A ValueAnimator object that is set up to animate between the given values. |
| 278 | */ |
| 279 | public static ValueAnimator ofFloat(float... values) { |
| 280 | ValueAnimator anim = new ValueAnimator(); |
| 281 | anim.setFloatValues(values); |
| 282 | return anim; |
| 283 | } |
| 284 | |
| 285 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 286 | * Constructs and returns a ValueAnimator that animates between the values |
| 287 | * specified in the PropertyValuesHolder objects. |
| 288 | * |
| 289 | * @param values A set of PropertyValuesHolder objects whose values will be animated |
| 290 | * between over time. |
| 291 | * @return A ValueAnimator object that is set up to animate between the given values. |
| 292 | */ |
| 293 | public static ValueAnimator ofPropertyValuesHolder(PropertyValuesHolder... values) { |
| 294 | ValueAnimator anim = new ValueAnimator(); |
| 295 | anim.setValues(values); |
| 296 | return anim; |
| 297 | } |
| 298 | /** |
| 299 | * Constructs and returns a ValueAnimator that animates between Object values. A single |
| 300 | * value implies that that value is the one being animated to. However, this is not typically |
| 301 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 302 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 303 | * from the target object and property being animated). Therefore, there should typically |
| 304 | * be two or more values. |
| 305 | * |
| 306 | * <p>Since ValueAnimator does not know how to animate between arbitrary Objects, this |
| 307 | * factory method also takes a TypeEvaluator object that the ValueAnimator will use |
| 308 | * to perform that interpolation. |
| 309 | * |
| 310 | * @param evaluator A TypeEvaluator that will be called on each animation frame to |
| 311 | * provide the ncessry interpolation between the Object values to derive the animated |
| 312 | * value. |
| 313 | * @param values A set of values that the animation will animate between over time. |
| 314 | * @return A ValueAnimator object that is set up to animate between the given values. |
| 315 | */ |
| 316 | public static ValueAnimator ofObject(TypeEvaluator evaluator, Object... values) { |
| 317 | ValueAnimator anim = new ValueAnimator(); |
| 318 | anim.setObjectValues(values); |
| 319 | anim.setEvaluator(evaluator); |
| 320 | return anim; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Sets int values that will be animated between. A single |
| 325 | * value implies that that value is the one being animated to. However, this is not typically |
| 326 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 327 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 328 | * from the target object and property being animated). Therefore, there should typically |
| 329 | * be two or more values. |
| 330 | * |
| 331 | * <p>If there are already multiple sets of values defined for this ValueAnimator via more |
| 332 | * than one PropertyValuesHolder object, this method will set the values for the first |
| 333 | * of those objects.</p> |
| 334 | * |
| 335 | * @param values A set of values that the animation will animate between over time. |
| 336 | */ |
| 337 | public void setIntValues(int... values) { |
| 338 | if (values == null || values.length == 0) { |
| 339 | return; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 340 | } |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 341 | if (mValues == null || mValues.length == 0) { |
Romain Guy | 18772ea | 2013-04-10 18:31:22 -0700 | [diff] [blame] | 342 | setValues(PropertyValuesHolder.ofInt("", values)); |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 343 | } else { |
| 344 | PropertyValuesHolder valuesHolder = mValues[0]; |
| 345 | valuesHolder.setIntValues(values); |
| 346 | } |
| 347 | // New property/values/target should cause re-initialization prior to starting |
| 348 | mInitialized = false; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Sets float values that will be animated between. A single |
| 353 | * value implies that that value is the one being animated to. However, this is not typically |
| 354 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 355 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 356 | * from the target object and property being animated). Therefore, there should typically |
| 357 | * be two or more values. |
| 358 | * |
| 359 | * <p>If there are already multiple sets of values defined for this ValueAnimator via more |
| 360 | * than one PropertyValuesHolder object, this method will set the values for the first |
| 361 | * of those objects.</p> |
| 362 | * |
| 363 | * @param values A set of values that the animation will animate between over time. |
| 364 | */ |
| 365 | public void setFloatValues(float... values) { |
| 366 | if (values == null || values.length == 0) { |
| 367 | return; |
| 368 | } |
| 369 | if (mValues == null || mValues.length == 0) { |
Romain Guy | 18772ea | 2013-04-10 18:31:22 -0700 | [diff] [blame] | 370 | setValues(PropertyValuesHolder.ofFloat("", values)); |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 371 | } else { |
| 372 | PropertyValuesHolder valuesHolder = mValues[0]; |
| 373 | valuesHolder.setFloatValues(values); |
| 374 | } |
| 375 | // New property/values/target should cause re-initialization prior to starting |
| 376 | mInitialized = false; |
| 377 | } |
| 378 | |
| 379 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 380 | * Sets the values to animate between for this animation. A single |
| 381 | * value implies that that value is the one being animated to. However, this is not typically |
| 382 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 383 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 384 | * from the target object and property being animated). Therefore, there should typically |
| 385 | * be two or more values. |
| 386 | * |
| 387 | * <p>If there are already multiple sets of values defined for this ValueAnimator via more |
| 388 | * than one PropertyValuesHolder object, this method will set the values for the first |
| 389 | * of those objects.</p> |
| 390 | * |
| 391 | * <p>There should be a TypeEvaluator set on the ValueAnimator that knows how to interpolate |
| 392 | * between these value objects. ValueAnimator only knows how to interpolate between the |
| 393 | * primitive types specified in the other setValues() methods.</p> |
| 394 | * |
| 395 | * @param values The set of values to animate between. |
| 396 | */ |
| 397 | public void setObjectValues(Object... values) { |
| 398 | if (values == null || values.length == 0) { |
| 399 | return; |
| 400 | } |
| 401 | if (mValues == null || mValues.length == 0) { |
Romain Guy | 18772ea | 2013-04-10 18:31:22 -0700 | [diff] [blame] | 402 | setValues(PropertyValuesHolder.ofObject("", null, values)); |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 403 | } else { |
| 404 | PropertyValuesHolder valuesHolder = mValues[0]; |
| 405 | valuesHolder.setObjectValues(values); |
| 406 | } |
| 407 | // New property/values/target should cause re-initialization prior to starting |
| 408 | mInitialized = false; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Sets the values, per property, being animated between. This function is called internally |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 413 | * by the constructors of ValueAnimator that take a list of values. But a ValueAnimator can |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 414 | * be constructed without values and this method can be called to set the values manually |
| 415 | * instead. |
| 416 | * |
| 417 | * @param values The set of values, per property, being animated between. |
| 418 | */ |
| 419 | public void setValues(PropertyValuesHolder... values) { |
| 420 | int numValues = values.length; |
| 421 | mValues = values; |
| 422 | mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues); |
| 423 | for (int i = 0; i < numValues; ++i) { |
Romain Guy | 18772ea | 2013-04-10 18:31:22 -0700 | [diff] [blame] | 424 | PropertyValuesHolder valuesHolder = values[i]; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 425 | mValuesMap.put(valuesHolder.getPropertyName(), valuesHolder); |
| 426 | } |
Chet Haase | 0e0590b | 2010-09-26 11:57:28 -0700 | [diff] [blame] | 427 | // New property/values/target should cause re-initialization prior to starting |
| 428 | mInitialized = false; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Returns the values that this ValueAnimator animates between. These values are stored in |
| 433 | * PropertyValuesHolder objects, even if the ValueAnimator was created with a simple list |
| 434 | * of value objects instead. |
| 435 | * |
| 436 | * @return PropertyValuesHolder[] An array of PropertyValuesHolder objects which hold the |
| 437 | * values, per property, that define the animation. |
| 438 | */ |
| 439 | public PropertyValuesHolder[] getValues() { |
| 440 | return mValues; |
| 441 | } |
| 442 | |
| 443 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 444 | * This function is called immediately before processing the first animation |
| 445 | * frame of an animation. If there is a nonzero <code>startDelay</code>, the |
| 446 | * function is called after that delay ends. |
| 447 | * It takes care of the final initialization steps for the |
| 448 | * animation. |
| 449 | * |
| 450 | * <p>Overrides of this method should call the superclass method to ensure |
| 451 | * that internal mechanisms for the animation are set up correctly.</p> |
| 452 | */ |
| 453 | void initAnimation() { |
| 454 | if (!mInitialized) { |
| 455 | int numValues = mValues.length; |
| 456 | for (int i = 0; i < numValues; ++i) { |
| 457 | mValues[i].init(); |
| 458 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 459 | mInitialized = true; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | |
| 464 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 465 | * Sets the length of the animation. The default duration is 300 milliseconds. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 466 | * |
Chet Haase | 27c1d4d | 2010-12-16 07:58:28 -0800 | [diff] [blame] | 467 | * @param duration The length of the animation, in milliseconds. This value cannot |
| 468 | * be negative. |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 469 | * @return ValueAnimator The object called with setDuration(). This return |
| 470 | * value makes it easier to compose statements together that construct and then set the |
| 471 | * duration, as in <code>ValueAnimator.ofInt(0, 10).setDuration(500).start()</code>. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 472 | */ |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 473 | public ValueAnimator setDuration(long duration) { |
Chet Haase | 27c1d4d | 2010-12-16 07:58:28 -0800 | [diff] [blame] | 474 | if (duration < 0) { |
| 475 | throw new IllegalArgumentException("Animators cannot have negative duration: " + |
| 476 | duration); |
| 477 | } |
Chet Haase | d21a9fe | 2012-02-01 14:14:17 -0800 | [diff] [blame] | 478 | mUnscaledDuration = duration; |
| 479 | mDuration = (long)(duration * sDurationScale); |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 480 | return this; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 484 | * Gets the length of the animation. The default duration is 300 milliseconds. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 485 | * |
| 486 | * @return The length of the animation, in milliseconds. |
| 487 | */ |
| 488 | public long getDuration() { |
Chet Haase | d21a9fe | 2012-02-01 14:14:17 -0800 | [diff] [blame] | 489 | return mUnscaledDuration; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Sets the position of the animation to the specified point in time. This time should |
| 494 | * be between 0 and the total duration of the animation, including any repetition. If |
| 495 | * the animation has not yet been started, then it will not advance forward after it is |
| 496 | * set to this time; it will simply set the time to this value and perform any appropriate |
| 497 | * actions based on that time. If the animation is already running, then setCurrentPlayTime() |
| 498 | * will set the current playing time to this value and continue playing from that point. |
| 499 | * |
| 500 | * @param playTime The time, in milliseconds, to which the animation is advanced or rewound. |
| 501 | */ |
| 502 | public void setCurrentPlayTime(long playTime) { |
| 503 | initAnimation(); |
| 504 | long currentTime = AnimationUtils.currentAnimationTimeMillis(); |
| 505 | if (mPlayingState != RUNNING) { |
| 506 | mSeekTime = playTime; |
| 507 | mPlayingState = SEEKED; |
| 508 | } |
| 509 | mStartTime = currentTime - playTime; |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 510 | doAnimationFrame(currentTime); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Gets the current position of the animation in time, which is equal to the current |
| 515 | * time minus the time that the animation started. An animation that is not yet started will |
| 516 | * return a value of zero. |
| 517 | * |
| 518 | * @return The current position in time of the animation. |
| 519 | */ |
| 520 | public long getCurrentPlayTime() { |
| 521 | if (!mInitialized || mPlayingState == STOPPED) { |
| 522 | return 0; |
| 523 | } |
| 524 | return AnimationUtils.currentAnimationTimeMillis() - mStartTime; |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * This custom, static handler handles the timing pulse that is shared by |
| 529 | * all active animations. This approach ensures that the setting of animation |
| 530 | * values will happen on the UI thread and that all animations will share |
| 531 | * the same times for calculating their values, which makes synchronizing |
| 532 | * animations possible. |
| 533 | * |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 534 | * The handler uses the Choreographer for executing periodic callbacks. |
Chet Haase | be19e03 | 2013-03-15 17:08:55 -0700 | [diff] [blame] | 535 | * |
| 536 | * @hide |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 537 | */ |
Romain Guy | 18772ea | 2013-04-10 18:31:22 -0700 | [diff] [blame] | 538 | @SuppressWarnings("unchecked") |
Chet Haase | be19e03 | 2013-03-15 17:08:55 -0700 | [diff] [blame] | 539 | protected static class AnimationHandler implements Runnable { |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 540 | // The per-thread list of all active animations |
Chet Haase | be19e03 | 2013-03-15 17:08:55 -0700 | [diff] [blame] | 541 | /** @hide */ |
| 542 | protected final ArrayList<ValueAnimator> mAnimations = new ArrayList<ValueAnimator>(); |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 543 | |
Chet Haase | 2936fc0 | 2012-08-16 14:34:04 -0700 | [diff] [blame] | 544 | // Used in doAnimationFrame() to avoid concurrent modifications of mAnimations |
| 545 | private final ArrayList<ValueAnimator> mTmpAnimations = new ArrayList<ValueAnimator>(); |
| 546 | |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 547 | // The per-thread set of animations to be started on the next animation frame |
Chet Haase | be19e03 | 2013-03-15 17:08:55 -0700 | [diff] [blame] | 548 | /** @hide */ |
| 549 | protected final ArrayList<ValueAnimator> mPendingAnimations = new ArrayList<ValueAnimator>(); |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 550 | |
| 551 | /** |
| 552 | * Internal per-thread collections used to avoid set collisions as animations start and end |
| 553 | * while being processed. |
Chet Haase | be19e03 | 2013-03-15 17:08:55 -0700 | [diff] [blame] | 554 | * @hide |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 555 | */ |
Chet Haase | be19e03 | 2013-03-15 17:08:55 -0700 | [diff] [blame] | 556 | protected final ArrayList<ValueAnimator> mDelayedAnims = new ArrayList<ValueAnimator>(); |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 557 | private final ArrayList<ValueAnimator> mEndingAnims = new ArrayList<ValueAnimator>(); |
| 558 | private final ArrayList<ValueAnimator> mReadyAnims = new ArrayList<ValueAnimator>(); |
| 559 | |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 560 | private final Choreographer mChoreographer; |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 561 | private boolean mAnimationScheduled; |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 562 | |
| 563 | private AnimationHandler() { |
| 564 | mChoreographer = Choreographer.getInstance(); |
| 565 | } |
| 566 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 567 | /** |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 568 | * Start animating on the next frame. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 569 | */ |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 570 | public void start() { |
| 571 | scheduleAnimation(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 572 | } |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 573 | |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 574 | private void doAnimationFrame(long frameTime) { |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 575 | // mPendingAnimations holds any animations that have requested to be started |
| 576 | // We're going to clear mPendingAnimations, but starting animation may |
| 577 | // cause more to be added to the pending list (for example, if one animation |
| 578 | // starting triggers another starting). So we loop until mPendingAnimations |
| 579 | // is empty. |
| 580 | while (mPendingAnimations.size() > 0) { |
| 581 | ArrayList<ValueAnimator> pendingCopy = |
| 582 | (ArrayList<ValueAnimator>) mPendingAnimations.clone(); |
| 583 | mPendingAnimations.clear(); |
| 584 | int count = pendingCopy.size(); |
| 585 | for (int i = 0; i < count; ++i) { |
| 586 | ValueAnimator anim = pendingCopy.get(i); |
| 587 | // If the animation has a startDelay, place it on the delayed list |
| 588 | if (anim.mStartDelay == 0) { |
| 589 | anim.startAnimation(this); |
| 590 | } else { |
| 591 | mDelayedAnims.add(anim); |
| 592 | } |
| 593 | } |
| 594 | } |
Chet Haase | c6ffab3 | 2012-03-16 16:32:26 -0700 | [diff] [blame] | 595 | // Next, process animations currently sitting on the delayed queue, adding |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 596 | // them to the active animations if they are ready |
| 597 | int numDelayedAnims = mDelayedAnims.size(); |
| 598 | for (int i = 0; i < numDelayedAnims; ++i) { |
| 599 | ValueAnimator anim = mDelayedAnims.get(i); |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 600 | if (anim.delayedAnimationFrame(frameTime)) { |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 601 | mReadyAnims.add(anim); |
| 602 | } |
| 603 | } |
| 604 | int numReadyAnims = mReadyAnims.size(); |
| 605 | if (numReadyAnims > 0) { |
| 606 | for (int i = 0; i < numReadyAnims; ++i) { |
| 607 | ValueAnimator anim = mReadyAnims.get(i); |
| 608 | anim.startAnimation(this); |
| 609 | anim.mRunning = true; |
| 610 | mDelayedAnims.remove(anim); |
| 611 | } |
| 612 | mReadyAnims.clear(); |
| 613 | } |
| 614 | |
| 615 | // Now process all active animations. The return value from animationFrame() |
| 616 | // tells the handler whether it should now be ended |
| 617 | int numAnims = mAnimations.size(); |
Chet Haase | 2936fc0 | 2012-08-16 14:34:04 -0700 | [diff] [blame] | 618 | for (int i = 0; i < numAnims; ++i) { |
| 619 | mTmpAnimations.add(mAnimations.get(i)); |
| 620 | } |
| 621 | for (int i = 0; i < numAnims; ++i) { |
| 622 | ValueAnimator anim = mTmpAnimations.get(i); |
| 623 | if (mAnimations.contains(anim) && anim.doAnimationFrame(frameTime)) { |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 624 | mEndingAnims.add(anim); |
| 625 | } |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 626 | } |
Chet Haase | 2936fc0 | 2012-08-16 14:34:04 -0700 | [diff] [blame] | 627 | mTmpAnimations.clear(); |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 628 | if (mEndingAnims.size() > 0) { |
Chet Haase | 2936fc0 | 2012-08-16 14:34:04 -0700 | [diff] [blame] | 629 | for (int i = 0; i < mEndingAnims.size(); ++i) { |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 630 | mEndingAnims.get(i).endAnimation(this); |
| 631 | } |
| 632 | mEndingAnims.clear(); |
| 633 | } |
| 634 | |
| 635 | // If there are still active or delayed animations, schedule a future call to |
| 636 | // onAnimate to process the next frame of the animations. |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 637 | if (!mAnimations.isEmpty() || !mDelayedAnims.isEmpty()) { |
| 638 | scheduleAnimation(); |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 642 | // Called by the Choreographer. |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 643 | @Override |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 644 | public void run() { |
| 645 | mAnimationScheduled = false; |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 646 | doAnimationFrame(mChoreographer.getFrameTime()); |
| 647 | } |
| 648 | |
| 649 | private void scheduleAnimation() { |
| 650 | if (!mAnimationScheduled) { |
| 651 | mChoreographer.postCallback(Choreographer.CALLBACK_ANIMATION, this, null); |
| 652 | mAnimationScheduled = true; |
| 653 | } |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 654 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | /** |
| 658 | * The amount of time, in milliseconds, to delay starting the animation after |
| 659 | * {@link #start()} is called. |
| 660 | * |
| 661 | * @return the number of milliseconds to delay running the animation |
| 662 | */ |
| 663 | public long getStartDelay() { |
Chet Haase | d21a9fe | 2012-02-01 14:14:17 -0800 | [diff] [blame] | 664 | return mUnscaledStartDelay; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /** |
| 668 | * The amount of time, in milliseconds, to delay starting the animation after |
| 669 | * {@link #start()} is called. |
| 670 | |
| 671 | * @param startDelay The amount of the delay, in milliseconds |
| 672 | */ |
| 673 | public void setStartDelay(long startDelay) { |
Chet Haase | d21a9fe | 2012-02-01 14:14:17 -0800 | [diff] [blame] | 674 | this.mStartDelay = (long)(startDelay * sDurationScale); |
| 675 | mUnscaledStartDelay = startDelay; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | /** |
| 679 | * The amount of time, in milliseconds, between each frame of the animation. This is a |
| 680 | * requested time that the animation will attempt to honor, but the actual delay between |
| 681 | * frames may be different, depending on system load and capabilities. This is a static |
| 682 | * function because the same delay will be applied to all animations, since they are all |
| 683 | * run off of a single timing loop. |
| 684 | * |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 685 | * The frame delay may be ignored when the animation system uses an external timing |
| 686 | * source, such as the display refresh rate (vsync), to govern animations. |
| 687 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 688 | * @return the requested time between frames, in milliseconds |
| 689 | */ |
| 690 | public static long getFrameDelay() { |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 691 | return Choreographer.getFrameDelay(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /** |
| 695 | * The amount of time, in milliseconds, between each frame of the animation. This is a |
| 696 | * requested time that the animation will attempt to honor, but the actual delay between |
| 697 | * frames may be different, depending on system load and capabilities. This is a static |
| 698 | * function because the same delay will be applied to all animations, since they are all |
| 699 | * run off of a single timing loop. |
| 700 | * |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 701 | * The frame delay may be ignored when the animation system uses an external timing |
| 702 | * source, such as the display refresh rate (vsync), to govern animations. |
| 703 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 704 | * @param frameDelay the requested time between frames, in milliseconds |
| 705 | */ |
| 706 | public static void setFrameDelay(long frameDelay) { |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 707 | Choreographer.setFrameDelay(frameDelay); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /** |
| 711 | * The most recent value calculated by this <code>ValueAnimator</code> when there is just one |
| 712 | * property being animated. This value is only sensible while the animation is running. The main |
| 713 | * purpose for this read-only property is to retrieve the value from the <code>ValueAnimator</code> |
| 714 | * during a call to {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which |
| 715 | * is called during each animation frame, immediately after the value is calculated. |
| 716 | * |
| 717 | * @return animatedValue The value most recently calculated by this <code>ValueAnimator</code> for |
| 718 | * the single property being animated. If there are several properties being animated |
| 719 | * (specified by several PropertyValuesHolder objects in the constructor), this function |
| 720 | * returns the animated value for the first of those objects. |
| 721 | */ |
| 722 | public Object getAnimatedValue() { |
| 723 | if (mValues != null && mValues.length > 0) { |
| 724 | return mValues[0].getAnimatedValue(); |
| 725 | } |
| 726 | // Shouldn't get here; should always have values unless ValueAnimator was set up wrong |
| 727 | return null; |
| 728 | } |
| 729 | |
| 730 | /** |
| 731 | * The most recent value calculated by this <code>ValueAnimator</code> for <code>propertyName</code>. |
| 732 | * The main purpose for this read-only property is to retrieve the value from the |
| 733 | * <code>ValueAnimator</code> during a call to |
| 734 | * {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which |
| 735 | * is called during each animation frame, immediately after the value is calculated. |
| 736 | * |
| 737 | * @return animatedValue The value most recently calculated for the named property |
| 738 | * by this <code>ValueAnimator</code>. |
| 739 | */ |
| 740 | public Object getAnimatedValue(String propertyName) { |
| 741 | PropertyValuesHolder valuesHolder = mValuesMap.get(propertyName); |
| 742 | if (valuesHolder != null) { |
| 743 | return valuesHolder.getAnimatedValue(); |
| 744 | } else { |
| 745 | // At least avoid crashing if called with bogus propertyName |
| 746 | return null; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | /** |
| 751 | * Sets how many times the animation should be repeated. If the repeat |
| 752 | * count is 0, the animation is never repeated. If the repeat count is |
| 753 | * greater than 0 or {@link #INFINITE}, the repeat mode will be taken |
| 754 | * into account. The repeat count is 0 by default. |
| 755 | * |
| 756 | * @param value the number of times the animation should be repeated |
| 757 | */ |
| 758 | public void setRepeatCount(int value) { |
| 759 | mRepeatCount = value; |
| 760 | } |
| 761 | /** |
| 762 | * Defines how many times the animation should repeat. The default value |
| 763 | * is 0. |
| 764 | * |
| 765 | * @return the number of times the animation should repeat, or {@link #INFINITE} |
| 766 | */ |
| 767 | public int getRepeatCount() { |
| 768 | return mRepeatCount; |
| 769 | } |
| 770 | |
| 771 | /** |
| 772 | * Defines what this animation should do when it reaches the end. This |
| 773 | * setting is applied only when the repeat count is either greater than |
| 774 | * 0 or {@link #INFINITE}. Defaults to {@link #RESTART}. |
| 775 | * |
| 776 | * @param value {@link #RESTART} or {@link #REVERSE} |
| 777 | */ |
| 778 | public void setRepeatMode(int value) { |
| 779 | mRepeatMode = value; |
| 780 | } |
| 781 | |
| 782 | /** |
| 783 | * Defines what this animation should do when it reaches the end. |
| 784 | * |
| 785 | * @return either one of {@link #REVERSE} or {@link #RESTART} |
| 786 | */ |
| 787 | public int getRepeatMode() { |
| 788 | return mRepeatMode; |
| 789 | } |
| 790 | |
| 791 | /** |
| 792 | * Adds a listener to the set of listeners that are sent update events through the life of |
| 793 | * an animation. This method is called on all listeners for every frame of the animation, |
| 794 | * after the values for the animation have been calculated. |
| 795 | * |
| 796 | * @param listener the listener to be added to the current set of listeners for this animation. |
| 797 | */ |
| 798 | public void addUpdateListener(AnimatorUpdateListener listener) { |
| 799 | if (mUpdateListeners == null) { |
| 800 | mUpdateListeners = new ArrayList<AnimatorUpdateListener>(); |
| 801 | } |
| 802 | mUpdateListeners.add(listener); |
| 803 | } |
| 804 | |
| 805 | /** |
Jim Miller | 3060421 | 2010-09-22 19:56:23 -0700 | [diff] [blame] | 806 | * Removes all listeners from the set listening to frame updates for this animation. |
| 807 | */ |
| 808 | public void removeAllUpdateListeners() { |
| 809 | if (mUpdateListeners == null) { |
| 810 | return; |
| 811 | } |
| 812 | mUpdateListeners.clear(); |
| 813 | mUpdateListeners = null; |
| 814 | } |
| 815 | |
| 816 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 817 | * Removes a listener from the set listening to frame updates for this animation. |
| 818 | * |
| 819 | * @param listener the listener to be removed from the current set of update listeners |
| 820 | * for this animation. |
| 821 | */ |
| 822 | public void removeUpdateListener(AnimatorUpdateListener listener) { |
| 823 | if (mUpdateListeners == null) { |
| 824 | return; |
| 825 | } |
| 826 | mUpdateListeners.remove(listener); |
| 827 | if (mUpdateListeners.size() == 0) { |
| 828 | mUpdateListeners = null; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | |
| 833 | /** |
| 834 | * The time interpolator used in calculating the elapsed fraction of this animation. The |
| 835 | * interpolator determines whether the animation runs with linear or non-linear motion, |
| 836 | * such as acceleration and deceleration. The default value is |
| 837 | * {@link android.view.animation.AccelerateDecelerateInterpolator} |
| 838 | * |
Chet Haase | 27c1d4d | 2010-12-16 07:58:28 -0800 | [diff] [blame] | 839 | * @param value the interpolator to be used by this animation. A value of <code>null</code> |
| 840 | * will result in linear interpolation. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 841 | */ |
| 842 | @Override |
Chet Haase | e0ee2e9 | 2010-10-07 09:06:18 -0700 | [diff] [blame] | 843 | public void setInterpolator(TimeInterpolator value) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 844 | if (value != null) { |
| 845 | mInterpolator = value; |
Chet Haase | 27c1d4d | 2010-12-16 07:58:28 -0800 | [diff] [blame] | 846 | } else { |
| 847 | mInterpolator = new LinearInterpolator(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | |
| 851 | /** |
| 852 | * Returns the timing interpolator that this ValueAnimator uses. |
| 853 | * |
| 854 | * @return The timing interpolator for this ValueAnimator. |
| 855 | */ |
Chet Haase | 430742f | 2013-04-12 11:18:36 -0700 | [diff] [blame] | 856 | @Override |
Chet Haase | e0ee2e9 | 2010-10-07 09:06:18 -0700 | [diff] [blame] | 857 | public TimeInterpolator getInterpolator() { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 858 | return mInterpolator; |
| 859 | } |
| 860 | |
| 861 | /** |
| 862 | * The type evaluator to be used when calculating the animated values of this animation. |
Chet Haase | b2ab04f | 2011-01-16 11:03:22 -0800 | [diff] [blame] | 863 | * The system will automatically assign a float or int evaluator based on the type |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 864 | * of <code>startValue</code> and <code>endValue</code> in the constructor. But if these values |
| 865 | * are not one of these primitive types, or if different evaluation is desired (such as is |
| 866 | * necessary with int values that represent colors), a custom evaluator needs to be assigned. |
Chet Haase | 53ee331 | 2011-01-10 15:56:56 -0800 | [diff] [blame] | 867 | * For example, when running an animation on color values, the {@link ArgbEvaluator} |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 868 | * should be used to get correct RGB color interpolation. |
| 869 | * |
| 870 | * <p>If this ValueAnimator has only one set of values being animated between, this evaluator |
| 871 | * will be used for that set. If there are several sets of values being animated, which is |
Chet Haase | fdd3ad7 | 2013-04-24 16:38:20 -0700 | [diff] [blame] | 872 | * the case if PropertyValuesHolder objects were set on the ValueAnimator, then the evaluator |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 873 | * is assigned just to the first PropertyValuesHolder object.</p> |
| 874 | * |
| 875 | * @param value the evaluator to be used this animation |
| 876 | */ |
| 877 | public void setEvaluator(TypeEvaluator value) { |
| 878 | if (value != null && mValues != null && mValues.length > 0) { |
| 879 | mValues[0].setEvaluator(value); |
| 880 | } |
| 881 | } |
| 882 | |
Chet Haase | 17cf42c | 2012-04-17 13:18:14 -0700 | [diff] [blame] | 883 | private void notifyStartListeners() { |
| 884 | if (mListeners != null && !mStartListenersCalled) { |
| 885 | ArrayList<AnimatorListener> tmpListeners = |
| 886 | (ArrayList<AnimatorListener>) mListeners.clone(); |
| 887 | int numListeners = tmpListeners.size(); |
| 888 | for (int i = 0; i < numListeners; ++i) { |
| 889 | tmpListeners.get(i).onAnimationStart(this); |
| 890 | } |
| 891 | } |
| 892 | mStartListenersCalled = true; |
| 893 | } |
| 894 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 895 | /** |
| 896 | * Start the animation playing. This version of start() takes a boolean flag that indicates |
| 897 | * whether the animation should play in reverse. The flag is usually false, but may be set |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 898 | * to true if called from the reverse() method. |
| 899 | * |
| 900 | * <p>The animation started by calling this method will be run on the thread that called |
| 901 | * this method. This thread should have a Looper on it (a runtime exception will be thrown if |
| 902 | * this is not the case). Also, if the animation will animate |
| 903 | * properties of objects in the view hierarchy, then the calling thread should be the UI |
| 904 | * thread for that view hierarchy.</p> |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 905 | * |
| 906 | * @param playBackwards Whether the ValueAnimator should start playing in reverse. |
| 907 | */ |
| 908 | private void start(boolean playBackwards) { |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 909 | if (Looper.myLooper() == null) { |
| 910 | throw new AndroidRuntimeException("Animators may only be run on Looper threads"); |
Jim Miller | 3060421 | 2010-09-22 19:56:23 -0700 | [diff] [blame] | 911 | } |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 912 | mPlayingBackwards = playBackwards; |
Chet Haase | add6577 | 2011-02-09 16:47:29 -0800 | [diff] [blame] | 913 | mCurrentIteration = 0; |
| 914 | mPlayingState = STOPPED; |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 915 | mStarted = true; |
Chet Haase | add6577 | 2011-02-09 16:47:29 -0800 | [diff] [blame] | 916 | mStartedDelay = false; |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 917 | AnimationHandler animationHandler = getOrCreateAnimationHandler(); |
| 918 | animationHandler.mPendingAnimations.add(this); |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 919 | if (mStartDelay == 0) { |
Chet Haase | add6577 | 2011-02-09 16:47:29 -0800 | [diff] [blame] | 920 | // This sets the initial value of the animation, prior to actually starting it running |
Jeff Brown | 59bbef0 | 2012-05-07 16:43:25 -0700 | [diff] [blame] | 921 | setCurrentPlayTime(0); |
Chet Haase | 154f145 | 2011-02-23 16:53:18 -0800 | [diff] [blame] | 922 | mPlayingState = STOPPED; |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 923 | mRunning = true; |
Chet Haase | 17cf42c | 2012-04-17 13:18:14 -0700 | [diff] [blame] | 924 | notifyStartListeners(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 925 | } |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 926 | animationHandler.start(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | @Override |
| 930 | public void start() { |
| 931 | start(false); |
| 932 | } |
| 933 | |
| 934 | @Override |
| 935 | public void cancel() { |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 936 | // Only cancel if the animation is actually running or has been started and is about |
| 937 | // to run |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 938 | AnimationHandler handler = getOrCreateAnimationHandler(); |
| 939 | if (mPlayingState != STOPPED |
| 940 | || handler.mPendingAnimations.contains(this) |
| 941 | || handler.mDelayedAnims.contains(this)) { |
Chet Haase | b8f574a | 2011-08-03 14:10:06 -0700 | [diff] [blame] | 942 | // Only notify listeners if the animator has actually started |
Chet Haase | 17cf42c | 2012-04-17 13:18:14 -0700 | [diff] [blame] | 943 | if ((mStarted || mRunning) && mListeners != null) { |
| 944 | if (!mRunning) { |
| 945 | // If it's not yet running, then start listeners weren't called. Call them now. |
| 946 | notifyStartListeners(); |
| 947 | } |
Chet Haase | 7dfacdb | 2011-07-11 17:01:56 -0700 | [diff] [blame] | 948 | ArrayList<AnimatorListener> tmpListeners = |
| 949 | (ArrayList<AnimatorListener>) mListeners.clone(); |
| 950 | for (AnimatorListener listener : tmpListeners) { |
| 951 | listener.onAnimationCancel(this); |
| 952 | } |
| 953 | } |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 954 | endAnimation(handler); |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 955 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | @Override |
| 959 | public void end() { |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 960 | AnimationHandler handler = getOrCreateAnimationHandler(); |
| 961 | if (!handler.mAnimations.contains(this) && !handler.mPendingAnimations.contains(this)) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 962 | // Special case if the animation has not yet started; get it ready for ending |
| 963 | mStartedDelay = false; |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 964 | startAnimation(handler); |
Chet Haase | 17cf42c | 2012-04-17 13:18:14 -0700 | [diff] [blame] | 965 | mStarted = true; |
Chet Haase | add6577 | 2011-02-09 16:47:29 -0800 | [diff] [blame] | 966 | } else if (!mInitialized) { |
| 967 | initAnimation(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 968 | } |
Chet Haase | 4dd17686 | 2012-09-12 16:33:26 -0700 | [diff] [blame] | 969 | animateValue(mPlayingBackwards ? 0f : 1f); |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 970 | endAnimation(handler); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | @Override |
| 974 | public boolean isRunning() { |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 975 | return (mPlayingState == RUNNING || mRunning); |
| 976 | } |
| 977 | |
| 978 | @Override |
| 979 | public boolean isStarted() { |
| 980 | return mStarted; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | /** |
| 984 | * Plays the ValueAnimator in reverse. If the animation is already running, |
| 985 | * it will stop itself and play backwards from the point reached when reverse was called. |
| 986 | * If the animation is not currently running, then it will start from the end and |
| 987 | * play backwards. This behavior is only set for the current animation; future playing |
| 988 | * of the animation will use the default behavior of playing forward. |
| 989 | */ |
| 990 | public void reverse() { |
| 991 | mPlayingBackwards = !mPlayingBackwards; |
| 992 | if (mPlayingState == RUNNING) { |
| 993 | long currentTime = AnimationUtils.currentAnimationTimeMillis(); |
| 994 | long currentPlayTime = currentTime - mStartTime; |
| 995 | long timeLeft = mDuration - currentPlayTime; |
| 996 | mStartTime = currentTime - timeLeft; |
| 997 | } else { |
| 998 | start(true); |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | /** |
| 1003 | * Called internally to end an animation by removing it from the animations list. Must be |
| 1004 | * called on the UI thread. |
| 1005 | */ |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 1006 | private void endAnimation(AnimationHandler handler) { |
| 1007 | handler.mAnimations.remove(this); |
| 1008 | handler.mPendingAnimations.remove(this); |
| 1009 | handler.mDelayedAnims.remove(this); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1010 | mPlayingState = STOPPED; |
Chet Haase | 17cf42c | 2012-04-17 13:18:14 -0700 | [diff] [blame] | 1011 | if ((mStarted || mRunning) && mListeners != null) { |
| 1012 | if (!mRunning) { |
| 1013 | // If it's not yet running, then start listeners weren't called. Call them now. |
| 1014 | notifyStartListeners(); |
| 1015 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1016 | ArrayList<AnimatorListener> tmpListeners = |
| 1017 | (ArrayList<AnimatorListener>) mListeners.clone(); |
Chet Haase | 7c608f2 | 2010-10-22 17:54:04 -0700 | [diff] [blame] | 1018 | int numListeners = tmpListeners.size(); |
| 1019 | for (int i = 0; i < numListeners; ++i) { |
| 1020 | tmpListeners.get(i).onAnimationEnd(this); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1021 | } |
| 1022 | } |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 1023 | mRunning = false; |
Chet Haase | b8f574a | 2011-08-03 14:10:06 -0700 | [diff] [blame] | 1024 | mStarted = false; |
Chet Haase | 17cf42c | 2012-04-17 13:18:14 -0700 | [diff] [blame] | 1025 | mStartListenersCalled = false; |
Chet Haase | caf4648 | 2013-03-15 13:04:42 -0700 | [diff] [blame] | 1026 | mPlayingBackwards = false; |
Chet Haase | 9b80ca8 | 2013-06-04 09:37:38 -0700 | [diff] [blame] | 1027 | if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) { |
| 1028 | Trace.asyncTraceEnd(Trace.TRACE_TAG_VIEW, getNameForTrace(), |
| 1029 | System.identityHashCode(this)); |
| 1030 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | /** |
| 1034 | * Called internally to start an animation by adding it to the active animations list. Must be |
| 1035 | * called on the UI thread. |
| 1036 | */ |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 1037 | private void startAnimation(AnimationHandler handler) { |
Chet Haase | 9b80ca8 | 2013-06-04 09:37:38 -0700 | [diff] [blame] | 1038 | if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) { |
| 1039 | Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW, getNameForTrace(), |
| 1040 | System.identityHashCode(this)); |
| 1041 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1042 | initAnimation(); |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 1043 | handler.mAnimations.add(this); |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 1044 | if (mStartDelay > 0 && mListeners != null) { |
| 1045 | // Listeners were already notified in start() if startDelay is 0; this is |
| 1046 | // just for delayed animations |
Chet Haase | 17cf42c | 2012-04-17 13:18:14 -0700 | [diff] [blame] | 1047 | notifyStartListeners(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | /** |
Chet Haase | fdd3ad7 | 2013-04-24 16:38:20 -0700 | [diff] [blame] | 1052 | * Returns the name of this animator for debugging purposes. |
| 1053 | */ |
| 1054 | String getNameForTrace() { |
| 1055 | return "animator"; |
| 1056 | } |
| 1057 | |
| 1058 | |
| 1059 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1060 | * Internal function called to process an animation frame on an animation that is currently |
| 1061 | * sleeping through its <code>startDelay</code> phase. The return value indicates whether it |
| 1062 | * should be woken up and put on the active animations queue. |
| 1063 | * |
| 1064 | * @param currentTime The current animation time, used to calculate whether the animation |
| 1065 | * has exceeded its <code>startDelay</code> and should be started. |
| 1066 | * @return True if the animation's <code>startDelay</code> has been exceeded and the animation |
| 1067 | * should be added to the set of active animations. |
| 1068 | */ |
| 1069 | private boolean delayedAnimationFrame(long currentTime) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1070 | if (!mStartedDelay) { |
| 1071 | mStartedDelay = true; |
| 1072 | mDelayStartTime = currentTime; |
| 1073 | } else { |
| 1074 | long deltaTime = currentTime - mDelayStartTime; |
| 1075 | if (deltaTime > mStartDelay) { |
| 1076 | // startDelay ended - start the anim and record the |
| 1077 | // mStartTime appropriately |
| 1078 | mStartTime = currentTime - (deltaTime - mStartDelay); |
| 1079 | mPlayingState = RUNNING; |
| 1080 | return true; |
| 1081 | } |
| 1082 | } |
| 1083 | return false; |
| 1084 | } |
| 1085 | |
| 1086 | /** |
| 1087 | * This internal function processes a single animation frame for a given animation. The |
| 1088 | * currentTime parameter is the timing pulse sent by the handler, used to calculate the |
| 1089 | * elapsed duration, and therefore |
| 1090 | * the elapsed fraction, of the animation. The return value indicates whether the animation |
| 1091 | * should be ended (which happens when the elapsed time of the animation exceeds the |
| 1092 | * animation's duration, including the repeatCount). |
| 1093 | * |
| 1094 | * @param currentTime The current time, as tracked by the static timing handler |
| 1095 | * @return true if the animation's duration, including any repetitions due to |
| 1096 | * <code>repeatCount</code> has been exceeded and the animation should be ended. |
| 1097 | */ |
Chet Haase | 051d35e | 2010-12-14 07:20:58 -0800 | [diff] [blame] | 1098 | boolean animationFrame(long currentTime) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1099 | boolean done = false; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1100 | switch (mPlayingState) { |
| 1101 | case RUNNING: |
| 1102 | case SEEKED: |
Chet Haase | 70d4ba1 | 2010-10-06 09:46:45 -0700 | [diff] [blame] | 1103 | float fraction = mDuration > 0 ? (float)(currentTime - mStartTime) / mDuration : 1f; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1104 | if (fraction >= 1f) { |
| 1105 | if (mCurrentIteration < mRepeatCount || mRepeatCount == INFINITE) { |
| 1106 | // Time to repeat |
| 1107 | if (mListeners != null) { |
Chet Haase | 7c608f2 | 2010-10-22 17:54:04 -0700 | [diff] [blame] | 1108 | int numListeners = mListeners.size(); |
| 1109 | for (int i = 0; i < numListeners; ++i) { |
| 1110 | mListeners.get(i).onAnimationRepeat(this); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1111 | } |
| 1112 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1113 | if (mRepeatMode == REVERSE) { |
Romain Guy | 18772ea | 2013-04-10 18:31:22 -0700 | [diff] [blame] | 1114 | mPlayingBackwards = !mPlayingBackwards; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1115 | } |
Chet Haase | 7306668 | 2010-11-29 15:55:32 -0800 | [diff] [blame] | 1116 | mCurrentIteration += (int)fraction; |
| 1117 | fraction = fraction % 1f; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1118 | mStartTime += mDuration; |
| 1119 | } else { |
| 1120 | done = true; |
| 1121 | fraction = Math.min(fraction, 1.0f); |
| 1122 | } |
| 1123 | } |
| 1124 | if (mPlayingBackwards) { |
| 1125 | fraction = 1f - fraction; |
| 1126 | } |
| 1127 | animateValue(fraction); |
| 1128 | break; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | return done; |
| 1132 | } |
| 1133 | |
| 1134 | /** |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 1135 | * Processes a frame of the animation, adjusting the start time if needed. |
| 1136 | * |
| 1137 | * @param frameTime The frame time. |
| 1138 | * @return true if the animation has ended. |
| 1139 | */ |
| 1140 | final boolean doAnimationFrame(long frameTime) { |
| 1141 | if (mPlayingState == STOPPED) { |
| 1142 | mPlayingState = RUNNING; |
| 1143 | if (mSeekTime < 0) { |
| 1144 | mStartTime = frameTime; |
| 1145 | } else { |
| 1146 | mStartTime = frameTime - mSeekTime; |
| 1147 | // Now that we're playing, reset the seek time |
| 1148 | mSeekTime = -1; |
| 1149 | } |
| 1150 | } |
| 1151 | // The frame time might be before the start time during the first frame of |
| 1152 | // an animation. The "current time" must always be on or after the start |
| 1153 | // time to avoid animating frames at negative time intervals. In practice, this |
| 1154 | // is very rare and only happens when seeking backwards. |
| 1155 | final long currentTime = Math.max(frameTime, mStartTime); |
| 1156 | return animationFrame(currentTime); |
| 1157 | } |
| 1158 | |
| 1159 | /** |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 1160 | * Returns the current animation fraction, which is the elapsed/interpolated fraction used in |
| 1161 | * the most recent frame update on the animation. |
| 1162 | * |
| 1163 | * @return Elapsed/interpolated fraction of the animation. |
| 1164 | */ |
| 1165 | public float getAnimatedFraction() { |
| 1166 | return mCurrentFraction; |
| 1167 | } |
| 1168 | |
| 1169 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1170 | * This method is called with the elapsed fraction of the animation during every |
| 1171 | * animation frame. This function turns the elapsed fraction into an interpolated fraction |
| 1172 | * and then into an animated value (from the evaluator. The function is called mostly during |
| 1173 | * animation updates, but it is also called when the <code>end()</code> |
| 1174 | * function is called, to set the final value on the property. |
| 1175 | * |
| 1176 | * <p>Overrides of this method must call the superclass to perform the calculation |
| 1177 | * of the animated value.</p> |
| 1178 | * |
| 1179 | * @param fraction The elapsed fraction of the animation. |
| 1180 | */ |
| 1181 | void animateValue(float fraction) { |
| 1182 | fraction = mInterpolator.getInterpolation(fraction); |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 1183 | mCurrentFraction = fraction; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1184 | int numValues = mValues.length; |
| 1185 | for (int i = 0; i < numValues; ++i) { |
| 1186 | mValues[i].calculateValue(fraction); |
| 1187 | } |
| 1188 | if (mUpdateListeners != null) { |
| 1189 | int numListeners = mUpdateListeners.size(); |
| 1190 | for (int i = 0; i < numListeners; ++i) { |
| 1191 | mUpdateListeners.get(i).onAnimationUpdate(this); |
| 1192 | } |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | @Override |
| 1197 | public ValueAnimator clone() { |
| 1198 | final ValueAnimator anim = (ValueAnimator) super.clone(); |
| 1199 | if (mUpdateListeners != null) { |
| 1200 | ArrayList<AnimatorUpdateListener> oldListeners = mUpdateListeners; |
| 1201 | anim.mUpdateListeners = new ArrayList<AnimatorUpdateListener>(); |
| 1202 | int numListeners = oldListeners.size(); |
| 1203 | for (int i = 0; i < numListeners; ++i) { |
| 1204 | anim.mUpdateListeners.add(oldListeners.get(i)); |
| 1205 | } |
| 1206 | } |
| 1207 | anim.mSeekTime = -1; |
| 1208 | anim.mPlayingBackwards = false; |
| 1209 | anim.mCurrentIteration = 0; |
| 1210 | anim.mInitialized = false; |
| 1211 | anim.mPlayingState = STOPPED; |
| 1212 | anim.mStartedDelay = false; |
| 1213 | PropertyValuesHolder[] oldValues = mValues; |
| 1214 | if (oldValues != null) { |
| 1215 | int numValues = oldValues.length; |
| 1216 | anim.mValues = new PropertyValuesHolder[numValues]; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1217 | anim.mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues); |
| 1218 | for (int i = 0; i < numValues; ++i) { |
Chet Haase | d4dd702 | 2011-04-04 15:25:09 -0700 | [diff] [blame] | 1219 | PropertyValuesHolder newValuesHolder = oldValues[i].clone(); |
| 1220 | anim.mValues[i] = newValuesHolder; |
| 1221 | anim.mValuesMap.put(newValuesHolder.getPropertyName(), newValuesHolder); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1222 | } |
| 1223 | } |
| 1224 | return anim; |
| 1225 | } |
| 1226 | |
| 1227 | /** |
| 1228 | * Implementors of this interface can add themselves as update listeners |
| 1229 | * to an <code>ValueAnimator</code> instance to receive callbacks on every animation |
| 1230 | * frame, after the current frame's values have been calculated for that |
| 1231 | * <code>ValueAnimator</code>. |
| 1232 | */ |
| 1233 | public static interface AnimatorUpdateListener { |
| 1234 | /** |
| 1235 | * <p>Notifies the occurrence of another frame of the animation.</p> |
| 1236 | * |
| 1237 | * @param animation The animation which was repeated. |
| 1238 | */ |
| 1239 | void onAnimationUpdate(ValueAnimator animation); |
| 1240 | |
| 1241 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 1242 | |
| 1243 | /** |
| 1244 | * Return the number of animations currently running. |
| 1245 | * |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 1246 | * Used by StrictMode internally to annotate violations. |
| 1247 | * May be called on arbitrary threads! |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 1248 | * |
| 1249 | * @hide |
| 1250 | */ |
| 1251 | public static int getCurrentAnimationsCount() { |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 1252 | AnimationHandler handler = sAnimationHandler.get(); |
| 1253 | return handler != null ? handler.mAnimations.size() : 0; |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 1254 | } |
Patrick Dubroy | 8901ffa | 2011-01-16 17:13:55 -0800 | [diff] [blame] | 1255 | |
| 1256 | /** |
| 1257 | * Clear all animations on this thread, without canceling or ending them. |
| 1258 | * This should be used with caution. |
| 1259 | * |
| 1260 | * @hide |
| 1261 | */ |
| 1262 | public static void clearAllAnimations() { |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 1263 | AnimationHandler handler = sAnimationHandler.get(); |
| 1264 | if (handler != null) { |
| 1265 | handler.mAnimations.clear(); |
| 1266 | handler.mPendingAnimations.clear(); |
| 1267 | handler.mDelayedAnims.clear(); |
| 1268 | } |
| 1269 | } |
| 1270 | |
Romain Guy | 18772ea | 2013-04-10 18:31:22 -0700 | [diff] [blame] | 1271 | private static AnimationHandler getOrCreateAnimationHandler() { |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 1272 | AnimationHandler handler = sAnimationHandler.get(); |
| 1273 | if (handler == null) { |
| 1274 | handler = new AnimationHandler(); |
| 1275 | sAnimationHandler.set(handler); |
| 1276 | } |
| 1277 | return handler; |
Patrick Dubroy | 8901ffa | 2011-01-16 17:13:55 -0800 | [diff] [blame] | 1278 | } |
Chet Haase | e9140a7 | 2011-02-16 16:23:29 -0800 | [diff] [blame] | 1279 | |
| 1280 | @Override |
| 1281 | public String toString() { |
| 1282 | String returnVal = "ValueAnimator@" + Integer.toHexString(hashCode()); |
| 1283 | if (mValues != null) { |
| 1284 | for (int i = 0; i < mValues.length; ++i) { |
| 1285 | returnVal += "\n " + mValues[i].toString(); |
| 1286 | } |
| 1287 | } |
| 1288 | return returnVal; |
| 1289 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 1290 | } |