Animator.java revision 010dbaa1236cf2dcdc62c29049468e90188acaae
117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase/* 217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Copyright (C) 2010 The Android Open Source Project 317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Licensed under the Apache License, Version 2.0 (the "License"); 517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * you may not use this file except in compliance with the License. 617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * You may obtain a copy of the License at 717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * http://www.apache.org/licenses/LICENSE-2.0 917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 1017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Unless required by applicable law or agreed to in writing, software 1117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * distributed under the License is distributed on an "AS IS" BASIS, 1217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * See the License for the specific language governing permissions and 1417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * limitations under the License. 1517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 1617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 1717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haasepackage android.animation; 1817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 1917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport android.os.Handler; 2017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport android.os.Message; 2117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport android.view.animation.AccelerateDecelerateInterpolator; 2217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport android.view.animation.AnimationUtils; 2317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport android.view.animation.Interpolator; 2417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 2517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport java.util.ArrayList; 2617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 2717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase/** 2817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * This class provides a simple timing engine for running animations 2917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * which calculate animated values and set them on target objects. 3017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 31010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * <p>There is a single timing pulse that all animations use. It runs in a 32010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * custom handler to ensure that property changes happen on the UI thread.</p> 33010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * 34010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * <p>By default, Animator uses non-linear time interpolation, via the 35010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * {@link AccelerateDecelerateInterpolator} class, which accelerates into and decelerates 36010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * out of an animation. This behavior can be changed by calling 37010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * {@link Animator#setInterpolator(Interpolator)}.</p> 3817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 3917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haasepublic class Animator extends Animatable { 4017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 4117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 4217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Internal constants 4317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 4417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 4517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /* 4617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The default amount of time in ms between animation frames 4717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 4817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final long DEFAULT_FRAME_DELAY = 30; 4917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 5017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 5117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Messages sent to timing handler: START is sent when an animation first begins, FRAME is sent 5217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * by the handler to itself to process the next animation frame 5317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 5417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final int ANIMATION_START = 0; 5517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final int ANIMATION_FRAME = 1; 5617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 5717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 5817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Values used with internal variable mPlayingState to indicate the current state of an 5917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * animation. 6017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 6117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final int STOPPED = 0; // Not yet playing 6217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final int RUNNING = 1; // Playing normally 6317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final int CANCELED = 2; // cancel() called - need to end it 6417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final int ENDED = 3; // end() called - need to end it 6517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 6617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 6717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Internal variables 6817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 6917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 7017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 7117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The first time that the animation's animateFrame() method is called. This time is used to 7217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // determine elapsed time (and therefore the elapsed fraction) in subsequent calls 7317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // to animateFrame() 7417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private long mStartTime; 7517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 7617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The static sAnimationHandler processes the internal timing loop on which all animations 7717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // are based 7817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static AnimationHandler sAnimationHandler; 7917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 8017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The static list of all active animations 8117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final ArrayList<Animator> sAnimations = new ArrayList<Animator>(); 8217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 8317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The set of animations to be started on the next animation frame 8417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final ArrayList<Animator> sPendingAnimations = new ArrayList<Animator>(); 8517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 8617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The time interpolator to be used if none is set on the animation 8717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final Interpolator sDefaultInterpolator = new AccelerateDecelerateInterpolator(); 8817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 8917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // type evaluators for the three primitive types handled by this implementation 9017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final TypeEvaluator sIntEvaluator = new IntEvaluator(); 9117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final TypeEvaluator sFloatEvaluator = new FloatEvaluator(); 9217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final TypeEvaluator sDoubleEvaluator = new DoubleEvaluator(); 9317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 9417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 9517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Used to indicate whether the animation is currently playing in reverse. This causes the 9617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * elapsed fraction to be inverted to calculate the appropriate values. 9717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 9817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private boolean mPlayingBackwards = false; 9917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 10017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 10117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * This variable tracks the current iteration that is playing. When mCurrentIteration exceeds the 10217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * repeatCount (if repeatCount!=INFINITE), the animation ends 10317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 10417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private int mCurrentIteration = 0; 10517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 10617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 10717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Tracks whether a startDelay'd animation has begun playing through the startDelay. 10817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 10917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private boolean mStartedDelay = false; 11017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 11117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 11217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Tracks the time at which the animation began playing through its startDelay. This is 11317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * different from the mStartTime variable, which is used to track when the animation became 11417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * active (which is when the startDelay expired and the animation was added to the active 11517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * animations list). 11617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 11717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private long mDelayStartTime; 11817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 11917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 12017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Flag that represents the current state of the animation. Used to figure out when to start 12117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * an animation (if state == STOPPED). Also used to end an animation that 12217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * has been cancel()'d or end()'d since the last animation frame. Possible values are 12317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * STOPPED, RUNNING, ENDED, CANCELED. 12417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 12517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private int mPlayingState = STOPPED; 12617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 12717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 12817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Internal collections used to avoid set collisions as animations start and end while being 12917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * processed. 13017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 13117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final ArrayList<Animator> sEndingAnims = new ArrayList<Animator>(); 13217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final ArrayList<Animator> sDelayedAnims = new ArrayList<Animator>(); 13317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final ArrayList<Animator> sReadyAnims = new ArrayList<Animator>(); 13417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 13517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // 13617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // Backing variables 13717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // 13817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 13917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // How long the animation should last in ms 14017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private long mDuration; 14117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 14217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The value that the animation should start from, set in the constructor 14317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private Object mValueFrom; 14417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 14517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The value that the animation should animate to, set in the constructor 14617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private Object mValueTo; 14717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 14817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The amount of time in ms to delay starting the animation after start() is called 14917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private long mStartDelay = 0; 15017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 15117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The number of milliseconds between animation frames 15217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static long sFrameDelay = DEFAULT_FRAME_DELAY; 15317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 15417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The number of times the animation will repeat. The default is 0, which means the animation 15517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // will play only once 15617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private int mRepeatCount = 0; 15717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 15817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 15917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The type of repetition that will occur when repeatMode is nonzero. RESTART means the 16017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * animation will start from the beginning on every new cycle. REVERSE means the animation 16117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * will reverse directions on each iteration. 16217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 16317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private int mRepeatMode = RESTART; 16417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 16517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 16617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The time interpolator to be used. The elapsed fraction of the animation will be passed 16717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * through this interpolator to calculate the interpolated fraction, which is then used to 16817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * calculate the animated values. 16917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 17017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private Interpolator mInterpolator = sDefaultInterpolator; 17117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 17217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 17317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The type evaluator used to calculate the animated values. This evaluator is determined 17417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * automatically based on the type of the start/end objects passed into the constructor, 17517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * but the system only knows about the primitive types int, double, and float. Any other 17617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * type will need to set the evaluator to a custom evaluator for that type. 17717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 17817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private TypeEvaluator mEvaluator; 17917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 18017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 18117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The set of listeners to be sent events through the life of an animation. 18217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 18317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private ArrayList<AnimatorUpdateListener> mUpdateListeners = null; 18417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 18517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 18617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The current value calculated by the animation. The value is calculated in animateFraction(), 18717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * prior to calling the setter (if set) and sending out the onAnimationUpdate() callback 18817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * to the update listeners. 18917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 19017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private Object mAnimatedValue = null; 19117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 19217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 19317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The type of the values, as determined by the valueFrom/valueTo properties. 19417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 19517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase Class mValueType; 19617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 19717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 19817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Public constants 19917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 20017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 20117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 20217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * When the animation reaches the end and <code>repeatCount</code> is INFINITE 20317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * or a positive value, the animation restarts from the beginning. 20417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 20517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public static final int RESTART = 1; 20617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 20717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * When the animation reaches the end and <code>repeatCount</code> is INFINITE 20817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * or a positive value, the animation reverses direction on every iteration. 20917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 21017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public static final int REVERSE = 2; 21117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 21217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * This value used used with the {@link #setRepeatCount(int)} property to repeat 21317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * the animation indefinitely. 21417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 21517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public static final int INFINITE = -1; 21617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 21717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private Animator(long duration, Object valueFrom, Object valueTo, Class valueType) { 21817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mDuration = duration; 21917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mValueFrom = valueFrom; 22017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mValueTo= valueTo; 22117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase this.mValueType = valueType; 22217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 22317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 22417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 22517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * This function is called immediately before processing the first animation 22617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * frame of an animation. If there is a nonzero <code>startDelay</code>, the 22717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * function is called after that delay ends. 22817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * It takes care of the final initialization steps for the 22917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * animation. 23017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 23117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <p>Overrides of this method should call the superclass method to ensure 23217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * that internal mechanisms for the animation are set up correctly.</p> 23317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 23417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase void initAnimation() { 23517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mEvaluator == null) { 23617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mEvaluator = (mValueType == int.class) ? sIntEvaluator : 23717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase (mValueType == double.class) ? sDoubleEvaluator : sFloatEvaluator; 23817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 23917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mPlayingBackwards = false; 24017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mCurrentIteration = 0; 24117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 24217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 24317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 24417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * A constructor that takes <code>float</code> values. 24517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 24617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param duration The length of the animation, in milliseconds. 24717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param valueFrom The initial value of the property when the animation begins. 24817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param valueTo The value to which the property will animate. 24917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 25017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public Animator(long duration, float valueFrom, float valueTo) { 25117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase this(duration, valueFrom, valueTo, float.class); 25217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 25317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 25417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 25517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * A constructor that takes <code>int</code> values. 25617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 25717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param duration The length of the animation, in milliseconds. 25817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param valueFrom The initial value of the property when the animation begins. 25917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param valueTo The value to which the property will animate. 26017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 26117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public Animator(long duration, int valueFrom, int valueTo) { 26217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase this(duration, valueFrom, valueTo, int.class); 26317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 26417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 26517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 26617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * A constructor that takes <code>double</code> values. 26717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 26817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param duration The length of the animation, in milliseconds. 26917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param valueFrom The initial value of the property when the animation begins. 27017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param valueTo The value to which the property will animate. 27117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 27217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public Animator(long duration, double valueFrom, double valueTo) { 27317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase this(duration, valueFrom, valueTo, double.class); 27417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 27517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 27617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 27717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * A constructor that takes <code>Object</code> values. 27817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 27917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param duration The length of the animation, in milliseconds. 28017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param valueFrom The initial value of the property when the animation begins. 28117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param valueTo The value to which the property will animate. 28217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 28317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public Animator(long duration, Object valueFrom, Object valueTo) { 28417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase this(duration, valueFrom, valueTo, 28517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase (valueFrom != null) ? valueFrom.getClass() : valueTo.getClass()); 28617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 28717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 28817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 28917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * This custom, static handler handles the timing pulse that is shared by 29017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * all active animations. This approach ensures that the setting of animation 29117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * values will happen on the UI thread and that all animations will share 29217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * the same times for calculating their values, which makes synchronizing 29317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * animations possible. 29417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 29517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 29617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static class AnimationHandler extends Handler { 29717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 29817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * There are only two messages that we care about: ANIMATION_START and 29917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * ANIMATION_FRAME. The START message is sent when an animation's start() 30017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * method is called. It cannot start synchronously when start() is called 30117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * because the call may be on the wrong thread, and it would also not be 30217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * synchronized with other animations because it would not start on a common 30317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * timing pulse. So each animation sends a START message to the handler, which 30417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * causes the handler to place the animation on the active animations queue and 30517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * start processing frames for that animation. 30617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The FRAME message is the one that is sent over and over while there are any 30717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * active animations to process. 30817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 30917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase @Override 31017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void handleMessage(Message msg) { 31117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase boolean callAgain = true; 31217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase switch (msg.what) { 31317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // TODO: should we avoid sending frame message when starting if we 31417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // were already running? 31517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase case ANIMATION_START: 31617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (sAnimations.size() > 0 || sDelayedAnims.size() > 0) { 31717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase callAgain = false; 31817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 31917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // pendingAnims holds any animations that have requested to be started 32017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // We're going to clear sPendingAnimations, but starting animation may 32117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // cause more to be added to the pending list (for example, if one animation 32217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // starting triggers another starting). So we loop until sPendingAnimations 32317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // is empty. 32417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase while (sPendingAnimations.size() > 0) { 32517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase ArrayList<Animator> pendingCopy = 32617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase (ArrayList<Animator>) sPendingAnimations.clone(); 32717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sPendingAnimations.clear(); 32817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase int count = pendingCopy.size(); 32917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase for (int i = 0; i < count; ++i) { 33017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase Animator anim = pendingCopy.get(i); 33117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // If the animation has a startDelay, place it on the delayed list 33217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (anim.mStartDelay == 0) { 33317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase anim.startAnimation(); 33417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } else { 33517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sDelayedAnims.add(anim); 33617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 33717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 33817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 33917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // fall through to process first frame of new animations 34017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase case ANIMATION_FRAME: 34117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // currentTime holds the common time for all animations processed 34217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // during this frame 34317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase long currentTime = AnimationUtils.currentAnimationTimeMillis(); 34417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 34517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // First, process animations currently sitting on the delayed queue, adding 34617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // them to the active animations if they are ready 34717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase int numDelayedAnims = sDelayedAnims.size(); 34817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase for (int i = 0; i < numDelayedAnims; ++i) { 34917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase Animator anim = sDelayedAnims.get(i); 35017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (anim.delayedAnimationFrame(currentTime)) { 35117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sReadyAnims.add(anim); 35217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 35317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 35417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase int numReadyAnims = sReadyAnims.size(); 35517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (numReadyAnims > 0) { 35617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase for (int i = 0; i < numReadyAnims; ++i) { 35717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase Animator anim = sReadyAnims.get(i); 35817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase anim.startAnimation(); 35917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sDelayedAnims.remove(anim); 36017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 36117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sReadyAnims.clear(); 36217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 36317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 36417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // Now process all active animations. The return value from animationFrame() 36517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // tells the handler whether it should now be ended 36617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase int numAnims = sAnimations.size(); 36717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase for (int i = 0; i < numAnims; ++i) { 36817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase Animator anim = sAnimations.get(i); 36917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (anim.animationFrame(currentTime)) { 37017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sEndingAnims.add(anim); 37117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 37217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 37317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (sEndingAnims.size() > 0) { 37417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase for (int i = 0; i < sEndingAnims.size(); ++i) { 37517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sEndingAnims.get(i).endAnimation(); 37617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 37717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sEndingAnims.clear(); 37817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 37917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 38017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // If there are still active or delayed animations, call the handler again 38117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // after the frameDelay 38217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (callAgain && (!sAnimations.isEmpty() || !sDelayedAnims.isEmpty())) { 38317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sendEmptyMessageDelayed(ANIMATION_FRAME, sFrameDelay); 38417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 38517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase break; 38617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 38717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 38817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 38917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 39017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 39117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The amount of time, in milliseconds, to delay starting the animation after 39217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * {@link #start()} is called. 39317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 39417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @return the number of milliseconds to delay running the animation 39517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 39617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public long getStartDelay() { 39717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase return mStartDelay; 39817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 39917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 40017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 40117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The amount of time, in milliseconds, to delay starting the animation after 40217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * {@link #start()} is called. 40317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 40417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param startDelay The amount of the delay, in milliseconds 40517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 40617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void setStartDelay(long startDelay) { 40717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase this.mStartDelay = startDelay; 40817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 40917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 41017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 41117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The amount of time, in milliseconds, between each frame of the animation. This is a 41217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * requested time that the animation will attempt to honor, but the actual delay between 41317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * frames may be different, depending on system load and capabilities. This is a static 41417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * function because the same delay will be applied to all animations, since they are all 41517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * run off of a single timing loop. 41617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 41717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @return the requested time between frames, in milliseconds 41817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 41917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public static long getFrameDelay() { 42017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase return sFrameDelay; 42117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 42217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 42317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 42417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Gets the value that this animation will start from. 42517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 42617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @return Object The starting value for the animation. 42717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 42817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public Object getValueFrom() { 42917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase return mValueFrom; 43017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 43117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 43217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 43317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Sets the value that this animation will start from. 43417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 43517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void setValueFrom(Object valueFrom) { 43617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mValueFrom = valueFrom; 43717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 43817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 43917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 44017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Gets the value that this animation will animate to. 44117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 44217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @return Object The ending value for the animation. 44317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 44417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public Object getValueTo() { 44517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase return mValueTo; 44617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 44717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 44817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 44917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Sets the value that this animation will animate to. 45017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 45117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @return Object The ending value for the animation. 45217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 45317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void setValueTo(Object valueTo) { 45417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mValueTo = valueTo; 45517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 45617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 45717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 45817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The amount of time, in milliseconds, between each frame of the animation. This is a 45917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * requested time that the animation will attempt to honor, but the actual delay between 46017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * frames may be different, depending on system load and capabilities. This is a static 46117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * function because the same delay will be applied to all animations, since they are all 46217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * run off of a single timing loop. 46317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 46417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param frameDelay the requested time between frames, in milliseconds 46517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 46617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public static void setFrameDelay(long frameDelay) { 46717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sFrameDelay = frameDelay; 46817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 46917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 47017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 47117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The most recent value calculated by this <code>Animator</code> for the property 47217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * being animated. This value is only sensible while the animation is running. The main 47317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * purpose for this read-only property is to retrieve the value from the <code>Animator</code> 47417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * during a call to {@link AnimatorUpdateListener#onAnimationUpdate(Animator)}, which 47517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * is called during each animation frame, immediately after the value is calculated. 47617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 47717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @return animatedValue The value most recently calculated by this <code>Animator</code> for 47817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * the property specified in the constructor. 47917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 48017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public Object getAnimatedValue() { 48117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase return mAnimatedValue; 48217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 48317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 48417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 48517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Sets how many times the animation should be repeated. If the repeat 48617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * count is 0, the animation is never repeated. If the repeat count is 48717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * greater than 0 or {@link #INFINITE}, the repeat mode will be taken 48817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * into account. The repeat count is 0 by default. 48917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 49017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param value the number of times the animation should be repeated 49117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 49217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void setRepeatCount(int value) { 49317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mRepeatCount = value; 49417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 49517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 49617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Defines how many times the animation should repeat. The default value 49717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * is 0. 49817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 49917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @return the number of times the animation should repeat, or {@link #INFINITE} 50017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 50117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public int getRepeatCount() { 50217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase return mRepeatCount; 50317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 50417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 50517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 50617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Defines what this animation should do when it reaches the end. This 50717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * setting is applied only when the repeat count is either greater than 50817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 0 or {@link #INFINITE}. Defaults to {@link #RESTART}. 50917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 51017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param value {@link #RESTART} or {@link #REVERSE} 51117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 51217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void setRepeatMode(int value) { 51317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mRepeatMode = value; 51417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 51517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 51617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 51717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Defines what this animation should do when it reaches the end. 51817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 51917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @return either one of {@link #REVERSE} or {@link #RESTART} 52017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 52117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public int getRepeatMode() { 52217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase return mRepeatMode; 52317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 52417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 52517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 52617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Adds a listener to the set of listeners that are sent update events through the life of 52717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * an animation. This method is called on all listeners for every frame of the animation, 52817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * after the values for the animation have been calculated. 52917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 53017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param listener the listener to be added to the current set of listeners for this animation. 53117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 53217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void addUpdateListener(AnimatorUpdateListener listener) { 53317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mUpdateListeners == null) { 53417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mUpdateListeners = new ArrayList<AnimatorUpdateListener>(); 53517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 53617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mUpdateListeners.add(listener); 53717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 53817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 53917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 54017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Removes a listener from the set listening to frame updates for this animation. 54117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 54217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param listener the listener to be removed from the current set of update listeners 54317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * for this animation. 54417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 54517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void removeUpdateListener(AnimatorUpdateListener listener) { 54617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mUpdateListeners == null) { 54717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase return; 54817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 54917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mUpdateListeners.remove(listener); 55017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mUpdateListeners.size() == 0) { 55117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mUpdateListeners = null; 55217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 55317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 55417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 55517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 55617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 55717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The time interpolator used in calculating the elapsed fraction of this animation. The 55817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * interpolator determines whether the animation runs with linear or non-linear motion, 55917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * such as acceleration and deceleration. The default value is 56017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * {@link android.view.animation.AccelerateDecelerateInterpolator} 56117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 56217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param value the interpolator to be used by this animation 56317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 56417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void setInterpolator(Interpolator value) { 56517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (value != null) { 56617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mInterpolator = value; 56717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 56817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 56917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 57017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 57117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The type evaluator to be used when calculating the animated values of this animation. 57217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The system will automatically assign a float, int, or double evaluator based on the type 57317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * of <code>startValue</code> and <code>endValue</code> in the constructor. But if these values 57417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * are not one of these primitive types, or if different evaluation is desired (such as is 57517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * necessary with int values that represent colors), a custom evaluator needs to be assigned. 57617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * For example, when running an animation on color values, the {@link RGBEvaluator} 57717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * should be used to get correct RGB color interpolation. 57817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 57917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param value the evaluator to be used this animation 58017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 58117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void setEvaluator(TypeEvaluator value) { 58217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (value != null) { 58317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mEvaluator = value; 58417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 58517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 58617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 58717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void start() { 58817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sPendingAnimations.add(this); 58917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (sAnimationHandler == null) { 59017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sAnimationHandler = new AnimationHandler(); 59117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 59217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // TODO: does this put too many messages on the queue if the handler 59317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // is already running? 59417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sAnimationHandler.sendEmptyMessage(ANIMATION_START); 59517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 59617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 59717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void cancel() { 59817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mListeners != null) { 59917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase ArrayList<AnimatableListener> tmpListeners = 60017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase (ArrayList<AnimatableListener>) mListeners.clone(); 60117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase for (AnimatableListener listener : tmpListeners) { 60217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase listener.onAnimationCancel(this); 60317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 60417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 60517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // Just set the CANCELED flag - this causes the animation to end the next time a frame 60617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // is processed. 60717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mPlayingState = CANCELED; 60817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 60917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 61017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void end() { 61117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // Just set the ENDED flag - this causes the animation to end the next time a frame 61217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // is processed. 61317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mPlayingState = ENDED; 61417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 61517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 61617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 61717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Called internally to end an animation by removing it from the animations list. Must be 61817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * called on the UI thread. 61917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 62017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private void endAnimation() { 62117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sAnimations.remove(this); 62217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mListeners != null) { 62317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase ArrayList<AnimatableListener> tmpListeners = 62417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase (ArrayList<AnimatableListener>) mListeners.clone(); 62517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase for (AnimatableListener listener : tmpListeners) { 62617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase listener.onAnimationEnd(this); 62717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 62817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 62917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mPlayingState = STOPPED; 63017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 63117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 63217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 63317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Called internally to start an animation by adding it to the active animations list. Must be 63417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * called on the UI thread. 63517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 63617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private void startAnimation() { 63717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase initAnimation(); 63817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sAnimations.add(this); 63917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mListeners != null) { 64017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase ArrayList<AnimatableListener> tmpListeners = 64117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase (ArrayList<AnimatableListener>) mListeners.clone(); 64217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase for (AnimatableListener listener : tmpListeners) { 64317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase listener.onAnimationStart(this); 64417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 64517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 64617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 64717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 64817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 64917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Internal function called to process an animation frame on an animation that is currently 65017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * sleeping through its <code>startDelay</code> phase. The return value indicates whether it 65117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * should be woken up and put on the active animations queue. 65217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 65317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param currentTime The current animation time, used to calculate whether the animation 65417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * has exceeded its <code>startDelay</code> and should be started. 65517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @return True if the animation's <code>startDelay</code> has been exceeded and the animation 65617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * should be added to the set of active animations. 65717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 65817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private boolean delayedAnimationFrame(long currentTime) { 65917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (!mStartedDelay) { 66017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mStartedDelay = true; 66117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mDelayStartTime = currentTime; 66217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } else { 66317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase long deltaTime = currentTime - mDelayStartTime; 66417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (deltaTime > mStartDelay) { 66517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // startDelay ended - start the anim and record the 66617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // mStartTime appropriately 66717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mStartTime = currentTime - (deltaTime - mStartDelay); 66817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mPlayingState = RUNNING; 66917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase return true; 67017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 67117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 67217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase return false; 67317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 67417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 67517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 67617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * This internal function processes a single animation frame for a given animation. The 67717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * currentTime parameter is the timing pulse sent by the handler, used to calculate the 67817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * elapsed duration, and therefore 67917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * the elapsed fraction, of the animation. The return value indicates whether the animation 68017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * should be ended (which happens when the elapsed time of the animation exceeds the 68117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * animation's duration, including the repeatCount). 68217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 68317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param currentTime The current time, as tracked by the static timing handler 68417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @return true if the animation's duration, including any repetitions due to 68517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <code>repeatCount</code> has been exceeded and the animation should be ended. 68617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 68717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private boolean animationFrame(long currentTime) { 68817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 68917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase boolean done = false; 69017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 69117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mPlayingState == STOPPED) { 69217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mPlayingState = RUNNING; 69317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mStartTime = currentTime; 69417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 69517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase switch (mPlayingState) { 69617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase case RUNNING: 69717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase float fraction = (float)(currentTime - mStartTime) / mDuration; 69817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (fraction >= 1f) { 69917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mCurrentIteration < mRepeatCount || mRepeatCount == INFINITE) { 70017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // Time to repeat 70117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mListeners != null) { 70217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase for (AnimatableListener listener : mListeners) { 70317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase listener.onAnimationRepeat(this); 70417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 70517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 70617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase ++mCurrentIteration; 70717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mRepeatMode == REVERSE) { 70817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mPlayingBackwards = mPlayingBackwards ? false : true; 70917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 71017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // TODO: doesn't account for fraction going Wayyyyy over 1, like 2+ 71117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase fraction = fraction - 1f; 71217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mStartTime += mDuration; 71317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } else { 71417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase done = true; 71517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase fraction = Math.min(fraction, 1.0f); 71617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 71717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 71817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mPlayingBackwards) { 71917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase fraction = 1f - fraction; 72017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 72117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase animateValue(fraction); 72217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase break; 72317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase case ENDED: 72417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The final value set on the target varies, depending on whether the animation 72517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // was supposed to repeat an odd number of times 72617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mRepeatCount > 0 && (mRepeatCount & 0x01) == 1) { 72717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase animateValue(0f); 72817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } else { 72917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase animateValue(1f); 73017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 73117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // Fall through to set done flag 73217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase case CANCELED: 73317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase done = true; 73417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase break; 73517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 73617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 73717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase return done; 73817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 73917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 74017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 74117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * This method is called with the elapsed fraction of the animation during every 74217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * animation frame. This function turns the elapsed fraction into an interpolated fraction 74317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * and then into an animated value (from the evaluator. The function is called mostly during 74417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * animation updates, but it is also called when the <code>end()</code> 74517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * function is called, to set the final value on the property. 74617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 74717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <p>Overrides of this method must call the superclass to perform the calculation 74817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * of the animated value.</p> 74917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 75017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param fraction The elapsed fraction of the animation. 75117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 75217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase void animateValue(float fraction) { 75317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase fraction = mInterpolator.getInterpolation(fraction); 75417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mAnimatedValue = mEvaluator.evaluate(fraction, mValueFrom, mValueTo); 75517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mUpdateListeners != null) { 75617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase int numListeners = mUpdateListeners.size(); 75717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase for (int i = 0; i < numListeners; ++i) { 75817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mUpdateListeners.get(i).onAnimationUpdate(this); 75917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 76017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 76117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 76217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 76317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 76417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Implementors of this interface can add themselves as update listeners 76517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * to an <code>Animator</code> instance to receive callbacks on every animation 76617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * frame, after the current frame's values have been calculated for that 76717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <code>Animator</code>. 76817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 76917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public static interface AnimatorUpdateListener { 77017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 77117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <p>Notifies the occurrence of another frame of the animation.</p> 77217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 77317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param animation The animation which was repeated. 77417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 77517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase void onAnimationUpdate(Animator animation); 77617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 77717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 77817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase}