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 | |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 19 | import android.annotation.CallSuper; |
George Mount | 7764b92 | 2016-01-13 14:51:33 -0800 | [diff] [blame^] | 20 | import android.annotation.IntDef; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 21 | import android.os.Looper; |
Romain Guy | 18772ea | 2013-04-10 18:31:22 -0700 | [diff] [blame] | 22 | import android.os.Trace; |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 23 | import android.util.AndroidRuntimeException; |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 24 | import android.util.Log; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 25 | import android.view.animation.AccelerateDecelerateInterpolator; |
| 26 | import android.view.animation.AnimationUtils; |
Chet Haase | 27c1d4d | 2010-12-16 07:58:28 -0800 | [diff] [blame] | 27 | import android.view.animation.LinearInterpolator; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 28 | |
George Mount | 7764b92 | 2016-01-13 14:51:33 -0800 | [diff] [blame^] | 29 | import java.lang.annotation.Retention; |
| 30 | import java.lang.annotation.RetentionPolicy; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 31 | import java.util.ArrayList; |
| 32 | import java.util.HashMap; |
| 33 | |
| 34 | /** |
| 35 | * This class provides a simple timing engine for running animations |
| 36 | * which calculate animated values and set them on target objects. |
| 37 | * |
| 38 | * <p>There is a single timing pulse that all animations use. It runs in a |
| 39 | * custom handler to ensure that property changes happen on the UI thread.</p> |
| 40 | * |
| 41 | * <p>By default, ValueAnimator uses non-linear time interpolation, via the |
| 42 | * {@link AccelerateDecelerateInterpolator} class, which accelerates into and decelerates |
| 43 | * out of an animation. This behavior can be changed by calling |
Chet Haase | e0ee2e9 | 2010-10-07 09:06:18 -0700 | [diff] [blame] | 44 | * {@link ValueAnimator#setInterpolator(TimeInterpolator)}.</p> |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 45 | * |
Chet Haase | d430753 | 2014-12-01 06:32:38 -0800 | [diff] [blame] | 46 | * <p>Animators can be created from either code or resource files. Here is an example |
| 47 | * of a ValueAnimator resource file:</p> |
| 48 | * |
| 49 | * {@sample development/samples/ApiDemos/res/anim/animator.xml ValueAnimatorResources} |
| 50 | * |
| 51 | * <p>It is also possible to use a combination of {@link PropertyValuesHolder} and |
| 52 | * {@link Keyframe} resource tags to create a multi-step animation. |
| 53 | * Note that you can specify explicit fractional values (from 0 to 1) for |
| 54 | * each keyframe to determine when, in the overall duration, the animation should arrive at that |
| 55 | * value. Alternatively, you can leave the fractions off and the keyframes will be equally |
| 56 | * distributed within the total duration:</p> |
| 57 | * |
| 58 | * {@sample development/samples/ApiDemos/res/anim/value_animator_pvh_kf.xml |
| 59 | * ValueAnimatorKeyframeResources} |
| 60 | * |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 61 | * <div class="special reference"> |
| 62 | * <h3>Developer Guides</h3> |
| 63 | * <p>For more information about animating with {@code ValueAnimator}, read the |
| 64 | * <a href="{@docRoot}guide/topics/graphics/prop-animation.html#value-animator">Property |
| 65 | * Animation</a> developer guide.</p> |
| 66 | * </div> |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 67 | */ |
Romain Guy | 18772ea | 2013-04-10 18:31:22 -0700 | [diff] [blame] | 68 | @SuppressWarnings("unchecked") |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 69 | public class ValueAnimator extends Animator implements AnimationHandler.AnimationFrameCallback { |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 70 | private static final String TAG = "ValueAnimator"; |
| 71 | private static final boolean DEBUG = false; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Internal constants |
| 75 | */ |
Chet Haase | d21a9fe | 2012-02-01 14:14:17 -0800 | [diff] [blame] | 76 | private static float sDurationScale = 1.0f; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 77 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 78 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 79 | * Internal variables |
| 80 | * NOTE: This object implements the clone() method, making a deep copy of any referenced |
| 81 | * objects. As other non-trivial fields are added to this class, make sure to add logic |
| 82 | * to clone() to make deep copies of them. |
| 83 | */ |
| 84 | |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 85 | /** |
| 86 | * The first time that the animation's animateFrame() method is called. This time is used to |
| 87 | * determine elapsed time (and therefore the elapsed fraction) in subsequent calls |
| 88 | * to animateFrame(). |
| 89 | * |
| 90 | * Whenever mStartTime is set, you must also update mStartTimeCommitted. |
| 91 | */ |
Chet Haase | 051d35e | 2010-12-14 07:20:58 -0800 | [diff] [blame] | 92 | long mStartTime; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 93 | |
| 94 | /** |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 95 | * When true, the start time has been firmly committed as a chosen reference point in |
| 96 | * time by which the progress of the animation will be evaluated. When false, the |
| 97 | * start time may be updated when the first animation frame is committed so as |
| 98 | * to compensate for jank that may have occurred between when the start time was |
| 99 | * initialized and when the frame was actually drawn. |
| 100 | * |
| 101 | * This flag is generally set to false during the first frame of the animation |
| 102 | * when the animation playing state transitions from STOPPED to RUNNING or |
| 103 | * resumes after having been paused. This flag is set to true when the start time |
| 104 | * is firmly committed and should not be further compensated for jank. |
| 105 | */ |
| 106 | boolean mStartTimeCommitted; |
| 107 | |
| 108 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 109 | * Set when setCurrentPlayTime() is called. If negative, animation is not currently seeked |
| 110 | * to a value. |
| 111 | */ |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 112 | float mSeekFraction = -1; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 113 | |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Set on the next frame after pause() is called, used to calculate a new startTime |
| 116 | * or delayStartTime which allows the animator to continue from the point at which |
| 117 | * it was paused. If negative, has not yet been set. |
| 118 | */ |
| 119 | private long mPauseTime; |
| 120 | |
| 121 | /** |
| 122 | * Set when an animator is resumed. This triggers logic in the next frame which |
| 123 | * actually resumes the animator. |
| 124 | */ |
| 125 | private boolean mResumed = false; |
| 126 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 127 | // 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] | 128 | private static final TimeInterpolator sDefaultInterpolator = |
| 129 | new AccelerateDecelerateInterpolator(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 130 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 131 | /** |
Chet Haase | f4e3bab | 2014-12-02 17:51:34 -0800 | [diff] [blame] | 132 | * Flag to indicate whether this animator is playing in reverse mode, specifically |
| 133 | * by being started or interrupted by a call to reverse(). This flag is different than |
| 134 | * mPlayingBackwards, which indicates merely whether the current iteration of the |
| 135 | * animator is playing in reverse. It is used in corner cases to determine proper end |
| 136 | * behavior. |
| 137 | */ |
| 138 | private boolean mReversing; |
| 139 | |
| 140 | /** |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 141 | * Tracks the overall fraction of the animation, ranging from 0 to mRepeatCount + 1 |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 142 | */ |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 143 | private float mOverallFraction = 0f; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 144 | |
| 145 | /** |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 146 | * Tracks current elapsed/eased fraction, for querying in getAnimatedFraction(). |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 147 | * This is calculated by interpolating the fraction (range: [0, 1]) in the current iteration. |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 148 | */ |
| 149 | private float mCurrentFraction = 0f; |
| 150 | |
| 151 | /** |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 152 | * Tracks the time (in milliseconds) when the last frame arrived. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 153 | */ |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 154 | private long mLastFrameTime = 0; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 155 | |
| 156 | /** |
Chet Haase | b8f574a | 2011-08-03 14:10:06 -0700 | [diff] [blame] | 157 | * Additional playing state to indicate whether an animator has been start()'d. There is |
| 158 | * some lag between a call to start() and the first animation frame. We should still note |
| 159 | * that the animation has been started, even if it's first animation frame has not yet |
| 160 | * happened, and reflect that state in isRunning(). |
| 161 | * Note that delayed animations are different: they are not started until their first |
| 162 | * animation frame, which occurs after their delay elapses. |
| 163 | */ |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 164 | private boolean mRunning = false; |
| 165 | |
| 166 | /** |
| 167 | * Additional playing state to indicate whether an animator has been start()'d, whether or |
| 168 | * not there is a nonzero startDelay. |
| 169 | */ |
Chet Haase | b8f574a | 2011-08-03 14:10:06 -0700 | [diff] [blame] | 170 | private boolean mStarted = false; |
| 171 | |
| 172 | /** |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 173 | * Tracks whether we've notified listeners of the onAnimationStart() event. This can be |
Chet Haase | 17cf42c | 2012-04-17 13:18:14 -0700 | [diff] [blame] | 174 | * complex to keep track of since we notify listeners at different times depending on |
| 175 | * startDelay and whether start() was called before end(). |
| 176 | */ |
| 177 | private boolean mStartListenersCalled = false; |
| 178 | |
| 179 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 180 | * Flag that denotes whether the animation is set up and ready to go. Used to |
| 181 | * set up animation that has not yet been started. |
| 182 | */ |
| 183 | boolean mInitialized = false; |
| 184 | |
Doris Liu | 3dbaae1 | 2015-08-27 14:56:30 -0700 | [diff] [blame] | 185 | /** |
| 186 | * Flag that tracks whether animation has been requested to end. |
| 187 | */ |
| 188 | private boolean mAnimationEndRequested = false; |
| 189 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 190 | // |
| 191 | // Backing variables |
| 192 | // |
| 193 | |
| 194 | // How long the animation should last in ms |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 195 | private long mDuration = 300; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 196 | |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 197 | // The amount of time in ms to delay starting the animation after start() is called. Note |
| 198 | // that this start delay is unscaled. When there is a duration scale set on the animator, the |
| 199 | // scaling factor will be applied to this delay. |
| 200 | private long mStartDelay = 0; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 201 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 202 | // The number of times the animation will repeat. The default is 0, which means the animation |
| 203 | // will play only once |
| 204 | private int mRepeatCount = 0; |
| 205 | |
| 206 | /** |
| 207 | * The type of repetition that will occur when repeatMode is nonzero. RESTART means the |
| 208 | * animation will start from the beginning on every new cycle. REVERSE means the animation |
| 209 | * will reverse directions on each iteration. |
| 210 | */ |
| 211 | private int mRepeatMode = RESTART; |
| 212 | |
| 213 | /** |
| 214 | * The time interpolator to be used. The elapsed fraction of the animation will be passed |
| 215 | * through this interpolator to calculate the interpolated fraction, which is then used to |
| 216 | * calculate the animated values. |
| 217 | */ |
Chet Haase | e0ee2e9 | 2010-10-07 09:06:18 -0700 | [diff] [blame] | 218 | private TimeInterpolator mInterpolator = sDefaultInterpolator; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 219 | |
| 220 | /** |
| 221 | * The set of listeners to be sent events through the life of an animation. |
| 222 | */ |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 223 | ArrayList<AnimatorUpdateListener> mUpdateListeners = null; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 224 | |
| 225 | /** |
| 226 | * The property/value sets being animated. |
| 227 | */ |
| 228 | PropertyValuesHolder[] mValues; |
| 229 | |
| 230 | /** |
| 231 | * A hashmap of the PropertyValuesHolder objects. This map is used to lookup animated values |
| 232 | * by property name during calls to getAnimatedValue(String). |
| 233 | */ |
| 234 | HashMap<String, PropertyValuesHolder> mValuesMap; |
| 235 | |
| 236 | /** |
| 237 | * Public constants |
| 238 | */ |
| 239 | |
George Mount | 7764b92 | 2016-01-13 14:51:33 -0800 | [diff] [blame^] | 240 | /** @hide */ |
| 241 | @IntDef({RESTART, REVERSE}) |
| 242 | @Retention(RetentionPolicy.SOURCE) |
| 243 | public @interface RepeatMode {} |
| 244 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 245 | /** |
| 246 | * When the animation reaches the end and <code>repeatCount</code> is INFINITE |
| 247 | * or a positive value, the animation restarts from the beginning. |
| 248 | */ |
| 249 | public static final int RESTART = 1; |
| 250 | /** |
| 251 | * When the animation reaches the end and <code>repeatCount</code> is INFINITE |
| 252 | * or a positive value, the animation reverses direction on every iteration. |
| 253 | */ |
| 254 | public static final int REVERSE = 2; |
| 255 | /** |
| 256 | * This value used used with the {@link #setRepeatCount(int)} property to repeat |
| 257 | * the animation indefinitely. |
| 258 | */ |
| 259 | public static final int INFINITE = -1; |
| 260 | |
Chet Haase | c38fa1f | 2012-02-01 16:37:46 -0800 | [diff] [blame] | 261 | /** |
| 262 | * @hide |
| 263 | */ |
| 264 | public static void setDurationScale(float durationScale) { |
| 265 | sDurationScale = durationScale; |
| 266 | } |
| 267 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 268 | /** |
Jeff Brown | ff7e6ef | 2012-08-15 02:05:18 -0700 | [diff] [blame] | 269 | * @hide |
| 270 | */ |
| 271 | public static float getDurationScale() { |
| 272 | return sDurationScale; |
| 273 | } |
| 274 | |
| 275 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 276 | * Creates a new ValueAnimator object. This default constructor is primarily for |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 277 | * use internally; the factory methods which take parameters are more generally |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 278 | * useful. |
| 279 | */ |
| 280 | public ValueAnimator() { |
| 281 | } |
| 282 | |
| 283 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 284 | * Constructs and returns a ValueAnimator that animates between int values. A single |
| 285 | * value implies that that value is the one being animated to. However, this is not typically |
| 286 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 287 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 288 | * from the target object and property being animated). Therefore, there should typically |
| 289 | * be two or more values. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 290 | * |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 291 | * @param values A set of values that the animation will animate between over time. |
| 292 | * @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] | 293 | */ |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 294 | public static ValueAnimator ofInt(int... values) { |
| 295 | ValueAnimator anim = new ValueAnimator(); |
| 296 | anim.setIntValues(values); |
| 297 | return anim; |
| 298 | } |
| 299 | |
| 300 | /** |
George Mount | 1ffb280 | 2013-10-09 16:13:54 -0700 | [diff] [blame] | 301 | * Constructs and returns a ValueAnimator that animates between color values. A single |
| 302 | * value implies that that value is the one being animated to. However, this is not typically |
| 303 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 304 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 305 | * from the target object and property being animated). Therefore, there should typically |
| 306 | * be two or more values. |
| 307 | * |
| 308 | * @param values A set of values that the animation will animate between over time. |
| 309 | * @return A ValueAnimator object that is set up to animate between the given values. |
| 310 | */ |
| 311 | public static ValueAnimator ofArgb(int... values) { |
| 312 | ValueAnimator anim = new ValueAnimator(); |
| 313 | anim.setIntValues(values); |
| 314 | anim.setEvaluator(ArgbEvaluator.getInstance()); |
| 315 | return anim; |
| 316 | } |
| 317 | |
| 318 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 319 | * Constructs and returns a ValueAnimator that animates between float values. A single |
| 320 | * value implies that that value is the one being animated to. However, this is not typically |
| 321 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 322 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 323 | * from the target object and property being animated). Therefore, there should typically |
| 324 | * be two or more values. |
| 325 | * |
| 326 | * @param values A set of values that the animation will animate between over time. |
| 327 | * @return A ValueAnimator object that is set up to animate between the given values. |
| 328 | */ |
| 329 | public static ValueAnimator ofFloat(float... values) { |
| 330 | ValueAnimator anim = new ValueAnimator(); |
| 331 | anim.setFloatValues(values); |
| 332 | return anim; |
| 333 | } |
| 334 | |
| 335 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 336 | * Constructs and returns a ValueAnimator that animates between the values |
| 337 | * specified in the PropertyValuesHolder objects. |
| 338 | * |
| 339 | * @param values A set of PropertyValuesHolder objects whose values will be animated |
| 340 | * between over time. |
| 341 | * @return A ValueAnimator object that is set up to animate between the given values. |
| 342 | */ |
| 343 | public static ValueAnimator ofPropertyValuesHolder(PropertyValuesHolder... values) { |
| 344 | ValueAnimator anim = new ValueAnimator(); |
| 345 | anim.setValues(values); |
| 346 | return anim; |
| 347 | } |
| 348 | /** |
| 349 | * Constructs and returns a ValueAnimator that animates between Object values. A single |
| 350 | * value implies that that value is the one being animated to. However, this is not typically |
| 351 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 352 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 353 | * from the target object and property being animated). Therefore, there should typically |
| 354 | * be two or more values. |
| 355 | * |
| 356 | * <p>Since ValueAnimator does not know how to animate between arbitrary Objects, this |
| 357 | * factory method also takes a TypeEvaluator object that the ValueAnimator will use |
| 358 | * to perform that interpolation. |
| 359 | * |
| 360 | * @param evaluator A TypeEvaluator that will be called on each animation frame to |
| 361 | * provide the ncessry interpolation between the Object values to derive the animated |
| 362 | * value. |
| 363 | * @param values A set of values that the animation will animate between over time. |
| 364 | * @return A ValueAnimator object that is set up to animate between the given values. |
| 365 | */ |
| 366 | public static ValueAnimator ofObject(TypeEvaluator evaluator, Object... values) { |
| 367 | ValueAnimator anim = new ValueAnimator(); |
| 368 | anim.setObjectValues(values); |
| 369 | anim.setEvaluator(evaluator); |
| 370 | return anim; |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Sets int values that will be animated between. A single |
| 375 | * value implies that that value is the one being animated to. However, this is not typically |
| 376 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 377 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 378 | * from the target object and property being animated). Therefore, there should typically |
| 379 | * be two or more values. |
| 380 | * |
| 381 | * <p>If there are already multiple sets of values defined for this ValueAnimator via more |
| 382 | * than one PropertyValuesHolder object, this method will set the values for the first |
| 383 | * of those objects.</p> |
| 384 | * |
| 385 | * @param values A set of values that the animation will animate between over time. |
| 386 | */ |
| 387 | public void setIntValues(int... values) { |
| 388 | if (values == null || values.length == 0) { |
| 389 | return; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 390 | } |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 391 | if (mValues == null || mValues.length == 0) { |
Romain Guy | 18772ea | 2013-04-10 18:31:22 -0700 | [diff] [blame] | 392 | setValues(PropertyValuesHolder.ofInt("", values)); |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 393 | } else { |
| 394 | PropertyValuesHolder valuesHolder = mValues[0]; |
| 395 | valuesHolder.setIntValues(values); |
| 396 | } |
| 397 | // New property/values/target should cause re-initialization prior to starting |
| 398 | mInitialized = false; |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Sets float values that will be animated between. A single |
| 403 | * value implies that that value is the one being animated to. However, this is not typically |
| 404 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 405 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 406 | * from the target object and property being animated). Therefore, there should typically |
| 407 | * be two or more values. |
| 408 | * |
| 409 | * <p>If there are already multiple sets of values defined for this ValueAnimator via more |
| 410 | * than one PropertyValuesHolder object, this method will set the values for the first |
| 411 | * of those objects.</p> |
| 412 | * |
| 413 | * @param values A set of values that the animation will animate between over time. |
| 414 | */ |
| 415 | public void setFloatValues(float... values) { |
| 416 | if (values == null || values.length == 0) { |
| 417 | return; |
| 418 | } |
| 419 | if (mValues == null || mValues.length == 0) { |
Romain Guy | 18772ea | 2013-04-10 18:31:22 -0700 | [diff] [blame] | 420 | setValues(PropertyValuesHolder.ofFloat("", values)); |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 421 | } else { |
| 422 | PropertyValuesHolder valuesHolder = mValues[0]; |
| 423 | valuesHolder.setFloatValues(values); |
| 424 | } |
| 425 | // New property/values/target should cause re-initialization prior to starting |
| 426 | mInitialized = false; |
| 427 | } |
| 428 | |
| 429 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 430 | * Sets the values to animate between for this animation. A single |
| 431 | * value implies that that value is the one being animated to. However, this is not typically |
| 432 | * useful in a ValueAnimator object because there is no way for the object to determine the |
| 433 | * starting value for the animation (unlike ObjectAnimator, which can derive that value |
| 434 | * from the target object and property being animated). Therefore, there should typically |
| 435 | * be two or more values. |
| 436 | * |
| 437 | * <p>If there are already multiple sets of values defined for this ValueAnimator via more |
| 438 | * than one PropertyValuesHolder object, this method will set the values for the first |
| 439 | * of those objects.</p> |
| 440 | * |
| 441 | * <p>There should be a TypeEvaluator set on the ValueAnimator that knows how to interpolate |
| 442 | * between these value objects. ValueAnimator only knows how to interpolate between the |
| 443 | * primitive types specified in the other setValues() methods.</p> |
| 444 | * |
| 445 | * @param values The set of values to animate between. |
| 446 | */ |
| 447 | public void setObjectValues(Object... values) { |
| 448 | if (values == null || values.length == 0) { |
| 449 | return; |
| 450 | } |
| 451 | if (mValues == null || mValues.length == 0) { |
Romain Guy | 18772ea | 2013-04-10 18:31:22 -0700 | [diff] [blame] | 452 | setValues(PropertyValuesHolder.ofObject("", null, values)); |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 453 | } else { |
| 454 | PropertyValuesHolder valuesHolder = mValues[0]; |
| 455 | valuesHolder.setObjectValues(values); |
| 456 | } |
| 457 | // New property/values/target should cause re-initialization prior to starting |
| 458 | mInitialized = false; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /** |
| 462 | * 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] | 463 | * 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] | 464 | * be constructed without values and this method can be called to set the values manually |
| 465 | * instead. |
| 466 | * |
| 467 | * @param values The set of values, per property, being animated between. |
| 468 | */ |
| 469 | public void setValues(PropertyValuesHolder... values) { |
| 470 | int numValues = values.length; |
| 471 | mValues = values; |
| 472 | mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues); |
| 473 | for (int i = 0; i < numValues; ++i) { |
Romain Guy | 18772ea | 2013-04-10 18:31:22 -0700 | [diff] [blame] | 474 | PropertyValuesHolder valuesHolder = values[i]; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 475 | mValuesMap.put(valuesHolder.getPropertyName(), valuesHolder); |
| 476 | } |
Chet Haase | 0e0590b | 2010-09-26 11:57:28 -0700 | [diff] [blame] | 477 | // New property/values/target should cause re-initialization prior to starting |
| 478 | mInitialized = false; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Returns the values that this ValueAnimator animates between. These values are stored in |
| 483 | * PropertyValuesHolder objects, even if the ValueAnimator was created with a simple list |
| 484 | * of value objects instead. |
| 485 | * |
| 486 | * @return PropertyValuesHolder[] An array of PropertyValuesHolder objects which hold the |
| 487 | * values, per property, that define the animation. |
| 488 | */ |
| 489 | public PropertyValuesHolder[] getValues() { |
| 490 | return mValues; |
| 491 | } |
| 492 | |
| 493 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 494 | * This function is called immediately before processing the first animation |
| 495 | * frame of an animation. If there is a nonzero <code>startDelay</code>, the |
| 496 | * function is called after that delay ends. |
| 497 | * It takes care of the final initialization steps for the |
| 498 | * animation. |
| 499 | * |
| 500 | * <p>Overrides of this method should call the superclass method to ensure |
| 501 | * that internal mechanisms for the animation are set up correctly.</p> |
| 502 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 503 | @CallSuper |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 504 | void initAnimation() { |
| 505 | if (!mInitialized) { |
| 506 | int numValues = mValues.length; |
| 507 | for (int i = 0; i < numValues; ++i) { |
| 508 | mValues[i].init(); |
| 509 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 510 | mInitialized = true; |
| 511 | } |
| 512 | } |
| 513 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 514 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 515 | * Sets the length of the animation. The default duration is 300 milliseconds. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 516 | * |
Chet Haase | 27c1d4d | 2010-12-16 07:58:28 -0800 | [diff] [blame] | 517 | * @param duration The length of the animation, in milliseconds. This value cannot |
| 518 | * be negative. |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 519 | * @return ValueAnimator The object called with setDuration(). This return |
| 520 | * value makes it easier to compose statements together that construct and then set the |
| 521 | * duration, as in <code>ValueAnimator.ofInt(0, 10).setDuration(500).start()</code>. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 522 | */ |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 523 | @Override |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 524 | public ValueAnimator setDuration(long duration) { |
Chet Haase | 27c1d4d | 2010-12-16 07:58:28 -0800 | [diff] [blame] | 525 | if (duration < 0) { |
| 526 | throw new IllegalArgumentException("Animators cannot have negative duration: " + |
| 527 | duration); |
| 528 | } |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 529 | mDuration = duration; |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 530 | return this; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 533 | private long getScaledDuration() { |
| 534 | return (long)(mDuration * sDurationScale); |
Jeff Brown | 7a08fe0 | 2014-10-07 15:55:35 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 537 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 538 | * Gets the length of the animation. The default duration is 300 milliseconds. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 539 | * |
| 540 | * @return The length of the animation, in milliseconds. |
| 541 | */ |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 542 | @Override |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 543 | public long getDuration() { |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 544 | return mDuration; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 547 | @Override |
| 548 | public long getTotalDuration() { |
| 549 | if (mRepeatCount == INFINITE) { |
| 550 | return DURATION_INFINITE; |
| 551 | } else { |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 552 | return mStartDelay + (mDuration * (mRepeatCount + 1)); |
Doris Liu | 1309914 | 2015-07-10 17:32:41 -0700 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
| 556 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 557 | * Sets the position of the animation to the specified point in time. This time should |
| 558 | * be between 0 and the total duration of the animation, including any repetition. If |
| 559 | * the animation has not yet been started, then it will not advance forward after it is |
| 560 | * set to this time; it will simply set the time to this value and perform any appropriate |
| 561 | * actions based on that time. If the animation is already running, then setCurrentPlayTime() |
| 562 | * will set the current playing time to this value and continue playing from that point. |
| 563 | * |
| 564 | * @param playTime The time, in milliseconds, to which the animation is advanced or rewound. |
| 565 | */ |
| 566 | public void setCurrentPlayTime(long playTime) { |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 567 | float fraction = mDuration > 0 ? (float) playTime / mDuration : 1; |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 568 | setCurrentFraction(fraction); |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * Sets the position of the animation to the specified fraction. This fraction should |
| 573 | * be between 0 and the total fraction of the animation, including any repetition. That is, |
| 574 | * a fraction of 0 will position the animation at the beginning, a value of 1 at the end, |
Chet Haase | f4e3bab | 2014-12-02 17:51:34 -0800 | [diff] [blame] | 575 | * and a value of 2 at the end of a reversing animator that repeats once. If |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 576 | * the animation has not yet been started, then it will not advance forward after it is |
| 577 | * set to this fraction; it will simply set the fraction to this value and perform any |
| 578 | * appropriate actions based on that fraction. If the animation is already running, then |
| 579 | * setCurrentFraction() will set the current fraction to this value and continue |
Eino-Ville Talvala | 5a5afe0 | 2014-12-05 11:11:45 -0800 | [diff] [blame] | 580 | * playing from that point. {@link Animator.AnimatorListener} events are not called |
Chet Haase | f4e3bab | 2014-12-02 17:51:34 -0800 | [diff] [blame] | 581 | * due to changing the fraction; those events are only processed while the animation |
| 582 | * is running. |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 583 | * |
Chet Haase | f4e3bab | 2014-12-02 17:51:34 -0800 | [diff] [blame] | 584 | * @param fraction The fraction to which the animation is advanced or rewound. Values |
| 585 | * outside the range of 0 to the maximum fraction for the animator will be clamped to |
| 586 | * the correct range. |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 587 | */ |
| 588 | public void setCurrentFraction(float fraction) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 589 | initAnimation(); |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 590 | fraction = clampFraction(fraction); |
| 591 | long seekTime = (long) (getScaledDuration() * fraction); |
Chet Haase | f4e3bab | 2014-12-02 17:51:34 -0800 | [diff] [blame] | 592 | long currentTime = AnimationUtils.currentAnimationTimeMillis(); |
| 593 | mStartTime = currentTime - seekTime; |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 594 | mStartTimeCommitted = true; // do not allow start time to be compensated for jank |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 595 | if (!mRunning) { |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 596 | mSeekFraction = fraction; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 597 | } |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 598 | mOverallFraction = fraction; |
| 599 | final float currentIterationFraction = getCurrentIterationFraction(fraction); |
| 600 | animateValue(currentIterationFraction); |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * Calculates current iteration based on the overall fraction. The overall fraction will be |
| 605 | * in the range of [0, mRepeatCount + 1]. Both current iteration and fraction in the current |
| 606 | * iteration can be derived from it. |
| 607 | */ |
| 608 | private int getCurrentIteration(float fraction) { |
| 609 | fraction = clampFraction(fraction); |
| 610 | // If the overall fraction is a positive integer, we consider the current iteration to be |
| 611 | // complete. In other words, the fraction for the current iteration would be 1, and the |
| 612 | // current iteration would be overall fraction - 1. |
| 613 | double iteration = Math.floor(fraction); |
| 614 | if (fraction == iteration && fraction > 0) { |
| 615 | iteration--; |
Chet Haase | f4e3bab | 2014-12-02 17:51:34 -0800 | [diff] [blame] | 616 | } |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 617 | return (int) iteration; |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * Calculates the fraction of the current iteration, taking into account whether the animation |
| 622 | * should be played backwards. E.g. When the animation is played backwards in an iteration, |
| 623 | * the fraction for that iteration will go from 1f to 0f. |
| 624 | */ |
| 625 | private float getCurrentIterationFraction(float fraction) { |
| 626 | fraction = clampFraction(fraction); |
| 627 | int iteration = getCurrentIteration(fraction); |
| 628 | float currentFraction = fraction - iteration; |
| 629 | return shouldPlayBackward(iteration) ? 1f - currentFraction : currentFraction; |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | * Clamps fraction into the correct range: [0, mRepeatCount + 1]. If repeat count is infinite, |
| 634 | * no upper bound will be set for the fraction. |
| 635 | * |
| 636 | * @param fraction fraction to be clamped |
| 637 | * @return fraction clamped into the range of [0, mRepeatCount + 1] |
| 638 | */ |
| 639 | private float clampFraction(float fraction) { |
| 640 | if (fraction < 0) { |
| 641 | fraction = 0; |
| 642 | } else if (mRepeatCount != INFINITE) { |
| 643 | fraction = Math.min(fraction, mRepeatCount + 1); |
| 644 | } |
| 645 | return fraction; |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * Calculates the direction of animation playing (i.e. forward or backward), based on 1) |
| 650 | * whether the entire animation is being reversed, 2) repeat mode applied to the current |
| 651 | * iteration. |
| 652 | */ |
| 653 | private boolean shouldPlayBackward(int iteration) { |
| 654 | if (iteration > 0 && mRepeatMode == REVERSE && |
| 655 | (iteration < (mRepeatCount + 1) || mRepeatCount == INFINITE)) { |
| 656 | // if we were seeked to some other iteration in a reversing animator, |
| 657 | // figure out the correct direction to start playing based on the iteration |
| 658 | if (mReversing) { |
| 659 | return (iteration % 2) == 0; |
| 660 | } else { |
| 661 | return (iteration % 2) != 0; |
| 662 | } |
| 663 | } else { |
| 664 | return mReversing; |
| 665 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | /** |
| 669 | * Gets the current position of the animation in time, which is equal to the current |
| 670 | * time minus the time that the animation started. An animation that is not yet started will |
Chet Haase | 4365e5a | 2015-10-06 19:32:57 -0700 | [diff] [blame] | 671 | * return a value of zero, unless the animation has has its play time set via |
| 672 | * {@link #setCurrentPlayTime(long)} or {@link #setCurrentFraction(float)}, in which case |
| 673 | * it will return the time that was set. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 674 | * |
| 675 | * @return The current position in time of the animation. |
| 676 | */ |
| 677 | public long getCurrentPlayTime() { |
Chet Haase | 4365e5a | 2015-10-06 19:32:57 -0700 | [diff] [blame] | 678 | if (!mInitialized || (!mStarted && mSeekFraction < 0)) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 679 | return 0; |
| 680 | } |
Chet Haase | 4365e5a | 2015-10-06 19:32:57 -0700 | [diff] [blame] | 681 | if (mSeekFraction >= 0) { |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 682 | return (long) (mDuration * mSeekFraction); |
Chet Haase | 4365e5a | 2015-10-06 19:32:57 -0700 | [diff] [blame] | 683 | } |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 684 | float durationScale = sDurationScale == 0 ? 1 : sDurationScale; |
| 685 | return (long) ((AnimationUtils.currentAnimationTimeMillis() - mStartTime) / durationScale); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 689 | * The amount of time, in milliseconds, to delay starting the animation after |
| 690 | * {@link #start()} is called. |
| 691 | * |
| 692 | * @return the number of milliseconds to delay running the animation |
| 693 | */ |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 694 | @Override |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 695 | public long getStartDelay() { |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 696 | return mStartDelay; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | /** |
| 700 | * The amount of time, in milliseconds, to delay starting the animation after |
| 701 | * {@link #start()} is called. |
| 702 | |
| 703 | * @param startDelay The amount of the delay, in milliseconds |
| 704 | */ |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 705 | @Override |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 706 | public void setStartDelay(long startDelay) { |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 707 | mStartDelay = startDelay; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /** |
| 711 | * The amount of time, in milliseconds, between each frame of the animation. This is a |
| 712 | * requested time that the animation will attempt to honor, but the actual delay between |
| 713 | * frames may be different, depending on system load and capabilities. This is a static |
| 714 | * function because the same delay will be applied to all animations, since they are all |
| 715 | * run off of a single timing loop. |
| 716 | * |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 717 | * The frame delay may be ignored when the animation system uses an external timing |
| 718 | * source, such as the display refresh rate (vsync), to govern animations. |
| 719 | * |
Doris Liu | 2b2e2c8 | 2015-10-01 11:08:54 -0700 | [diff] [blame] | 720 | * Note that this method should be called from the same thread that {@link #start()} is |
| 721 | * called in order to check the frame delay for that animation. A runtime exception will be |
| 722 | * thrown if the calling thread does not have a Looper. |
| 723 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 724 | * @return the requested time between frames, in milliseconds |
| 725 | */ |
| 726 | public static long getFrameDelay() { |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 727 | return AnimationHandler.getInstance().getFrameDelay(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | /** |
| 731 | * The amount of time, in milliseconds, between each frame of the animation. This is a |
| 732 | * requested time that the animation will attempt to honor, but the actual delay between |
| 733 | * frames may be different, depending on system load and capabilities. This is a static |
| 734 | * function because the same delay will be applied to all animations, since they are all |
| 735 | * run off of a single timing loop. |
| 736 | * |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 737 | * The frame delay may be ignored when the animation system uses an external timing |
| 738 | * source, such as the display refresh rate (vsync), to govern animations. |
| 739 | * |
Doris Liu | 2b2e2c8 | 2015-10-01 11:08:54 -0700 | [diff] [blame] | 740 | * Note that this method should be called from the same thread that {@link #start()} is |
| 741 | * called in order to have the new frame delay take effect on that animation. A runtime |
| 742 | * exception will be thrown if the calling thread does not have a Looper. |
| 743 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 744 | * @param frameDelay the requested time between frames, in milliseconds |
| 745 | */ |
| 746 | public static void setFrameDelay(long frameDelay) { |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 747 | AnimationHandler.getInstance().setFrameDelay(frameDelay); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | /** |
| 751 | * The most recent value calculated by this <code>ValueAnimator</code> when there is just one |
| 752 | * property being animated. This value is only sensible while the animation is running. The main |
| 753 | * purpose for this read-only property is to retrieve the value from the <code>ValueAnimator</code> |
| 754 | * during a call to {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which |
| 755 | * is called during each animation frame, immediately after the value is calculated. |
| 756 | * |
| 757 | * @return animatedValue The value most recently calculated by this <code>ValueAnimator</code> for |
| 758 | * the single property being animated. If there are several properties being animated |
| 759 | * (specified by several PropertyValuesHolder objects in the constructor), this function |
| 760 | * returns the animated value for the first of those objects. |
| 761 | */ |
| 762 | public Object getAnimatedValue() { |
| 763 | if (mValues != null && mValues.length > 0) { |
| 764 | return mValues[0].getAnimatedValue(); |
| 765 | } |
| 766 | // Shouldn't get here; should always have values unless ValueAnimator was set up wrong |
| 767 | return null; |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * The most recent value calculated by this <code>ValueAnimator</code> for <code>propertyName</code>. |
| 772 | * The main purpose for this read-only property is to retrieve the value from the |
| 773 | * <code>ValueAnimator</code> during a call to |
| 774 | * {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which |
| 775 | * is called during each animation frame, immediately after the value is calculated. |
| 776 | * |
| 777 | * @return animatedValue The value most recently calculated for the named property |
| 778 | * by this <code>ValueAnimator</code>. |
| 779 | */ |
| 780 | public Object getAnimatedValue(String propertyName) { |
| 781 | PropertyValuesHolder valuesHolder = mValuesMap.get(propertyName); |
| 782 | if (valuesHolder != null) { |
| 783 | return valuesHolder.getAnimatedValue(); |
| 784 | } else { |
| 785 | // At least avoid crashing if called with bogus propertyName |
| 786 | return null; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | /** |
| 791 | * Sets how many times the animation should be repeated. If the repeat |
| 792 | * count is 0, the animation is never repeated. If the repeat count is |
| 793 | * greater than 0 or {@link #INFINITE}, the repeat mode will be taken |
| 794 | * into account. The repeat count is 0 by default. |
| 795 | * |
| 796 | * @param value the number of times the animation should be repeated |
| 797 | */ |
| 798 | public void setRepeatCount(int value) { |
| 799 | mRepeatCount = value; |
| 800 | } |
| 801 | /** |
| 802 | * Defines how many times the animation should repeat. The default value |
| 803 | * is 0. |
| 804 | * |
| 805 | * @return the number of times the animation should repeat, or {@link #INFINITE} |
| 806 | */ |
| 807 | public int getRepeatCount() { |
| 808 | return mRepeatCount; |
| 809 | } |
| 810 | |
| 811 | /** |
| 812 | * Defines what this animation should do when it reaches the end. This |
| 813 | * setting is applied only when the repeat count is either greater than |
| 814 | * 0 or {@link #INFINITE}. Defaults to {@link #RESTART}. |
| 815 | * |
| 816 | * @param value {@link #RESTART} or {@link #REVERSE} |
| 817 | */ |
George Mount | 7764b92 | 2016-01-13 14:51:33 -0800 | [diff] [blame^] | 818 | public void setRepeatMode(@RepeatMode int value) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 819 | mRepeatMode = value; |
| 820 | } |
| 821 | |
| 822 | /** |
| 823 | * Defines what this animation should do when it reaches the end. |
| 824 | * |
| 825 | * @return either one of {@link #REVERSE} or {@link #RESTART} |
| 826 | */ |
George Mount | 7764b92 | 2016-01-13 14:51:33 -0800 | [diff] [blame^] | 827 | @RepeatMode |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 828 | public int getRepeatMode() { |
| 829 | return mRepeatMode; |
| 830 | } |
| 831 | |
| 832 | /** |
| 833 | * Adds a listener to the set of listeners that are sent update events through the life of |
| 834 | * an animation. This method is called on all listeners for every frame of the animation, |
| 835 | * after the values for the animation have been calculated. |
| 836 | * |
| 837 | * @param listener the listener to be added to the current set of listeners for this animation. |
| 838 | */ |
| 839 | public void addUpdateListener(AnimatorUpdateListener listener) { |
| 840 | if (mUpdateListeners == null) { |
| 841 | mUpdateListeners = new ArrayList<AnimatorUpdateListener>(); |
| 842 | } |
| 843 | mUpdateListeners.add(listener); |
| 844 | } |
| 845 | |
| 846 | /** |
Jim Miller | 3060421 | 2010-09-22 19:56:23 -0700 | [diff] [blame] | 847 | * Removes all listeners from the set listening to frame updates for this animation. |
| 848 | */ |
| 849 | public void removeAllUpdateListeners() { |
| 850 | if (mUpdateListeners == null) { |
| 851 | return; |
| 852 | } |
| 853 | mUpdateListeners.clear(); |
| 854 | mUpdateListeners = null; |
| 855 | } |
| 856 | |
| 857 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 858 | * Removes a listener from the set listening to frame updates for this animation. |
| 859 | * |
| 860 | * @param listener the listener to be removed from the current set of update listeners |
| 861 | * for this animation. |
| 862 | */ |
| 863 | public void removeUpdateListener(AnimatorUpdateListener listener) { |
| 864 | if (mUpdateListeners == null) { |
| 865 | return; |
| 866 | } |
| 867 | mUpdateListeners.remove(listener); |
| 868 | if (mUpdateListeners.size() == 0) { |
| 869 | mUpdateListeners = null; |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | |
| 874 | /** |
| 875 | * The time interpolator used in calculating the elapsed fraction of this animation. The |
| 876 | * interpolator determines whether the animation runs with linear or non-linear motion, |
| 877 | * such as acceleration and deceleration. The default value is |
| 878 | * {@link android.view.animation.AccelerateDecelerateInterpolator} |
| 879 | * |
Chet Haase | 27c1d4d | 2010-12-16 07:58:28 -0800 | [diff] [blame] | 880 | * @param value the interpolator to be used by this animation. A value of <code>null</code> |
| 881 | * will result in linear interpolation. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 882 | */ |
| 883 | @Override |
Chet Haase | e0ee2e9 | 2010-10-07 09:06:18 -0700 | [diff] [blame] | 884 | public void setInterpolator(TimeInterpolator value) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 885 | if (value != null) { |
| 886 | mInterpolator = value; |
Chet Haase | 27c1d4d | 2010-12-16 07:58:28 -0800 | [diff] [blame] | 887 | } else { |
| 888 | mInterpolator = new LinearInterpolator(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 889 | } |
| 890 | } |
| 891 | |
| 892 | /** |
| 893 | * Returns the timing interpolator that this ValueAnimator uses. |
| 894 | * |
| 895 | * @return The timing interpolator for this ValueAnimator. |
| 896 | */ |
Chet Haase | 430742f | 2013-04-12 11:18:36 -0700 | [diff] [blame] | 897 | @Override |
Chet Haase | e0ee2e9 | 2010-10-07 09:06:18 -0700 | [diff] [blame] | 898 | public TimeInterpolator getInterpolator() { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 899 | return mInterpolator; |
| 900 | } |
| 901 | |
| 902 | /** |
| 903 | * 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] | 904 | * 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] | 905 | * of <code>startValue</code> and <code>endValue</code> in the constructor. But if these values |
| 906 | * are not one of these primitive types, or if different evaluation is desired (such as is |
| 907 | * 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] | 908 | * For example, when running an animation on color values, the {@link ArgbEvaluator} |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 909 | * should be used to get correct RGB color interpolation. |
| 910 | * |
| 911 | * <p>If this ValueAnimator has only one set of values being animated between, this evaluator |
| 912 | * 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] | 913 | * 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] | 914 | * is assigned just to the first PropertyValuesHolder object.</p> |
| 915 | * |
| 916 | * @param value the evaluator to be used this animation |
| 917 | */ |
| 918 | public void setEvaluator(TypeEvaluator value) { |
| 919 | if (value != null && mValues != null && mValues.length > 0) { |
| 920 | mValues[0].setEvaluator(value); |
| 921 | } |
| 922 | } |
| 923 | |
Chet Haase | 17cf42c | 2012-04-17 13:18:14 -0700 | [diff] [blame] | 924 | private void notifyStartListeners() { |
| 925 | if (mListeners != null && !mStartListenersCalled) { |
| 926 | ArrayList<AnimatorListener> tmpListeners = |
| 927 | (ArrayList<AnimatorListener>) mListeners.clone(); |
| 928 | int numListeners = tmpListeners.size(); |
| 929 | for (int i = 0; i < numListeners; ++i) { |
| 930 | tmpListeners.get(i).onAnimationStart(this); |
| 931 | } |
| 932 | } |
| 933 | mStartListenersCalled = true; |
| 934 | } |
| 935 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 936 | /** |
| 937 | * Start the animation playing. This version of start() takes a boolean flag that indicates |
| 938 | * 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] | 939 | * to true if called from the reverse() method. |
| 940 | * |
| 941 | * <p>The animation started by calling this method will be run on the thread that called |
| 942 | * this method. This thread should have a Looper on it (a runtime exception will be thrown if |
| 943 | * this is not the case). Also, if the animation will animate |
| 944 | * properties of objects in the view hierarchy, then the calling thread should be the UI |
| 945 | * thread for that view hierarchy.</p> |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 946 | * |
| 947 | * @param playBackwards Whether the ValueAnimator should start playing in reverse. |
| 948 | */ |
| 949 | private void start(boolean playBackwards) { |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 950 | if (Looper.myLooper() == null) { |
| 951 | throw new AndroidRuntimeException("Animators may only be run on Looper threads"); |
Jim Miller | 3060421 | 2010-09-22 19:56:23 -0700 | [diff] [blame] | 952 | } |
Chet Haase | f4e3bab | 2014-12-02 17:51:34 -0800 | [diff] [blame] | 953 | mReversing = playBackwards; |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 954 | // Special case: reversing from seek-to-0 should act as if not seeked at all. |
| 955 | if (playBackwards && mSeekFraction != -1 && mSeekFraction != 0) { |
| 956 | if (mRepeatCount == INFINITE) { |
| 957 | // Calculate the fraction of the current iteration. |
| 958 | float fraction = (float) (mSeekFraction - Math.floor(mSeekFraction)); |
| 959 | mSeekFraction = 1 - fraction; |
Chet Haase | f4e3bab | 2014-12-02 17:51:34 -0800 | [diff] [blame] | 960 | } else { |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 961 | mSeekFraction = 1 + mRepeatCount - mSeekFraction; |
Chet Haase | f4e3bab | 2014-12-02 17:51:34 -0800 | [diff] [blame] | 962 | } |
| 963 | } |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 964 | mStarted = true; |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 965 | mPaused = false; |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 966 | mRunning = false; |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 967 | AnimationHandler animationHandler = AnimationHandler.getInstance(); |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 968 | animationHandler.addAnimationFrameCallback(this, (long) (mStartDelay * sDurationScale)); |
Doris Liu | f57bfe2 | 2015-10-01 13:26:01 -0700 | [diff] [blame] | 969 | |
| 970 | if (mStartDelay == 0) { |
| 971 | // If there's no start delay, init the animation and notify start listeners right away |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 972 | // to be consistent with the previous behavior. Otherwise, postpone this until the first |
| 973 | // frame after the start delay. |
Doris Liu | f57bfe2 | 2015-10-01 13:26:01 -0700 | [diff] [blame] | 974 | startAnimation(); |
| 975 | setCurrentFraction(mSeekFraction == -1 ? 0 : mSeekFraction); |
| 976 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | @Override |
| 980 | public void start() { |
| 981 | start(false); |
| 982 | } |
| 983 | |
| 984 | @Override |
| 985 | public void cancel() { |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 986 | if (Looper.myLooper() == null) { |
| 987 | throw new AndroidRuntimeException("Animators may only be run on Looper threads"); |
| 988 | } |
Doris Liu | 3dbaae1 | 2015-08-27 14:56:30 -0700 | [diff] [blame] | 989 | |
| 990 | // If end has already been requested, through a previous end() or cancel() call, no-op |
| 991 | // until animation starts again. |
| 992 | if (mAnimationEndRequested) { |
| 993 | return; |
| 994 | } |
| 995 | |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 996 | // Only cancel if the animation is actually running or has been started and is about |
| 997 | // to run |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 998 | // Only notify listeners if the animator has actually started |
| 999 | if ((mStarted || mRunning) && mListeners != null) { |
| 1000 | if (!mRunning) { |
| 1001 | // If it's not yet running, then start listeners weren't called. Call them now. |
| 1002 | notifyStartListeners(); |
Chet Haase | 7dfacdb | 2011-07-11 17:01:56 -0700 | [diff] [blame] | 1003 | } |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1004 | ArrayList<AnimatorListener> tmpListeners = |
| 1005 | (ArrayList<AnimatorListener>) mListeners.clone(); |
| 1006 | for (AnimatorListener listener : tmpListeners) { |
| 1007 | listener.onAnimationCancel(this); |
| 1008 | } |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 1009 | } |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1010 | endAnimation(); |
| 1011 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | @Override |
| 1015 | public void end() { |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1016 | if (Looper.myLooper() == null) { |
| 1017 | throw new AndroidRuntimeException("Animators may only be run on Looper threads"); |
| 1018 | } |
| 1019 | if (!mRunning) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1020 | // Special case if the animation has not yet started; get it ready for ending |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1021 | startAnimation(); |
Chet Haase | 17cf42c | 2012-04-17 13:18:14 -0700 | [diff] [blame] | 1022 | mStarted = true; |
Chet Haase | add6577 | 2011-02-09 16:47:29 -0800 | [diff] [blame] | 1023 | } else if (!mInitialized) { |
| 1024 | initAnimation(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1025 | } |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 1026 | animateValue(shouldPlayBackward(mRepeatCount) ? 0f : 1f); |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1027 | endAnimation(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | @Override |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 1031 | public void resume() { |
| 1032 | if (mPaused) { |
| 1033 | mResumed = true; |
| 1034 | } |
| 1035 | super.resume(); |
| 1036 | } |
| 1037 | |
| 1038 | @Override |
| 1039 | public void pause() { |
| 1040 | boolean previouslyPaused = mPaused; |
| 1041 | super.pause(); |
| 1042 | if (!previouslyPaused && mPaused) { |
| 1043 | mPauseTime = -1; |
| 1044 | mResumed = false; |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | @Override |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1049 | public boolean isRunning() { |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1050 | return mRunning; |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | @Override |
| 1054 | public boolean isStarted() { |
| 1055 | return mStarted; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | /** |
| 1059 | * Plays the ValueAnimator in reverse. If the animation is already running, |
| 1060 | * it will stop itself and play backwards from the point reached when reverse was called. |
| 1061 | * If the animation is not currently running, then it will start from the end and |
| 1062 | * play backwards. This behavior is only set for the current animation; future playing |
| 1063 | * of the animation will use the default behavior of playing forward. |
| 1064 | */ |
ztenghui | 7bc6a3f | 2014-07-15 15:12:12 -0700 | [diff] [blame] | 1065 | @Override |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1066 | public void reverse() { |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1067 | if (mRunning) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1068 | long currentTime = AnimationUtils.currentAnimationTimeMillis(); |
| 1069 | long currentPlayTime = currentTime - mStartTime; |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 1070 | long timeLeft = getScaledDuration() - currentPlayTime; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1071 | mStartTime = currentTime - timeLeft; |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 1072 | mStartTimeCommitted = true; // do not allow start time to be compensated for jank |
Chet Haase | f4e3bab | 2014-12-02 17:51:34 -0800 | [diff] [blame] | 1073 | mReversing = !mReversing; |
Chet Haase | f43fb2a | 2013-09-06 07:59:36 -0700 | [diff] [blame] | 1074 | } else if (mStarted) { |
| 1075 | end(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1076 | } else { |
| 1077 | start(true); |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | /** |
ztenghui | 7bc6a3f | 2014-07-15 15:12:12 -0700 | [diff] [blame] | 1082 | * @hide |
| 1083 | */ |
| 1084 | @Override |
| 1085 | public boolean canReverse() { |
| 1086 | return true; |
| 1087 | } |
| 1088 | |
| 1089 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1090 | * Called internally to end an animation by removing it from the animations list. Must be |
| 1091 | * called on the UI thread. |
| 1092 | */ |
Doris Liu | 3dbaae1 | 2015-08-27 14:56:30 -0700 | [diff] [blame] | 1093 | private void endAnimation() { |
| 1094 | if (mAnimationEndRequested) { |
| 1095 | return; |
| 1096 | } |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1097 | AnimationHandler handler = AnimationHandler.getInstance(); |
| 1098 | handler.removeCallback(this); |
Doris Liu | 3dbaae1 | 2015-08-27 14:56:30 -0700 | [diff] [blame] | 1099 | |
| 1100 | mAnimationEndRequested = true; |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 1101 | mPaused = false; |
Chet Haase | 17cf42c | 2012-04-17 13:18:14 -0700 | [diff] [blame] | 1102 | if ((mStarted || mRunning) && mListeners != null) { |
| 1103 | if (!mRunning) { |
| 1104 | // If it's not yet running, then start listeners weren't called. Call them now. |
| 1105 | notifyStartListeners(); |
| 1106 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1107 | ArrayList<AnimatorListener> tmpListeners = |
| 1108 | (ArrayList<AnimatorListener>) mListeners.clone(); |
Chet Haase | 7c608f2 | 2010-10-22 17:54:04 -0700 | [diff] [blame] | 1109 | int numListeners = tmpListeners.size(); |
| 1110 | for (int i = 0; i < numListeners; ++i) { |
| 1111 | tmpListeners.get(i).onAnimationEnd(this); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1112 | } |
| 1113 | } |
Chet Haase | 8b69979 | 2011-08-05 15:20:19 -0700 | [diff] [blame] | 1114 | mRunning = false; |
Chet Haase | b8f574a | 2011-08-03 14:10:06 -0700 | [diff] [blame] | 1115 | mStarted = false; |
Chet Haase | 17cf42c | 2012-04-17 13:18:14 -0700 | [diff] [blame] | 1116 | mStartListenersCalled = false; |
Chet Haase | f4e3bab | 2014-12-02 17:51:34 -0800 | [diff] [blame] | 1117 | mReversing = false; |
Doris Liu | f57bfe2 | 2015-10-01 13:26:01 -0700 | [diff] [blame] | 1118 | mLastFrameTime = 0; |
Chet Haase | 9b80ca8 | 2013-06-04 09:37:38 -0700 | [diff] [blame] | 1119 | if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) { |
| 1120 | Trace.asyncTraceEnd(Trace.TRACE_TAG_VIEW, getNameForTrace(), |
| 1121 | System.identityHashCode(this)); |
| 1122 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | /** |
| 1126 | * Called internally to start an animation by adding it to the active animations list. Must be |
| 1127 | * called on the UI thread. |
| 1128 | */ |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1129 | private void startAnimation() { |
Chet Haase | 9b80ca8 | 2013-06-04 09:37:38 -0700 | [diff] [blame] | 1130 | if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) { |
| 1131 | Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW, getNameForTrace(), |
| 1132 | System.identityHashCode(this)); |
| 1133 | } |
Doris Liu | 66d4856 | 2015-11-09 14:44:35 -0800 | [diff] [blame] | 1134 | |
| 1135 | mAnimationEndRequested = false; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1136 | initAnimation(); |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1137 | mRunning = true; |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 1138 | if (mSeekFraction >= 0) { |
| 1139 | mOverallFraction = mSeekFraction; |
| 1140 | } else { |
| 1141 | mOverallFraction = 0f; |
| 1142 | } |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1143 | if (mListeners != null) { |
Chet Haase | 17cf42c | 2012-04-17 13:18:14 -0700 | [diff] [blame] | 1144 | notifyStartListeners(); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | /** |
Chet Haase | fdd3ad7 | 2013-04-24 16:38:20 -0700 | [diff] [blame] | 1149 | * Returns the name of this animator for debugging purposes. |
| 1150 | */ |
| 1151 | String getNameForTrace() { |
| 1152 | return "animator"; |
| 1153 | } |
| 1154 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1155 | /** |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 1156 | * Applies an adjustment to the animation to compensate for jank between when |
| 1157 | * the animation first ran and when the frame was drawn. |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1158 | * @hide |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 1159 | */ |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1160 | public void commitAnimationFrame(long frameTime) { |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 1161 | if (!mStartTimeCommitted) { |
| 1162 | mStartTimeCommitted = true; |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1163 | long adjustment = frameTime - mLastFrameTime; |
| 1164 | if (adjustment > 0) { |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 1165 | mStartTime += adjustment; |
| 1166 | if (DEBUG) { |
| 1167 | Log.d(TAG, "Adjusted start time by " + adjustment + " ms: " + toString()); |
| 1168 | } |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1174 | * This internal function processes a single animation frame for a given animation. The |
| 1175 | * currentTime parameter is the timing pulse sent by the handler, used to calculate the |
| 1176 | * elapsed duration, and therefore |
| 1177 | * the elapsed fraction, of the animation. The return value indicates whether the animation |
| 1178 | * should be ended (which happens when the elapsed time of the animation exceeds the |
| 1179 | * animation's duration, including the repeatCount). |
| 1180 | * |
| 1181 | * @param currentTime The current time, as tracked by the static timing handler |
| 1182 | * @return true if the animation's duration, including any repetitions due to |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1183 | * <code>repeatCount</code> has been exceeded and the animation should be ended. |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1184 | */ |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1185 | boolean animateBasedOnTime(long currentTime) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1186 | boolean done = false; |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1187 | if (mRunning) { |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 1188 | final float fraction = getScaledDuration() > 0 ? |
| 1189 | (float)(currentTime - mStartTime) / getScaledDuration() : 1f; |
| 1190 | final float lastFraction = mOverallFraction; |
| 1191 | final boolean newIteration = (int) fraction > (int) lastFraction; |
| 1192 | final boolean lastIterationFinished = (fraction >= mRepeatCount + 1) && |
| 1193 | (mRepeatCount != INFINITE); |
| 1194 | if (newIteration && !lastIterationFinished) { |
| 1195 | // Time to repeat |
| 1196 | if (mListeners != null) { |
| 1197 | int numListeners = mListeners.size(); |
| 1198 | for (int i = 0; i < numListeners; ++i) { |
| 1199 | mListeners.get(i).onAnimationRepeat(this); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1200 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1201 | } |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 1202 | } else if (lastIterationFinished) { |
| 1203 | done = true; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1204 | } |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 1205 | mOverallFraction = clampFraction(fraction); |
| 1206 | float currentIterationFraction = getCurrentIterationFraction(mOverallFraction); |
| 1207 | animateValue(currentIterationFraction); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1208 | } |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1209 | return done; |
| 1210 | } |
| 1211 | |
| 1212 | /** |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 1213 | * Processes a frame of the animation, adjusting the start time if needed. |
| 1214 | * |
| 1215 | * @param frameTime The frame time. |
| 1216 | * @return true if the animation has ended. |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1217 | * @hide |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 1218 | */ |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1219 | public final void doAnimationFrame(long frameTime) { |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1220 | AnimationHandler handler = AnimationHandler.getInstance(); |
Doris Liu | f57bfe2 | 2015-10-01 13:26:01 -0700 | [diff] [blame] | 1221 | if (mLastFrameTime == 0) { |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1222 | // First frame |
| 1223 | handler.addOneShotCommitCallback(this); |
Doris Liu | f57bfe2 | 2015-10-01 13:26:01 -0700 | [diff] [blame] | 1224 | if (mStartDelay > 0) { |
| 1225 | startAnimation(); |
| 1226 | } |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 1227 | if (mSeekFraction < 0) { |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 1228 | mStartTime = frameTime; |
| 1229 | } else { |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 1230 | long seekTime = (long) (getScaledDuration() * mSeekFraction); |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 1231 | mStartTime = frameTime - seekTime; |
| 1232 | mSeekFraction = -1; |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 1233 | } |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 1234 | mStartTimeCommitted = false; // allow start time to be compensated for jank |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 1235 | } |
Doris Liu | f57bfe2 | 2015-10-01 13:26:01 -0700 | [diff] [blame] | 1236 | mLastFrameTime = frameTime; |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 1237 | if (mPaused) { |
| 1238 | if (mPauseTime < 0) { |
| 1239 | mPauseTime = frameTime; |
| 1240 | } |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1241 | return; |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 1242 | } else if (mResumed) { |
| 1243 | mResumed = false; |
| 1244 | if (mPauseTime > 0) { |
| 1245 | // Offset by the duration that the animation was paused |
| 1246 | mStartTime += (frameTime - mPauseTime); |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 1247 | mStartTimeCommitted = false; // allow start time to be compensated for jank |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 1248 | } |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1249 | handler.addOneShotCommitCallback(this); |
Chet Haase | 8aa1ffb | 2013-08-08 14:00:00 -0700 | [diff] [blame] | 1250 | } |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 1251 | // The frame time might be before the start time during the first frame of |
| 1252 | // an animation. The "current time" must always be on or after the start |
| 1253 | // time to avoid animating frames at negative time intervals. In practice, this |
| 1254 | // is very rare and only happens when seeking backwards. |
| 1255 | final long currentTime = Math.max(frameTime, mStartTime); |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1256 | boolean finished = animateBasedOnTime(currentTime); |
| 1257 | |
| 1258 | if (finished) { |
| 1259 | endAnimation(); |
| 1260 | } |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | /** |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 1264 | * Returns the current animation fraction, which is the elapsed/interpolated fraction used in |
| 1265 | * the most recent frame update on the animation. |
| 1266 | * |
| 1267 | * @return Elapsed/interpolated fraction of the animation. |
| 1268 | */ |
| 1269 | public float getAnimatedFraction() { |
| 1270 | return mCurrentFraction; |
| 1271 | } |
| 1272 | |
| 1273 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1274 | * This method is called with the elapsed fraction of the animation during every |
| 1275 | * animation frame. This function turns the elapsed fraction into an interpolated fraction |
| 1276 | * and then into an animated value (from the evaluator. The function is called mostly during |
| 1277 | * animation updates, but it is also called when the <code>end()</code> |
| 1278 | * function is called, to set the final value on the property. |
| 1279 | * |
| 1280 | * <p>Overrides of this method must call the superclass to perform the calculation |
| 1281 | * of the animated value.</p> |
| 1282 | * |
| 1283 | * @param fraction The elapsed fraction of the animation. |
| 1284 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1285 | @CallSuper |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1286 | void animateValue(float fraction) { |
| 1287 | fraction = mInterpolator.getInterpolation(fraction); |
Chet Haase | a00f386 | 2011-02-22 06:34:40 -0800 | [diff] [blame] | 1288 | mCurrentFraction = fraction; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1289 | int numValues = mValues.length; |
| 1290 | for (int i = 0; i < numValues; ++i) { |
| 1291 | mValues[i].calculateValue(fraction); |
| 1292 | } |
| 1293 | if (mUpdateListeners != null) { |
| 1294 | int numListeners = mUpdateListeners.size(); |
| 1295 | for (int i = 0; i < numListeners; ++i) { |
| 1296 | mUpdateListeners.get(i).onAnimationUpdate(this); |
| 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | @Override |
| 1302 | public ValueAnimator clone() { |
| 1303 | final ValueAnimator anim = (ValueAnimator) super.clone(); |
| 1304 | if (mUpdateListeners != null) { |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 1305 | anim.mUpdateListeners = new ArrayList<AnimatorUpdateListener>(mUpdateListeners); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1306 | } |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 1307 | anim.mSeekFraction = -1; |
Chet Haase | f4e3bab | 2014-12-02 17:51:34 -0800 | [diff] [blame] | 1308 | anim.mReversing = false; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1309 | anim.mInitialized = false; |
ztenghui | 26e9a19 | 2015-04-10 13:14:17 -0700 | [diff] [blame] | 1310 | anim.mStarted = false; |
| 1311 | anim.mRunning = false; |
| 1312 | anim.mPaused = false; |
| 1313 | anim.mResumed = false; |
| 1314 | anim.mStartListenersCalled = false; |
ztenghui | e1b5c2b | 2015-04-21 14:17:00 -0700 | [diff] [blame] | 1315 | anim.mStartTime = 0; |
| 1316 | anim.mStartTimeCommitted = false; |
Doris Liu | 3dbaae1 | 2015-08-27 14:56:30 -0700 | [diff] [blame] | 1317 | anim.mAnimationEndRequested = false; |
ztenghui | e1b5c2b | 2015-04-21 14:17:00 -0700 | [diff] [blame] | 1318 | anim.mPauseTime = 0; |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1319 | anim.mLastFrameTime = 0; |
Doris Liu | fbe94ec | 2015-09-09 14:52:59 -0700 | [diff] [blame] | 1320 | anim.mOverallFraction = 0; |
ztenghui | e1b5c2b | 2015-04-21 14:17:00 -0700 | [diff] [blame] | 1321 | anim.mCurrentFraction = 0; |
ztenghui | 26e9a19 | 2015-04-10 13:14:17 -0700 | [diff] [blame] | 1322 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1323 | PropertyValuesHolder[] oldValues = mValues; |
| 1324 | if (oldValues != null) { |
| 1325 | int numValues = oldValues.length; |
| 1326 | anim.mValues = new PropertyValuesHolder[numValues]; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1327 | anim.mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues); |
| 1328 | for (int i = 0; i < numValues; ++i) { |
Chet Haase | d4dd702 | 2011-04-04 15:25:09 -0700 | [diff] [blame] | 1329 | PropertyValuesHolder newValuesHolder = oldValues[i].clone(); |
| 1330 | anim.mValues[i] = newValuesHolder; |
| 1331 | anim.mValuesMap.put(newValuesHolder.getPropertyName(), newValuesHolder); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1332 | } |
| 1333 | } |
| 1334 | return anim; |
| 1335 | } |
| 1336 | |
| 1337 | /** |
| 1338 | * Implementors of this interface can add themselves as update listeners |
| 1339 | * to an <code>ValueAnimator</code> instance to receive callbacks on every animation |
| 1340 | * frame, after the current frame's values have been calculated for that |
| 1341 | * <code>ValueAnimator</code>. |
| 1342 | */ |
| 1343 | public static interface AnimatorUpdateListener { |
| 1344 | /** |
| 1345 | * <p>Notifies the occurrence of another frame of the animation.</p> |
| 1346 | * |
| 1347 | * @param animation The animation which was repeated. |
| 1348 | */ |
| 1349 | void onAnimationUpdate(ValueAnimator animation); |
| 1350 | |
| 1351 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 1352 | |
| 1353 | /** |
| 1354 | * Return the number of animations currently running. |
| 1355 | * |
Jeff Brown | 9c38dbe | 2011-12-02 16:22:46 -0800 | [diff] [blame] | 1356 | * Used by StrictMode internally to annotate violations. |
| 1357 | * May be called on arbitrary threads! |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 1358 | * |
| 1359 | * @hide |
| 1360 | */ |
| 1361 | public static int getCurrentAnimationsCount() { |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 1362 | return AnimationHandler.getAnimationCount(); |
Patrick Dubroy | 8901ffa | 2011-01-16 17:13:55 -0800 | [diff] [blame] | 1363 | } |
Chet Haase | e9140a7 | 2011-02-16 16:23:29 -0800 | [diff] [blame] | 1364 | |
| 1365 | @Override |
| 1366 | public String toString() { |
| 1367 | String returnVal = "ValueAnimator@" + Integer.toHexString(hashCode()); |
| 1368 | if (mValues != null) { |
| 1369 | for (int i = 0; i < mValues.length; ++i) { |
| 1370 | returnVal += "\n " + mValues[i].toString(); |
| 1371 | } |
| 1372 | } |
| 1373 | return returnVal; |
| 1374 | } |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 1375 | |
| 1376 | /** |
| 1377 | * <p>Whether or not the ValueAnimator is allowed to run asynchronously off of |
| 1378 | * the UI thread. This is a hint that informs the ValueAnimator that it is |
| 1379 | * OK to run the animation off-thread, however ValueAnimator may decide |
| 1380 | * that it must run the animation on the UI thread anyway. For example if there |
| 1381 | * is an {@link AnimatorUpdateListener} the animation will run on the UI thread, |
| 1382 | * regardless of the value of this hint.</p> |
| 1383 | * |
| 1384 | * <p>Regardless of whether or not the animation runs asynchronously, all |
| 1385 | * listener callbacks will be called on the UI thread.</p> |
| 1386 | * |
| 1387 | * <p>To be able to use this hint the following must be true:</p> |
| 1388 | * <ol> |
| 1389 | * <li>{@link #getAnimatedFraction()} is not needed (it will return undefined values).</li> |
| 1390 | * <li>The animator is immutable while {@link #isStarted()} is true. Requests |
| 1391 | * to change values, duration, delay, etc... may be ignored.</li> |
| 1392 | * <li>Lifecycle callback events may be asynchronous. Events such as |
| 1393 | * {@link Animator.AnimatorListener#onAnimationEnd(Animator)} or |
| 1394 | * {@link Animator.AnimatorListener#onAnimationRepeat(Animator)} may end up delayed |
| 1395 | * as they must be posted back to the UI thread, and any actions performed |
| 1396 | * by those callbacks (such as starting new animations) will not happen |
| 1397 | * in the same frame.</li> |
| 1398 | * <li>State change requests ({@link #cancel()}, {@link #end()}, {@link #reverse()}, etc...) |
| 1399 | * may be asynchronous. It is guaranteed that all state changes that are |
| 1400 | * performed on the UI thread in the same frame will be applied as a single |
| 1401 | * atomic update, however that frame may be the current frame, |
| 1402 | * the next frame, or some future frame. This will also impact the observed |
| 1403 | * state of the Animator. For example, {@link #isStarted()} may still return true |
| 1404 | * after a call to {@link #end()}. Using the lifecycle callbacks is preferred over |
| 1405 | * queries to {@link #isStarted()}, {@link #isRunning()}, and {@link #isPaused()} |
| 1406 | * for this reason.</li> |
| 1407 | * </ol> |
| 1408 | * @hide |
| 1409 | */ |
John Reck | c01bd11 | 2014-07-18 16:22:09 -0700 | [diff] [blame] | 1410 | @Override |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 1411 | public void setAllowRunningAsynchronously(boolean mayRunAsync) { |
| 1412 | // It is up to subclasses to support this, if they can. |
| 1413 | } |
Brad Fitzpatrick | 599ca29 | 2010-10-22 14:47:03 -0700 | [diff] [blame] | 1414 | } |