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