ValueAnimator.java revision c6ffab32415a58bbb010dcd115684f9dbc249710
1a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase/* 2a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Copyright (C) 2010 The Android Open Source Project 3a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 4a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Licensed under the Apache License, Version 2.0 (the "License"); 5a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * you may not use this file except in compliance with the License. 6a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * You may obtain a copy of the License at 7a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 8a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * http://www.apache.org/licenses/LICENSE-2.0 9a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 10a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Unless required by applicable law or agreed to in writing, software 11a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * distributed under the License is distributed on an "AS IS" BASIS, 12a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * See the License for the specific language governing permissions and 14a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * limitations under the License. 15a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 16a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 17a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haasepackage android.animation; 18a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 19a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.os.Handler; 20a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.os.Looper; 21a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.os.Message; 22d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haaseimport android.os.SystemProperties; 232970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haaseimport android.util.AndroidRuntimeException; 2496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brownimport android.view.Choreographer; 25a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.view.animation.AccelerateDecelerateInterpolator; 26a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport android.view.animation.AnimationUtils; 2727c1d4debb3848f5accd5673fffeeacad3e61648Chet Haaseimport android.view.animation.LinearInterpolator; 28a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 29a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport java.util.ArrayList; 30a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haaseimport java.util.HashMap; 31a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 32a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase/** 33a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This class provides a simple timing engine for running animations 34a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * which calculate animated values and set them on target objects. 35a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 36a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>There is a single timing pulse that all animations use. It runs in a 37a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * custom handler to ensure that property changes happen on the UI thread.</p> 38a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 39a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>By default, ValueAnimator uses non-linear time interpolation, via the 40a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link AccelerateDecelerateInterpolator} class, which accelerates into and decelerates 41a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * out of an animation. This behavior can be changed by calling 42e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase * {@link ValueAnimator#setInterpolator(TimeInterpolator)}.</p> 433aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * 443aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <div class="special reference"> 453aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <h3>Developer Guides</h3> 463aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <p>For more information about animating with {@code ValueAnimator}, read the 473aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <a href="{@docRoot}guide/topics/graphics/prop-animation.html#value-animator">Property 483aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * Animation</a> developer guide.</p> 493aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * </div> 50a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 512794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haasepublic class ValueAnimator extends Animator { 52a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 53a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 54a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Internal constants 55a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 56d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase private static float sDurationScale = 1.0f; 57a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 58a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 5996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown * Messages sent to timing handler: START is sent when an animation first begins. 60a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 617f9f99ea11051614a7727dfb9f9578b518e76e3cXavier Ducrohet static final int ANIMATION_START = 0; 62a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 63a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 64a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Values used with internal variable mPlayingState to indicate the current state of an 65a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animation. 66a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 67051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase static final int STOPPED = 0; // Not yet playing 68051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase static final int RUNNING = 1; // Playing normally 69051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase static final int SEEKED = 2; // Seeked to some time value 70a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 71a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 72a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Internal variables 73a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * NOTE: This object implements the clone() method, making a deep copy of any referenced 74a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * objects. As other non-trivial fields are added to this class, make sure to add logic 75a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * to clone() to make deep copies of them. 76a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 77a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 78a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // The first time that the animation's animateFrame() method is called. This time is used to 79a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // determine elapsed time (and therefore the elapsed fraction) in subsequent calls 80a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // to animateFrame() 81051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase long mStartTime; 82a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 83a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 84a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Set when setCurrentPlayTime() is called. If negative, animation is not currently seeked 85a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * to a value. 86a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 87051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase long mSeekTime = -1; 88a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 89a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // The static sAnimationHandler processes the internal timing loop on which all animations 90a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // are based 91e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase private static ThreadLocal<AnimationHandler> sAnimationHandler = 92e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase new ThreadLocal<AnimationHandler>(); 93e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase 94a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // The time interpolator to be used if none is set on the animation 95e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase private static final TimeInterpolator sDefaultInterpolator = 96e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase new AccelerateDecelerateInterpolator(); 97a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 98a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 99a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Used to indicate whether the animation is currently playing in reverse. This causes the 100a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * elapsed fraction to be inverted to calculate the appropriate values. 101a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 102a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private boolean mPlayingBackwards = false; 103a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 104a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 105a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This variable tracks the current iteration that is playing. When mCurrentIteration exceeds the 106a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * repeatCount (if repeatCount!=INFINITE), the animation ends 107a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 108a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private int mCurrentIteration = 0; 109a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 110a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 111a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase * Tracks current elapsed/eased fraction, for querying in getAnimatedFraction(). 112a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase */ 113a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase private float mCurrentFraction = 0f; 114a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase 115a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase /** 116a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Tracks whether a startDelay'd animation has begun playing through the startDelay. 117a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 118a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private boolean mStartedDelay = false; 119a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 120a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 121a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Tracks the time at which the animation began playing through its startDelay. This is 122a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * different from the mStartTime variable, which is used to track when the animation became 123a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * active (which is when the startDelay expired and the animation was added to the active 124a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animations list). 125a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 126a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private long mDelayStartTime; 127a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 128a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 129a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Flag that represents the current state of the animation. Used to figure out when to start 130a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * an animation (if state == STOPPED). Also used to end an animation that 131a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * has been cancel()'d or end()'d since the last animation frame. Possible values are 132e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase * STOPPED, RUNNING, SEEKED. 133a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 134051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase int mPlayingState = STOPPED; 135a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 136a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 137b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase * Additional playing state to indicate whether an animator has been start()'d. There is 138b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase * some lag between a call to start() and the first animation frame. We should still note 139b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase * that the animation has been started, even if it's first animation frame has not yet 140b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase * happened, and reflect that state in isRunning(). 141b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase * Note that delayed animations are different: they are not started until their first 142b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase * animation frame, which occurs after their delay elapses. 143b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase */ 1448b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase private boolean mRunning = false; 1458b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase 1468b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase /** 1478b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase * Additional playing state to indicate whether an animator has been start()'d, whether or 1488b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase * not there is a nonzero startDelay. 1498b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase */ 150b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase private boolean mStarted = false; 151b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase 152b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase /** 153a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Flag that denotes whether the animation is set up and ready to go. Used to 154a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * set up animation that has not yet been started. 155a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 156a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase boolean mInitialized = false; 157a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 158a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // 159a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // Backing variables 160a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // 161a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 162a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // How long the animation should last in ms 163c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase private long mDuration = (long)(300 * sDurationScale); 164d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase private long mUnscaledDuration = 300; 165a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 166a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // The amount of time in ms to delay starting the animation after start() is called 167a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private long mStartDelay = 0; 168d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase private long mUnscaledStartDelay = 0; 169a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 170a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // The number of times the animation will repeat. The default is 0, which means the animation 171a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // will play only once 172a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private int mRepeatCount = 0; 173a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 174a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 175a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The type of repetition that will occur when repeatMode is nonzero. RESTART means the 176a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animation will start from the beginning on every new cycle. REVERSE means the animation 177a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * will reverse directions on each iteration. 178a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 179a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private int mRepeatMode = RESTART; 180a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 181a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 182a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The time interpolator to be used. The elapsed fraction of the animation will be passed 183a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * through this interpolator to calculate the interpolated fraction, which is then used to 184a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * calculate the animated values. 185a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 186e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase private TimeInterpolator mInterpolator = sDefaultInterpolator; 187a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 188a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 189a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The set of listeners to be sent events through the life of an animation. 190a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 191a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private ArrayList<AnimatorUpdateListener> mUpdateListeners = null; 192a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 193a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 194a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The property/value sets being animated. 195a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 196a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase PropertyValuesHolder[] mValues; 197a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 198a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 199a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * A hashmap of the PropertyValuesHolder objects. This map is used to lookup animated values 200a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * by property name during calls to getAnimatedValue(String). 201a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 202a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase HashMap<String, PropertyValuesHolder> mValuesMap; 203a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 204a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 205a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Public constants 206a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 207a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 208a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 209a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * When the animation reaches the end and <code>repeatCount</code> is INFINITE 210a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * or a positive value, the animation restarts from the beginning. 211a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 212a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public static final int RESTART = 1; 213a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 214a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * When the animation reaches the end and <code>repeatCount</code> is INFINITE 215a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * or a positive value, the animation reverses direction on every iteration. 216a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 217a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public static final int REVERSE = 2; 218a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 219a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This value used used with the {@link #setRepeatCount(int)} property to repeat 220a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * the animation indefinitely. 221a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 222a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public static final int INFINITE = -1; 223a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 224c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase 225c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase /** 226c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase * @hide 227c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase */ 228c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase public static void setDurationScale(float durationScale) { 229c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase sDurationScale = durationScale; 230c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase } 231c38fa1f63674971f9ac6ced1a449fb81026b62f7Chet Haase 232a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 233a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Creates a new ValueAnimator object. This default constructor is primarily for 2342794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * use internally; the factory methods which take parameters are more generally 235a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * useful. 236a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 237a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public ValueAnimator() { 238a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 239a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 240a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 2412794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Constructs and returns a ValueAnimator that animates between int values. A single 2422794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value implies that that value is the one being animated to. However, this is not typically 2432794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * useful in a ValueAnimator object because there is no way for the object to determine the 2442794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * starting value for the animation (unlike ObjectAnimator, which can derive that value 2452794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * from the target object and property being animated). Therefore, there should typically 2462794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * be two or more values. 247a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 2482794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param values A set of values that the animation will animate between over time. 2492794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @return A ValueAnimator object that is set up to animate between the given values. 25041f041d9986f8a5d45b6cb0b86e881c81a412168Chet Haase */ 2512794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public static ValueAnimator ofInt(int... values) { 2522794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase ValueAnimator anim = new ValueAnimator(); 2532794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase anim.setIntValues(values); 2542794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return anim; 2552794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 2562794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase 2572794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase /** 2582794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Constructs and returns a ValueAnimator that animates between float values. A single 2592794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value implies that that value is the one being animated to. However, this is not typically 2602794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * useful in a ValueAnimator object because there is no way for the object to determine the 2612794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * starting value for the animation (unlike ObjectAnimator, which can derive that value 2622794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * from the target object and property being animated). Therefore, there should typically 2632794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * be two or more values. 2642794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 2652794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param values A set of values that the animation will animate between over time. 2662794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @return A ValueAnimator object that is set up to animate between the given values. 2672794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase */ 2682794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public static ValueAnimator ofFloat(float... values) { 2692794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase ValueAnimator anim = new ValueAnimator(); 2702794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase anim.setFloatValues(values); 2712794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return anim; 2722794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 2732794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase 2742794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase /** 2752794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Constructs and returns a ValueAnimator that animates between the values 2762794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * specified in the PropertyValuesHolder objects. 2772794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 2782794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param values A set of PropertyValuesHolder objects whose values will be animated 2792794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * between over time. 2802794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @return A ValueAnimator object that is set up to animate between the given values. 2812794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase */ 2822794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public static ValueAnimator ofPropertyValuesHolder(PropertyValuesHolder... values) { 2832794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase ValueAnimator anim = new ValueAnimator(); 2842794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase anim.setValues(values); 2852794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return anim; 2862794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 2872794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase /** 2882794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Constructs and returns a ValueAnimator that animates between Object values. A single 2892794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value implies that that value is the one being animated to. However, this is not typically 2902794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * useful in a ValueAnimator object because there is no way for the object to determine the 2912794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * starting value for the animation (unlike ObjectAnimator, which can derive that value 2922794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * from the target object and property being animated). Therefore, there should typically 2932794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * be two or more values. 2942794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 2952794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * <p>Since ValueAnimator does not know how to animate between arbitrary Objects, this 2962794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * factory method also takes a TypeEvaluator object that the ValueAnimator will use 2972794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * to perform that interpolation. 2982794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 2992794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param evaluator A TypeEvaluator that will be called on each animation frame to 3002794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * provide the ncessry interpolation between the Object values to derive the animated 3012794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value. 3022794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param values A set of values that the animation will animate between over time. 3032794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @return A ValueAnimator object that is set up to animate between the given values. 3042794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase */ 3052794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public static ValueAnimator ofObject(TypeEvaluator evaluator, Object... values) { 3062794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase ValueAnimator anim = new ValueAnimator(); 3072794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase anim.setObjectValues(values); 3082794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase anim.setEvaluator(evaluator); 3092794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return anim; 3102794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 3112794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase 3122794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase /** 3132794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Sets int values that will be animated between. A single 3142794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value implies that that value is the one being animated to. However, this is not typically 3152794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * useful in a ValueAnimator object because there is no way for the object to determine the 3162794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * starting value for the animation (unlike ObjectAnimator, which can derive that value 3172794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * from the target object and property being animated). Therefore, there should typically 3182794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * be two or more values. 3192794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 3202794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * <p>If there are already multiple sets of values defined for this ValueAnimator via more 3212794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * than one PropertyValuesHolder object, this method will set the values for the first 3222794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * of those objects.</p> 3232794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 3242794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param values A set of values that the animation will animate between over time. 3252794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase */ 3262794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public void setIntValues(int... values) { 3272794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase if (values == null || values.length == 0) { 3282794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return; 3292794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 3302794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase if (mValues == null || mValues.length == 0) { 3312794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase setValues(new PropertyValuesHolder[]{PropertyValuesHolder.ofInt("", values)}); 3322794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } else { 3332794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase PropertyValuesHolder valuesHolder = mValues[0]; 3342794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase valuesHolder.setIntValues(values); 3352794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 3362794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase // New property/values/target should cause re-initialization prior to starting 3372794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase mInitialized = false; 3382794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 3392794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase 3402794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase /** 3412794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Sets float values that will be animated between. A single 3422794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value implies that that value is the one being animated to. However, this is not typically 3432794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * useful in a ValueAnimator object because there is no way for the object to determine the 3442794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * starting value for the animation (unlike ObjectAnimator, which can derive that value 3452794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * from the target object and property being animated). Therefore, there should typically 3462794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * be two or more values. 3472794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 3482794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * <p>If there are already multiple sets of values defined for this ValueAnimator via more 3492794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * than one PropertyValuesHolder object, this method will set the values for the first 3502794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * of those objects.</p> 3512794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 3522794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param values A set of values that the animation will animate between over time. 3532794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase */ 3542794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public void setFloatValues(float... values) { 3552794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase if (values == null || values.length == 0) { 3562794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return; 35741f041d9986f8a5d45b6cb0b86e881c81a412168Chet Haase } 3582794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase if (mValues == null || mValues.length == 0) { 3592794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase setValues(new PropertyValuesHolder[]{PropertyValuesHolder.ofFloat("", values)}); 3602794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } else { 3612794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase PropertyValuesHolder valuesHolder = mValues[0]; 3622794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase valuesHolder.setFloatValues(values); 3632794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 3642794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase // New property/values/target should cause re-initialization prior to starting 3652794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase mInitialized = false; 3662794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 3672794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase 3682794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase /** 3692794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Sets the values to animate between for this animation. A single 3702794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value implies that that value is the one being animated to. However, this is not typically 3712794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * useful in a ValueAnimator object because there is no way for the object to determine the 3722794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * starting value for the animation (unlike ObjectAnimator, which can derive that value 3732794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * from the target object and property being animated). Therefore, there should typically 3742794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * be two or more values. 3752794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 3762794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * <p>If there are already multiple sets of values defined for this ValueAnimator via more 3772794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * than one PropertyValuesHolder object, this method will set the values for the first 3782794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * of those objects.</p> 3792794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 3802794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * <p>There should be a TypeEvaluator set on the ValueAnimator that knows how to interpolate 3812794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * between these value objects. ValueAnimator only knows how to interpolate between the 3822794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * primitive types specified in the other setValues() methods.</p> 3832794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * 3842794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @param values The set of values to animate between. 3852794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase */ 3862794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public void setObjectValues(Object... values) { 3872794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase if (values == null || values.length == 0) { 3882794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return; 3892794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 3902794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase if (mValues == null || mValues.length == 0) { 3912794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase setValues(new PropertyValuesHolder[]{PropertyValuesHolder.ofObject("", 3922794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase (TypeEvaluator)null, values)}); 3932794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } else { 3942794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase PropertyValuesHolder valuesHolder = mValues[0]; 3952794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase valuesHolder.setObjectValues(values); 3962794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase } 3972794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase // New property/values/target should cause re-initialization prior to starting 3982794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase mInitialized = false; 399a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 400a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 401a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 402a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Sets the values, per property, being animated between. This function is called internally 403f76a50ce8fdc6aea22cabc77b2977a1a15a79630Ken Wakasa * by the constructors of ValueAnimator that take a list of values. But a ValueAnimator can 404a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * be constructed without values and this method can be called to set the values manually 405a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * instead. 406a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 407a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param values The set of values, per property, being animated between. 408a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 409a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void setValues(PropertyValuesHolder... values) { 410a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase int numValues = values.length; 411a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mValues = values; 412a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues); 413a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase for (int i = 0; i < numValues; ++i) { 414a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase PropertyValuesHolder valuesHolder = (PropertyValuesHolder) values[i]; 415a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mValuesMap.put(valuesHolder.getPropertyName(), valuesHolder); 416a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 4170e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase // New property/values/target should cause re-initialization prior to starting 4180e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase mInitialized = false; 419a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 420a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 421a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 422a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Returns the values that this ValueAnimator animates between. These values are stored in 423a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * PropertyValuesHolder objects, even if the ValueAnimator was created with a simple list 424a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * of value objects instead. 425a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 426a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return PropertyValuesHolder[] An array of PropertyValuesHolder objects which hold the 427a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * values, per property, that define the animation. 428a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 429a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public PropertyValuesHolder[] getValues() { 430a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return mValues; 431a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 432a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 433a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 434a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This function is called immediately before processing the first animation 435a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * frame of an animation. If there is a nonzero <code>startDelay</code>, the 436a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * function is called after that delay ends. 437a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * It takes care of the final initialization steps for the 438a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animation. 439a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 440a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>Overrides of this method should call the superclass method to ensure 441a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * that internal mechanisms for the animation are set up correctly.</p> 442a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 443a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase void initAnimation() { 444a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (!mInitialized) { 445a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase int numValues = mValues.length; 446a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase for (int i = 0; i < numValues; ++i) { 447a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mValues[i].init(); 448a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 449a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mInitialized = true; 450a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 451a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 452a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 453a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 454a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 4552794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Sets the length of the animation. The default duration is 300 milliseconds. 456a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 45727c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase * @param duration The length of the animation, in milliseconds. This value cannot 45827c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase * be negative. 4592794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * @return ValueAnimator The object called with setDuration(). This return 4602794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * value makes it easier to compose statements together that construct and then set the 4612794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * duration, as in <code>ValueAnimator.ofInt(0, 10).setDuration(500).start()</code>. 462a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 4632794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase public ValueAnimator setDuration(long duration) { 46427c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase if (duration < 0) { 46527c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase throw new IllegalArgumentException("Animators cannot have negative duration: " + 46627c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase duration); 46727c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase } 468d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase mUnscaledDuration = duration; 469d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase mDuration = (long)(duration * sDurationScale); 4702794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase return this; 471a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 472a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 473a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 4742794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase * Gets the length of the animation. The default duration is 300 milliseconds. 475a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 476a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return The length of the animation, in milliseconds. 477a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 478a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public long getDuration() { 479d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase return mUnscaledDuration; 480a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 481a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 482a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 483a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Sets the position of the animation to the specified point in time. This time should 484a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * be between 0 and the total duration of the animation, including any repetition. If 485a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * the animation has not yet been started, then it will not advance forward after it is 486a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * set to this time; it will simply set the time to this value and perform any appropriate 487a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * actions based on that time. If the animation is already running, then setCurrentPlayTime() 488a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * will set the current playing time to this value and continue playing from that point. 489a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 490a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param playTime The time, in milliseconds, to which the animation is advanced or rewound. 491a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 492a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void setCurrentPlayTime(long playTime) { 493a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase initAnimation(); 494a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase long currentTime = AnimationUtils.currentAnimationTimeMillis(); 495a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mPlayingState != RUNNING) { 496a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mSeekTime = playTime; 497a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mPlayingState = SEEKED; 498a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 499a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mStartTime = currentTime - playTime; 500a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase animationFrame(currentTime); 501a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 502a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 503a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 504a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Gets the current position of the animation in time, which is equal to the current 505a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * time minus the time that the animation started. An animation that is not yet started will 506a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * return a value of zero. 507a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 508a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return The current position in time of the animation. 509a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 510a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public long getCurrentPlayTime() { 511a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (!mInitialized || mPlayingState == STOPPED) { 512a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return 0; 513a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 514a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return AnimationUtils.currentAnimationTimeMillis() - mStartTime; 515a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 516a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 517a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 518a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This custom, static handler handles the timing pulse that is shared by 519a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * all active animations. This approach ensures that the setting of animation 520a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * values will happen on the UI thread and that all animations will share 521a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * the same times for calculating their values, which makes synchronizing 522a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animations possible. 523a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 524a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 5254a06c8008b2edd6677f9a411af79b0a4971b87feJeff Brown private static class AnimationHandler extends Handler implements Runnable { 5269c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown // The per-thread list of all active animations 5279c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown private final ArrayList<ValueAnimator> mAnimations = new ArrayList<ValueAnimator>(); 5289c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown 5299c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown // The per-thread set of animations to be started on the next animation frame 5309c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown private final ArrayList<ValueAnimator> mPendingAnimations = new ArrayList<ValueAnimator>(); 5319c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown 5329c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown /** 5339c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown * Internal per-thread collections used to avoid set collisions as animations start and end 5349c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown * while being processed. 5359c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown */ 5369c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown private final ArrayList<ValueAnimator> mDelayedAnims = new ArrayList<ValueAnimator>(); 5379c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown private final ArrayList<ValueAnimator> mEndingAnims = new ArrayList<ValueAnimator>(); 5389c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown private final ArrayList<ValueAnimator> mReadyAnims = new ArrayList<ValueAnimator>(); 5399c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown 54096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown private final Choreographer mChoreographer; 5414a06c8008b2edd6677f9a411af79b0a4971b87feJeff Brown private boolean mAnimationScheduled; 54296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown 54396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown private AnimationHandler() { 54496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown mChoreographer = Choreographer.getInstance(); 54596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 54696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown 547a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 54896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown * The START message is sent when an animation's start() method is called. 54996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown * It cannot start synchronously when start() is called 550a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * because the call may be on the wrong thread, and it would also not be 551a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * synchronized with other animations because it would not start on a common 552a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * timing pulse. So each animation sends a START message to the handler, which 553a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * causes the handler to place the animation on the active animations queue and 554a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * start processing frames for that animation. 555a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 556a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase @Override 557a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void handleMessage(Message msg) { 558a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase switch (msg.what) { 559a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase case ANIMATION_START: 560c6ffab32415a58bbb010dcd115684f9dbc249710Chet Haase // If there are already active animations, or if another ANIMATION_START 561c6ffab32415a58bbb010dcd115684f9dbc249710Chet Haase // message was processed during this frame, then the pending list may already 562c6ffab32415a58bbb010dcd115684f9dbc249710Chet Haase // have been cleared. If that's the case, we've already processed the 563c6ffab32415a58bbb010dcd115684f9dbc249710Chet Haase // active animations for this frame - don't do it again. 564c6ffab32415a58bbb010dcd115684f9dbc249710Chet Haase if (mPendingAnimations.size() > 0) { 565c6ffab32415a58bbb010dcd115684f9dbc249710Chet Haase doAnimationFrame(); 566c6ffab32415a58bbb010dcd115684f9dbc249710Chet Haase } 56796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown break; 56896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 56996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 570a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 571c6ffab32415a58bbb010dcd115684f9dbc249710Chet Haase private void doAnimationFrame() { 572c6ffab32415a58bbb010dcd115684f9dbc249710Chet Haase // currentTime holds the common time for all animations processed 573c6ffab32415a58bbb010dcd115684f9dbc249710Chet Haase // during this frame 574c6ffab32415a58bbb010dcd115684f9dbc249710Chet Haase long currentTime = AnimationUtils.currentAnimationTimeMillis(); 575c6ffab32415a58bbb010dcd115684f9dbc249710Chet Haase 57696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // mPendingAnimations holds any animations that have requested to be started 57796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // We're going to clear mPendingAnimations, but starting animation may 57896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // cause more to be added to the pending list (for example, if one animation 57996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // starting triggers another starting). So we loop until mPendingAnimations 58096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // is empty. 58196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown while (mPendingAnimations.size() > 0) { 58296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown ArrayList<ValueAnimator> pendingCopy = 58396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown (ArrayList<ValueAnimator>) mPendingAnimations.clone(); 58496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown mPendingAnimations.clear(); 58596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown int count = pendingCopy.size(); 58696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown for (int i = 0; i < count; ++i) { 58796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown ValueAnimator anim = pendingCopy.get(i); 58896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // If the animation has a startDelay, place it on the delayed list 58996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown if (anim.mStartDelay == 0) { 59096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown anim.startAnimation(this); 59196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } else { 59296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown mDelayedAnims.add(anim); 593a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 59496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 59596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 596c6ffab32415a58bbb010dcd115684f9dbc249710Chet Haase // Next, process animations currently sitting on the delayed queue, adding 59796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // them to the active animations if they are ready 59896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown int numDelayedAnims = mDelayedAnims.size(); 59996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown for (int i = 0; i < numDelayedAnims; ++i) { 60096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown ValueAnimator anim = mDelayedAnims.get(i); 60196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown if (anim.delayedAnimationFrame(currentTime)) { 60296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown mReadyAnims.add(anim); 60396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 60496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 60596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown int numReadyAnims = mReadyAnims.size(); 60696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown if (numReadyAnims > 0) { 60796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown for (int i = 0; i < numReadyAnims; ++i) { 60896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown ValueAnimator anim = mReadyAnims.get(i); 60996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown anim.startAnimation(this); 61096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown anim.mRunning = true; 61196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown mDelayedAnims.remove(anim); 61296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 61396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown mReadyAnims.clear(); 61496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 61596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown 61696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // Now process all active animations. The return value from animationFrame() 61796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // tells the handler whether it should now be ended 61896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown int numAnims = mAnimations.size(); 61996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown int i = 0; 62096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown while (i < numAnims) { 62196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown ValueAnimator anim = mAnimations.get(i); 62296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown if (anim.animationFrame(currentTime)) { 62396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown mEndingAnims.add(anim); 62496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 62596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown if (mAnimations.size() == numAnims) { 62696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown ++i; 62796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } else { 62896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // An animation might be canceled or ended by client code 62996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // during the animation frame. Check to see if this happened by 63096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // seeing whether the current index is the same as it was before 63196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // calling animationFrame(). Another approach would be to copy 63296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // animations to a temporary list and process that list instead, 63396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // but that entails garbage and processing overhead that would 63496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // be nice to avoid. 63596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown --numAnims; 63696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown mEndingAnims.remove(anim); 63796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 63896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 63996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown if (mEndingAnims.size() > 0) { 64096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown for (i = 0; i < mEndingAnims.size(); ++i) { 64196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown mEndingAnims.get(i).endAnimation(this); 64296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 64396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown mEndingAnims.clear(); 64496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 64596e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown 64696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // If there are still active or delayed animations, schedule a future call to 64796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown // onAnimate to process the next frame of the animations. 6484a06c8008b2edd6677f9a411af79b0a4971b87feJeff Brown if (!mAnimationScheduled 6494a06c8008b2edd6677f9a411af79b0a4971b87feJeff Brown && (!mAnimations.isEmpty() || !mDelayedAnims.isEmpty())) { 6507ae9d5faad5816f7e567ec1ec77e78d746cf7e5cJeff Brown mChoreographer.postAnimationCallback(this, null); 6514a06c8008b2edd6677f9a411af79b0a4971b87feJeff Brown mAnimationScheduled = true; 652a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 653a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 65496e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown 6554a06c8008b2edd6677f9a411af79b0a4971b87feJeff Brown // Called by the Choreographer. 65696e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown @Override 6574a06c8008b2edd6677f9a411af79b0a4971b87feJeff Brown public void run() { 6584a06c8008b2edd6677f9a411af79b0a4971b87feJeff Brown mAnimationScheduled = false; 65996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown doAnimationFrame(); 66096e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown } 661a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 662a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 663a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 664a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The amount of time, in milliseconds, to delay starting the animation after 665a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link #start()} is called. 666a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 667a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return the number of milliseconds to delay running the animation 668a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 669a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public long getStartDelay() { 670d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase return mUnscaledStartDelay; 671a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 672a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 673a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 674a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The amount of time, in milliseconds, to delay starting the animation after 675a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link #start()} is called. 676a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 677a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param startDelay The amount of the delay, in milliseconds 678a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 679a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void setStartDelay(long startDelay) { 680d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase this.mStartDelay = (long)(startDelay * sDurationScale); 681d21a9fe2d9a6cfe966fc7df3a8c37c172d7ac302Chet Haase mUnscaledStartDelay = startDelay; 682a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 683a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 684a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 685a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The amount of time, in milliseconds, between each frame of the animation. This is a 686a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * requested time that the animation will attempt to honor, but the actual delay between 687a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * frames may be different, depending on system load and capabilities. This is a static 688a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * function because the same delay will be applied to all animations, since they are all 689a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * run off of a single timing loop. 690a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 69196e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown * The frame delay may be ignored when the animation system uses an external timing 69296e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown * source, such as the display refresh rate (vsync), to govern animations. 69396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown * 694a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return the requested time between frames, in milliseconds 695a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 696a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public static long getFrameDelay() { 69796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown return Choreographer.getFrameDelay(); 698a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 699a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 700a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 701a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The amount of time, in milliseconds, between each frame of the animation. This is a 702a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * requested time that the animation will attempt to honor, but the actual delay between 703a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * frames may be different, depending on system load and capabilities. This is a static 704a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * function because the same delay will be applied to all animations, since they are all 705a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * run off of a single timing loop. 706a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 70796e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown * The frame delay may be ignored when the animation system uses an external timing 70896e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown * source, such as the display refresh rate (vsync), to govern animations. 70996e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown * 710a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param frameDelay the requested time between frames, in milliseconds 711a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 712a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public static void setFrameDelay(long frameDelay) { 71396e942dabeeaaa9ab6df3a870668c6fe53d930daJeff Brown Choreographer.setFrameDelay(frameDelay); 714a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 715a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 716a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 717a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The most recent value calculated by this <code>ValueAnimator</code> when there is just one 718a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * property being animated. This value is only sensible while the animation is running. The main 719a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * purpose for this read-only property is to retrieve the value from the <code>ValueAnimator</code> 720a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * during a call to {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which 721a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * is called during each animation frame, immediately after the value is calculated. 722a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 723a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return animatedValue The value most recently calculated by this <code>ValueAnimator</code> for 724a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * the single property being animated. If there are several properties being animated 725a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * (specified by several PropertyValuesHolder objects in the constructor), this function 726a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * returns the animated value for the first of those objects. 727a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 728a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public Object getAnimatedValue() { 729a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mValues != null && mValues.length > 0) { 730a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return mValues[0].getAnimatedValue(); 731a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 732a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // Shouldn't get here; should always have values unless ValueAnimator was set up wrong 733a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return null; 734a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 735a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 736a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 737a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The most recent value calculated by this <code>ValueAnimator</code> for <code>propertyName</code>. 738a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The main purpose for this read-only property is to retrieve the value from the 739a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <code>ValueAnimator</code> during a call to 740a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link AnimatorUpdateListener#onAnimationUpdate(ValueAnimator)}, which 741a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * is called during each animation frame, immediately after the value is calculated. 742a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 743a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return animatedValue The value most recently calculated for the named property 744a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * by this <code>ValueAnimator</code>. 745a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 746a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public Object getAnimatedValue(String propertyName) { 747a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase PropertyValuesHolder valuesHolder = mValuesMap.get(propertyName); 748a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (valuesHolder != null) { 749a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return valuesHolder.getAnimatedValue(); 750a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } else { 751a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // At least avoid crashing if called with bogus propertyName 752a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return null; 753a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 754a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 755a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 756a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 757a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Sets how many times the animation should be repeated. If the repeat 758a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * count is 0, the animation is never repeated. If the repeat count is 759a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * greater than 0 or {@link #INFINITE}, the repeat mode will be taken 760a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * into account. The repeat count is 0 by default. 761a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 762a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param value the number of times the animation should be repeated 763a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 764a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void setRepeatCount(int value) { 765a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mRepeatCount = value; 766a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 767a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 768a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Defines how many times the animation should repeat. The default value 769a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * is 0. 770a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 771a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return the number of times the animation should repeat, or {@link #INFINITE} 772a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 773a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public int getRepeatCount() { 774a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return mRepeatCount; 775a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 776a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 777a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 778a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Defines what this animation should do when it reaches the end. This 779a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * setting is applied only when the repeat count is either greater than 780a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 0 or {@link #INFINITE}. Defaults to {@link #RESTART}. 781a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 782a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param value {@link #RESTART} or {@link #REVERSE} 783a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 784a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void setRepeatMode(int value) { 785a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mRepeatMode = value; 786a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 787a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 788a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 789a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Defines what this animation should do when it reaches the end. 790a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 791a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return either one of {@link #REVERSE} or {@link #RESTART} 792a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 793a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public int getRepeatMode() { 794a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return mRepeatMode; 795a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 796a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 797a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 798a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Adds a listener to the set of listeners that are sent update events through the life of 799a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * an animation. This method is called on all listeners for every frame of the animation, 800a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * after the values for the animation have been calculated. 801a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 802a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param listener the listener to be added to the current set of listeners for this animation. 803a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 804a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void addUpdateListener(AnimatorUpdateListener listener) { 805a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mUpdateListeners == null) { 806a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mUpdateListeners = new ArrayList<AnimatorUpdateListener>(); 807a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 808a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mUpdateListeners.add(listener); 809a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 810a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 811a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 8123060421045d4d9e411797f91bb509824b03e33fbJim Miller * Removes all listeners from the set listening to frame updates for this animation. 8133060421045d4d9e411797f91bb509824b03e33fbJim Miller */ 8143060421045d4d9e411797f91bb509824b03e33fbJim Miller public void removeAllUpdateListeners() { 8153060421045d4d9e411797f91bb509824b03e33fbJim Miller if (mUpdateListeners == null) { 8163060421045d4d9e411797f91bb509824b03e33fbJim Miller return; 8173060421045d4d9e411797f91bb509824b03e33fbJim Miller } 8183060421045d4d9e411797f91bb509824b03e33fbJim Miller mUpdateListeners.clear(); 8193060421045d4d9e411797f91bb509824b03e33fbJim Miller mUpdateListeners = null; 8203060421045d4d9e411797f91bb509824b03e33fbJim Miller } 8213060421045d4d9e411797f91bb509824b03e33fbJim Miller 8223060421045d4d9e411797f91bb509824b03e33fbJim Miller /** 823a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Removes a listener from the set listening to frame updates for this animation. 824a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 825a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param listener the listener to be removed from the current set of update listeners 826a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * for this animation. 827a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 828a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void removeUpdateListener(AnimatorUpdateListener listener) { 829a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mUpdateListeners == null) { 830a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return; 831a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 832a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mUpdateListeners.remove(listener); 833a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mUpdateListeners.size() == 0) { 834a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mUpdateListeners = null; 835a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 836a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 837a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 838a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 839a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 840a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The time interpolator used in calculating the elapsed fraction of this animation. The 841a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * interpolator determines whether the animation runs with linear or non-linear motion, 842a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * such as acceleration and deceleration. The default value is 843a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * {@link android.view.animation.AccelerateDecelerateInterpolator} 844a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 84527c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase * @param value the interpolator to be used by this animation. A value of <code>null</code> 84627c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase * will result in linear interpolation. 847a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 848a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase @Override 849e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase public void setInterpolator(TimeInterpolator value) { 850a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (value != null) { 851a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mInterpolator = value; 85227c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase } else { 85327c1d4debb3848f5accd5673fffeeacad3e61648Chet Haase mInterpolator = new LinearInterpolator(); 854a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 855a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 856a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 857a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 858a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Returns the timing interpolator that this ValueAnimator uses. 859a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 860a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return The timing interpolator for this ValueAnimator. 861a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 862e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase public TimeInterpolator getInterpolator() { 863a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return mInterpolator; 864a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 865a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 866a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 867a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * The type evaluator to be used when calculating the animated values of this animation. 868b2ab04ffb6894f399d5c9ceb15f64eb17b654426Chet Haase * The system will automatically assign a float or int evaluator based on the type 869a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * of <code>startValue</code> and <code>endValue</code> in the constructor. But if these values 870a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * are not one of these primitive types, or if different evaluation is desired (such as is 871a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * necessary with int values that represent colors), a custom evaluator needs to be assigned. 87253ee3316bcb3590ff156b3fd7108903c0817c35dChet Haase * For example, when running an animation on color values, the {@link ArgbEvaluator} 873a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * should be used to get correct RGB color interpolation. 874a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 875a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>If this ValueAnimator has only one set of values being animated between, this evaluator 876a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * will be used for that set. If there are several sets of values being animated, which is 877a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * the case if PropertyValuesHOlder objects were set on the ValueAnimator, then the evaluator 878a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * is assigned just to the first PropertyValuesHolder object.</p> 879a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 880a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param value the evaluator to be used this animation 881a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 882a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void setEvaluator(TypeEvaluator value) { 883a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (value != null && mValues != null && mValues.length > 0) { 884a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mValues[0].setEvaluator(value); 885a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 886a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 887a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 888a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 889a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Start the animation playing. This version of start() takes a boolean flag that indicates 890a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * whether the animation should play in reverse. The flag is usually false, but may be set 8912970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase * to true if called from the reverse() method. 8922970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase * 8932970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase * <p>The animation started by calling this method will be run on the thread that called 8942970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase * this method. This thread should have a Looper on it (a runtime exception will be thrown if 8952970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase * this is not the case). Also, if the animation will animate 8962970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase * properties of objects in the view hierarchy, then the calling thread should be the UI 8972970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase * thread for that view hierarchy.</p> 898a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 899a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param playBackwards Whether the ValueAnimator should start playing in reverse. 900a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 901a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private void start(boolean playBackwards) { 9022970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase if (Looper.myLooper() == null) { 9032970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase throw new AndroidRuntimeException("Animators may only be run on Looper threads"); 9043060421045d4d9e411797f91bb509824b03e33fbJim Miller } 9052970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase mPlayingBackwards = playBackwards; 906add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase mCurrentIteration = 0; 907add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase mPlayingState = STOPPED; 9088b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase mStarted = true; 909add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase mStartedDelay = false; 9109c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown AnimationHandler animationHandler = getOrCreateAnimationHandler(); 9119c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown animationHandler.mPendingAnimations.add(this); 9122970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase if (mStartDelay == 0) { 913add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase // This sets the initial value of the animation, prior to actually starting it running 914add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase setCurrentPlayTime(getCurrentPlayTime()); 915154f14508a11627d5a995b6fe2a14a83d794a6feChet Haase mPlayingState = STOPPED; 9168b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase mRunning = true; 917add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase 918b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase if (mListeners != null) { 919b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase ArrayList<AnimatorListener> tmpListeners = 920b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase (ArrayList<AnimatorListener>) mListeners.clone(); 9217c608f25d494c8a0a671e7373efbb47ca635367eChet Haase int numListeners = tmpListeners.size(); 9227c608f25d494c8a0a671e7373efbb47ca635367eChet Haase for (int i = 0; i < numListeners; ++i) { 9237c608f25d494c8a0a671e7373efbb47ca635367eChet Haase tmpListeners.get(i).onAnimationStart(this); 924b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase } 925b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase } 926a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 927e3bc4e6f102fbef760fe0a59dd807363571b0867Chet Haase animationHandler.sendEmptyMessage(ANIMATION_START); 928a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 929a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 930a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase @Override 931a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void start() { 932a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase start(false); 933a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 934a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 935a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase @Override 936a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void cancel() { 9372970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase // Only cancel if the animation is actually running or has been started and is about 9382970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase // to run 9399c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown AnimationHandler handler = getOrCreateAnimationHandler(); 9409c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown if (mPlayingState != STOPPED 9419c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown || handler.mPendingAnimations.contains(this) 9429c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown || handler.mDelayedAnims.contains(this)) { 943b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase // Only notify listeners if the animator has actually started 9448b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase if (mRunning && mListeners != null) { 9457dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase ArrayList<AnimatorListener> tmpListeners = 9467dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase (ArrayList<AnimatorListener>) mListeners.clone(); 9477dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase for (AnimatorListener listener : tmpListeners) { 9487dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase listener.onAnimationCancel(this); 9497dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase } 9507dfacdb1c820f955cb3cd6032ff5fbc2dd7d9df5Chet Haase } 9519c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown endAnimation(handler); 9522970c499388b4dcd1232cd622a9b80b395eeb2b4Chet Haase } 953a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 954a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 955a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase @Override 956a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void end() { 9579c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown AnimationHandler handler = getOrCreateAnimationHandler(); 9589c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown if (!handler.mAnimations.contains(this) && !handler.mPendingAnimations.contains(this)) { 959a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // Special case if the animation has not yet started; get it ready for ending 960a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mStartedDelay = false; 9619c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown startAnimation(handler); 962add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase } else if (!mInitialized) { 963add6577a0196258e5a48c5deefcdb12e05c935b3Chet Haase initAnimation(); 964e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase } 965e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase // The final value set on the target varies, depending on whether the animation 966e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase // was supposed to repeat an odd number of times 967e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase if (mRepeatCount > 0 && (mRepeatCount & 0x01) == 1) { 968e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase animateValue(0f); 969e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase } else { 970e2ab7ccd385cdb6517955c719e1d2b49771bedb6Chet Haase animateValue(1f); 971a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 9729c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown endAnimation(handler); 973a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 974a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 975a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase @Override 976a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public boolean isRunning() { 9778b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase return (mPlayingState == RUNNING || mRunning); 9788b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase } 9798b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase 9808b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase @Override 9818b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase public boolean isStarted() { 9828b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase return mStarted; 983a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 984a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 985a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 986a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Plays the ValueAnimator in reverse. If the animation is already running, 987a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * it will stop itself and play backwards from the point reached when reverse was called. 988a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * If the animation is not currently running, then it will start from the end and 989a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * play backwards. This behavior is only set for the current animation; future playing 990a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * of the animation will use the default behavior of playing forward. 991a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 992a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public void reverse() { 993a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mPlayingBackwards = !mPlayingBackwards; 994a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mPlayingState == RUNNING) { 995a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase long currentTime = AnimationUtils.currentAnimationTimeMillis(); 996a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase long currentPlayTime = currentTime - mStartTime; 997a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase long timeLeft = mDuration - currentPlayTime; 998a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mStartTime = currentTime - timeLeft; 999a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } else { 1000a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase start(true); 1001a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1002a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1003a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1004a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 1005a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Called internally to end an animation by removing it from the animations list. Must be 1006a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * called on the UI thread. 1007a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 10089c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown private void endAnimation(AnimationHandler handler) { 10099c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown handler.mAnimations.remove(this); 10109c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown handler.mPendingAnimations.remove(this); 10119c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown handler.mDelayedAnims.remove(this); 1012a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mPlayingState = STOPPED; 10138b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase if (mRunning && mListeners != null) { 1014a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase ArrayList<AnimatorListener> tmpListeners = 1015a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase (ArrayList<AnimatorListener>) mListeners.clone(); 10167c608f25d494c8a0a671e7373efbb47ca635367eChet Haase int numListeners = tmpListeners.size(); 10177c608f25d494c8a0a671e7373efbb47ca635367eChet Haase for (int i = 0; i < numListeners; ++i) { 10187c608f25d494c8a0a671e7373efbb47ca635367eChet Haase tmpListeners.get(i).onAnimationEnd(this); 1019a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1020a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 10218b699792b677bd4dd8442b32641ac09d48fdd79cChet Haase mRunning = false; 1022b8f574a165bf6ec5b316734b367ac274ded4809bChet Haase mStarted = false; 1023a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1024a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1025a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 1026a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Called internally to start an animation by adding it to the active animations list. Must be 1027a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * called on the UI thread. 1028a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 10299c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown private void startAnimation(AnimationHandler handler) { 1030a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase initAnimation(); 10319c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown handler.mAnimations.add(this); 1032b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase if (mStartDelay > 0 && mListeners != null) { 1033b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase // Listeners were already notified in start() if startDelay is 0; this is 1034b20db3ec34e846010f389880b2cfab4d7bf79820Chet Haase // just for delayed animations 1035a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase ArrayList<AnimatorListener> tmpListeners = 1036a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase (ArrayList<AnimatorListener>) mListeners.clone(); 10377c608f25d494c8a0a671e7373efbb47ca635367eChet Haase int numListeners = tmpListeners.size(); 10387c608f25d494c8a0a671e7373efbb47ca635367eChet Haase for (int i = 0; i < numListeners; ++i) { 10397c608f25d494c8a0a671e7373efbb47ca635367eChet Haase tmpListeners.get(i).onAnimationStart(this); 1040a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1041a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1042a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1043a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1044a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 1045a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Internal function called to process an animation frame on an animation that is currently 1046a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * sleeping through its <code>startDelay</code> phase. The return value indicates whether it 1047a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * should be woken up and put on the active animations queue. 1048a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 1049a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param currentTime The current animation time, used to calculate whether the animation 1050a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * has exceeded its <code>startDelay</code> and should be started. 1051a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return True if the animation's <code>startDelay</code> has been exceeded and the animation 1052a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * should be added to the set of active animations. 1053a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 1054a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase private boolean delayedAnimationFrame(long currentTime) { 1055a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (!mStartedDelay) { 1056a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mStartedDelay = true; 1057a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mDelayStartTime = currentTime; 1058a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } else { 1059a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase long deltaTime = currentTime - mDelayStartTime; 1060a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (deltaTime > mStartDelay) { 1061a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // startDelay ended - start the anim and record the 1062a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // mStartTime appropriately 1063a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mStartTime = currentTime - (deltaTime - mStartDelay); 1064a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mPlayingState = RUNNING; 1065a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return true; 1066a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1067a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1068a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return false; 1069a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1070a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1071a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 1072a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This internal function processes a single animation frame for a given animation. The 1073a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * currentTime parameter is the timing pulse sent by the handler, used to calculate the 1074a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * elapsed duration, and therefore 1075a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * the elapsed fraction, of the animation. The return value indicates whether the animation 1076a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * should be ended (which happens when the elapsed time of the animation exceeds the 1077a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animation's duration, including the repeatCount). 1078a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 1079a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param currentTime The current time, as tracked by the static timing handler 1080a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @return true if the animation's duration, including any repetitions due to 1081a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <code>repeatCount</code> has been exceeded and the animation should be ended. 1082a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 1083051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase boolean animationFrame(long currentTime) { 1084a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase boolean done = false; 1085a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1086a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mPlayingState == STOPPED) { 1087a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mPlayingState = RUNNING; 1088a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mSeekTime < 0) { 1089a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mStartTime = currentTime; 1090a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } else { 1091a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mStartTime = currentTime - mSeekTime; 1092a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // Now that we're playing, reset the seek time 1093a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mSeekTime = -1; 1094a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1095a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1096a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase switch (mPlayingState) { 1097a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase case RUNNING: 1098a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase case SEEKED: 109970d4ba15b1f0c1133c5aabc86de828b41e482fffChet Haase float fraction = mDuration > 0 ? (float)(currentTime - mStartTime) / mDuration : 1f; 1100a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (fraction >= 1f) { 1101a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mCurrentIteration < mRepeatCount || mRepeatCount == INFINITE) { 1102a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase // Time to repeat 1103a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mListeners != null) { 11047c608f25d494c8a0a671e7373efbb47ca635367eChet Haase int numListeners = mListeners.size(); 11057c608f25d494c8a0a671e7373efbb47ca635367eChet Haase for (int i = 0; i < numListeners; ++i) { 11067c608f25d494c8a0a671e7373efbb47ca635367eChet Haase mListeners.get(i).onAnimationRepeat(this); 1107a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1108a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1109a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mRepeatMode == REVERSE) { 1110a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mPlayingBackwards = mPlayingBackwards ? false : true; 1111a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1112730666858692ea396f5ad779654b5d86ff90b6caChet Haase mCurrentIteration += (int)fraction; 1113730666858692ea396f5ad779654b5d86ff90b6caChet Haase fraction = fraction % 1f; 1114a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mStartTime += mDuration; 1115a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } else { 1116a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase done = true; 1117a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase fraction = Math.min(fraction, 1.0f); 1118a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1119a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1120a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mPlayingBackwards) { 1121a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase fraction = 1f - fraction; 1122a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1123a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase animateValue(fraction); 1124a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase break; 1125a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1126a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1127a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return done; 1128a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1129a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1130a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 1131a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase * Returns the current animation fraction, which is the elapsed/interpolated fraction used in 1132a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase * the most recent frame update on the animation. 1133a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase * 1134a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase * @return Elapsed/interpolated fraction of the animation. 1135a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase */ 1136a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase public float getAnimatedFraction() { 1137a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase return mCurrentFraction; 1138a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase } 1139a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase 1140a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase /** 1141a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This method is called with the elapsed fraction of the animation during every 1142a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animation frame. This function turns the elapsed fraction into an interpolated fraction 1143a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * and then into an animated value (from the evaluator. The function is called mostly during 1144a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * animation updates, but it is also called when the <code>end()</code> 1145a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * function is called, to set the final value on the property. 1146a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 1147a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>Overrides of this method must call the superclass to perform the calculation 1148a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * of the animated value.</p> 1149a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 1150a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param fraction The elapsed fraction of the animation. 1151a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 1152a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase void animateValue(float fraction) { 1153a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase fraction = mInterpolator.getInterpolation(fraction); 1154a00f3865f55c5c9cb74510ee2b239d101230133cChet Haase mCurrentFraction = fraction; 1155a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase int numValues = mValues.length; 1156a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase for (int i = 0; i < numValues; ++i) { 1157a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mValues[i].calculateValue(fraction); 1158a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1159a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mUpdateListeners != null) { 1160a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase int numListeners = mUpdateListeners.size(); 1161a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase for (int i = 0; i < numListeners; ++i) { 1162a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase mUpdateListeners.get(i).onAnimationUpdate(this); 1163a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1164a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1165a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1166a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1167a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase @Override 1168a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public ValueAnimator clone() { 1169a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase final ValueAnimator anim = (ValueAnimator) super.clone(); 1170a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (mUpdateListeners != null) { 1171a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase ArrayList<AnimatorUpdateListener> oldListeners = mUpdateListeners; 1172a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase anim.mUpdateListeners = new ArrayList<AnimatorUpdateListener>(); 1173a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase int numListeners = oldListeners.size(); 1174a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase for (int i = 0; i < numListeners; ++i) { 1175a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase anim.mUpdateListeners.add(oldListeners.get(i)); 1176a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1177a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1178a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase anim.mSeekTime = -1; 1179a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase anim.mPlayingBackwards = false; 1180a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase anim.mCurrentIteration = 0; 1181a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase anim.mInitialized = false; 1182a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase anim.mPlayingState = STOPPED; 1183a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase anim.mStartedDelay = false; 1184a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase PropertyValuesHolder[] oldValues = mValues; 1185a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase if (oldValues != null) { 1186a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase int numValues = oldValues.length; 1187a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase anim.mValues = new PropertyValuesHolder[numValues]; 1188a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase anim.mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues); 1189a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase for (int i = 0; i < numValues; ++i) { 1190d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase PropertyValuesHolder newValuesHolder = oldValues[i].clone(); 1191d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase anim.mValues[i] = newValuesHolder; 1192d4dd7025a1b356476e119de19a2e2cd5cf50d43cChet Haase anim.mValuesMap.put(newValuesHolder.getPropertyName(), newValuesHolder); 1193a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1194a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1195a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase return anim; 1196a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1197a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1198a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 1199a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * Implementors of this interface can add themselves as update listeners 1200a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * to an <code>ValueAnimator</code> instance to receive callbacks on every animation 1201a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * frame, after the current frame's values have been calculated for that 1202a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <code>ValueAnimator</code>. 1203a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 1204a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase public static interface AnimatorUpdateListener { 1205a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase /** 1206a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * <p>Notifies the occurrence of another frame of the animation.</p> 1207a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * 1208a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * @param animation The animation which was repeated. 1209a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase */ 1210a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase void onAnimationUpdate(ValueAnimator animation); 1211a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase 1212a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase } 1213599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick 1214599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick /** 1215599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick * Return the number of animations currently running. 1216599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick * 12179c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown * Used by StrictMode internally to annotate violations. 12189c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown * May be called on arbitrary threads! 1219599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick * 1220599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick * @hide 1221599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick */ 1222599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick public static int getCurrentAnimationsCount() { 12239c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown AnimationHandler handler = sAnimationHandler.get(); 12249c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown return handler != null ? handler.mAnimations.size() : 0; 1225599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick } 12268901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy 12278901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy /** 12288901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy * Clear all animations on this thread, without canceling or ending them. 12298901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy * This should be used with caution. 12308901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy * 12318901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy * @hide 12328901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy */ 12338901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy public static void clearAllAnimations() { 12349c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown AnimationHandler handler = sAnimationHandler.get(); 12359c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown if (handler != null) { 12369c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown handler.mAnimations.clear(); 12379c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown handler.mPendingAnimations.clear(); 12389c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown handler.mDelayedAnims.clear(); 12399c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown } 12409c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown } 12419c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown 12429c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown private AnimationHandler getOrCreateAnimationHandler() { 12439c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown AnimationHandler handler = sAnimationHandler.get(); 12449c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown if (handler == null) { 12459c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown handler = new AnimationHandler(); 12469c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown sAnimationHandler.set(handler); 12479c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown } 12489c38dbeb1d183ecd48bbf5d18a39f5e0508a1223Jeff Brown return handler; 12498901ffab294934fc4899143f31bd58f3d58df225Patrick Dubroy } 1250e9140a72b1059574046a624b471b2c3a35806496Chet Haase 1251e9140a72b1059574046a624b471b2c3a35806496Chet Haase @Override 1252e9140a72b1059574046a624b471b2c3a35806496Chet Haase public String toString() { 1253e9140a72b1059574046a624b471b2c3a35806496Chet Haase String returnVal = "ValueAnimator@" + Integer.toHexString(hashCode()); 1254e9140a72b1059574046a624b471b2c3a35806496Chet Haase if (mValues != null) { 1255e9140a72b1059574046a624b471b2c3a35806496Chet Haase for (int i = 0; i < mValues.length; ++i) { 1256e9140a72b1059574046a624b471b2c3a35806496Chet Haase returnVal += "\n " + mValues[i].toString(); 1257e9140a72b1059574046a624b471b2c3a35806496Chet Haase } 1258e9140a72b1059574046a624b471b2c3a35806496Chet Haase } 1259e9140a72b1059574046a624b471b2c3a35806496Chet Haase return returnVal; 1260e9140a72b1059574046a624b471b2c3a35806496Chet Haase } 1261599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick} 1262