Animator.java revision d953d08e9299072130d9f4411cbcf6678bbce822
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 19f54a8d7c479485174941c38f151ea7083c658da3Chet Haaseimport android.content.Context; 20f54a8d7c479485174941c38f151ea7083c658da3Chet Haaseimport android.content.res.TypedArray; 2117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport android.os.Handler; 2217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport android.os.Message; 23f54a8d7c479485174941c38f151ea7083c658da3Chet Haaseimport android.util.AttributeSet; 2417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport android.view.animation.AccelerateDecelerateInterpolator; 2517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport android.view.animation.AnimationUtils; 2617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport android.view.animation.Interpolator; 2717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 2817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport java.util.ArrayList; 29d953d08e9299072130d9f4411cbcf6678bbce822Chet Haaseimport java.util.HashMap; 3017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 3117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase/** 3217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * This class provides a simple timing engine for running animations 3317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * which calculate animated values and set them on target objects. 3417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 35010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * <p>There is a single timing pulse that all animations use. It runs in a 36010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * custom handler to ensure that property changes happen on the UI thread.</p> 37010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * 38010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * <p>By default, Animator uses non-linear time interpolation, via the 39010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * {@link AccelerateDecelerateInterpolator} class, which accelerates into and decelerates 40010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * out of an animation. This behavior can be changed by calling 41010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * {@link Animator#setInterpolator(Interpolator)}.</p> 4217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 43d953d08e9299072130d9f4411cbcf6678bbce822Chet Haasepublic class Animator<T> extends Animatable { 4417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 4517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 4617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Internal constants 4717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 4817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 4917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /* 5017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The default amount of time in ms between animation frames 5117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 5217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final long DEFAULT_FRAME_DELAY = 30; 5317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 5417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 5517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Messages sent to timing handler: START is sent when an animation first begins, FRAME is sent 5617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * by the handler to itself to process the next animation frame 5717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 5817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final int ANIMATION_START = 0; 5917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final int ANIMATION_FRAME = 1; 6017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 6117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 6217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Values used with internal variable mPlayingState to indicate the current state of an 6317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * animation. 6417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 6517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final int STOPPED = 0; // Not yet playing 6617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final int RUNNING = 1; // Playing normally 6717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final int CANCELED = 2; // cancel() called - need to end it 6817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final int ENDED = 3; // end() called - need to end it 695d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase private static final int SEEKED = 4; // Seeked to some time value 7017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 7117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 72f54a8d7c479485174941c38f151ea7083c658da3Chet Haase * Enum values used in XML attributes to indicate the value for mValueType 73f54a8d7c479485174941c38f151ea7083c658da3Chet Haase */ 74f54a8d7c479485174941c38f151ea7083c658da3Chet Haase private static final int VALUE_TYPE_FLOAT = 0; 75f54a8d7c479485174941c38f151ea7083c658da3Chet Haase private static final int VALUE_TYPE_INT = 1; 76f54a8d7c479485174941c38f151ea7083c658da3Chet Haase private static final int VALUE_TYPE_DOUBLE = 2; 77f54a8d7c479485174941c38f151ea7083c658da3Chet Haase private static final int VALUE_TYPE_COLOR = 3; 78f54a8d7c479485174941c38f151ea7083c658da3Chet Haase private static final int VALUE_TYPE_CUSTOM = 4; 79f54a8d7c479485174941c38f151ea7083c658da3Chet Haase 80f54a8d7c479485174941c38f151ea7083c658da3Chet Haase /** 8117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Internal variables 8217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 8317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 8417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 8517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The first time that the animation's animateFrame() method is called. This time is used to 8617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // determine elapsed time (and therefore the elapsed fraction) in subsequent calls 8717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // to animateFrame() 8817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private long mStartTime; 8917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 905d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase /** 915d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase * Set when setCurrentPlayTime() is called. If negative, animation is not currently seeked 925d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase * to a value. 935d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase */ 945d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase private long mSeekTime = -1; 955d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase 9617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The static sAnimationHandler processes the internal timing loop on which all animations 9717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // are based 9817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static AnimationHandler sAnimationHandler; 9917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 10017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The static list of all active animations 10117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final ArrayList<Animator> sAnimations = new ArrayList<Animator>(); 10217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 10317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The set of animations to be started on the next animation frame 10417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final ArrayList<Animator> sPendingAnimations = new ArrayList<Animator>(); 10517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 10617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The time interpolator to be used if none is set on the animation 10717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final Interpolator sDefaultInterpolator = new AccelerateDecelerateInterpolator(); 10817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 10917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // type evaluators for the three primitive types handled by this implementation 11017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final TypeEvaluator sIntEvaluator = new IntEvaluator(); 11117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final TypeEvaluator sFloatEvaluator = new FloatEvaluator(); 11217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final TypeEvaluator sDoubleEvaluator = new DoubleEvaluator(); 11317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 11417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 11517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Used to indicate whether the animation is currently playing in reverse. This causes the 11617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * elapsed fraction to be inverted to calculate the appropriate values. 11717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 11817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private boolean mPlayingBackwards = false; 11917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 12017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 12117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * This variable tracks the current iteration that is playing. When mCurrentIteration exceeds the 12217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * repeatCount (if repeatCount!=INFINITE), the animation ends 12317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 12417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private int mCurrentIteration = 0; 12517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 12617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 12717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Tracks whether a startDelay'd animation has begun playing through the startDelay. 12817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 12917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private boolean mStartedDelay = false; 13017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 13117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 13217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Tracks the time at which the animation began playing through its startDelay. This is 13317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * different from the mStartTime variable, which is used to track when the animation became 13417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * active (which is when the startDelay expired and the animation was added to the active 13517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * animations list). 13617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 13717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private long mDelayStartTime; 13817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 13917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 14017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Flag that represents the current state of the animation. Used to figure out when to start 14117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * an animation (if state == STOPPED). Also used to end an animation that 14217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * has been cancel()'d or end()'d since the last animation frame. Possible values are 14317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * STOPPED, RUNNING, ENDED, CANCELED. 14417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 14517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private int mPlayingState = STOPPED; 14617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 14717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 14817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Internal collections used to avoid set collisions as animations start and end while being 14917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * processed. 15017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 15117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final ArrayList<Animator> sEndingAnims = new ArrayList<Animator>(); 15217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final ArrayList<Animator> sDelayedAnims = new ArrayList<Animator>(); 15317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static final ArrayList<Animator> sReadyAnims = new ArrayList<Animator>(); 15417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 1555d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase /** 1565d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase * Flag that denotes whether the animation is set up and ready to go. Used by seek() to 1575d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase * set up animation that has not yet been started. 1585d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase */ 1595d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase private boolean mInitialized = false; 1605d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase 16117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // 16217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // Backing variables 16317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // 16417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 16517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // How long the animation should last in ms 16617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private long mDuration; 16717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 16817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The amount of time in ms to delay starting the animation after start() is called 16917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private long mStartDelay = 0; 17017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 17117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The number of milliseconds between animation frames 17217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static long sFrameDelay = DEFAULT_FRAME_DELAY; 17317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 17417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The number of times the animation will repeat. The default is 0, which means the animation 17517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // will play only once 17617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private int mRepeatCount = 0; 17717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 17817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 17917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The type of repetition that will occur when repeatMode is nonzero. RESTART means the 18017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * animation will start from the beginning on every new cycle. REVERSE means the animation 18117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * will reverse directions on each iteration. 18217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 18317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private int mRepeatMode = RESTART; 18417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 18517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 18617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The time interpolator to be used. The elapsed fraction of the animation will be passed 18717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * through this interpolator to calculate the interpolated fraction, which is then used to 18817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * calculate the animated values. 18917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 19017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private Interpolator mInterpolator = sDefaultInterpolator; 19117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 19217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 19317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The set of listeners to be sent events through the life of an animation. 19417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 19517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private ArrayList<AnimatorUpdateListener> mUpdateListeners = null; 19617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 19717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 198d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * The property/value sets being animated. 19917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 200d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase HashMap<String, PropertyValuesHolder> mValues; 20117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 20217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 203d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * This value is used in the simple/common case of animating just one value; the user 204d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * may call getAnimatedValue(), which should return the value of the first (and only) 205d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * ProeprtyValuesHolder animated value, which is looked up using this string. 2063dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase */ 207d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase String mFirstPropertyName; 208d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase 2093dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase 2103dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase /** 21117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The type of the values, as determined by the valueFrom/valueTo properties. 21217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 21317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase Class mValueType; 21417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 21517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 21617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Public constants 21717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 21817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 21917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 22017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * When the animation reaches the end and <code>repeatCount</code> is INFINITE 22117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * or a positive value, the animation restarts from the beginning. 22217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 22317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public static final int RESTART = 1; 22417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 22517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * When the animation reaches the end and <code>repeatCount</code> is INFINITE 22617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * or a positive value, the animation reverses direction on every iteration. 22717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 22817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public static final int REVERSE = 2; 22917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 23017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * This value used used with the {@link #setRepeatCount(int)} property to repeat 23117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * the animation indefinitely. 23217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 23317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public static final int INFINITE = -1; 23417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 235f54a8d7c479485174941c38f151ea7083c658da3Chet Haase /** 236f54a8d7c479485174941c38f151ea7083c658da3Chet Haase * Creates a new animation whose parameters come from the specified context and 237f54a8d7c479485174941c38f151ea7083c658da3Chet Haase * attributes set. 238f54a8d7c479485174941c38f151ea7083c658da3Chet Haase * 239f54a8d7c479485174941c38f151ea7083c658da3Chet Haase * @param context the application environment 240f54a8d7c479485174941c38f151ea7083c658da3Chet Haase * @param attrs the set of attributes holding the animation parameters 241f54a8d7c479485174941c38f151ea7083c658da3Chet Haase */ 242f54a8d7c479485174941c38f151ea7083c658da3Chet Haase public Animator(Context context, AttributeSet attrs) { 243f54a8d7c479485174941c38f151ea7083c658da3Chet Haase TypedArray a = 244f54a8d7c479485174941c38f151ea7083c658da3Chet Haase context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Animator); 245f54a8d7c479485174941c38f151ea7083c658da3Chet Haase 246f54a8d7c479485174941c38f151ea7083c658da3Chet Haase mDuration = (long) a.getInt(com.android.internal.R.styleable.Animator_duration, 0); 247f54a8d7c479485174941c38f151ea7083c658da3Chet Haase 248f54a8d7c479485174941c38f151ea7083c658da3Chet Haase mStartDelay = (long) a.getInt(com.android.internal.R.styleable.Animator_startOffset, 0); 249f54a8d7c479485174941c38f151ea7083c658da3Chet Haase 250f54a8d7c479485174941c38f151ea7083c658da3Chet Haase final int resID = 251f54a8d7c479485174941c38f151ea7083c658da3Chet Haase a.getResourceId(com.android.internal.R.styleable.Animator_interpolator, 0); 252f54a8d7c479485174941c38f151ea7083c658da3Chet Haase if (resID > 0) { 253f54a8d7c479485174941c38f151ea7083c658da3Chet Haase setInterpolator(AnimationUtils.loadInterpolator(context, resID)); 254f54a8d7c479485174941c38f151ea7083c658da3Chet Haase } 255f54a8d7c479485174941c38f151ea7083c658da3Chet Haase int valueType = a.getInt(com.android.internal.R.styleable.Animator_valueType, 256f54a8d7c479485174941c38f151ea7083c658da3Chet Haase VALUE_TYPE_FLOAT); 257f54a8d7c479485174941c38f151ea7083c658da3Chet Haase 258d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase Object valueFrom = null; 259d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase Object valueTo = null; 260d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase 261f54a8d7c479485174941c38f151ea7083c658da3Chet Haase switch (valueType) { 262f54a8d7c479485174941c38f151ea7083c658da3Chet Haase case VALUE_TYPE_FLOAT: 263d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase valueFrom = a.getFloat(com.android.internal.R.styleable.Animator_valueFrom, 0f); 264d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase valueTo = a.getFloat(com.android.internal.R.styleable.Animator_valueTo, 0f); 265f54a8d7c479485174941c38f151ea7083c658da3Chet Haase mValueType = float.class; 266f54a8d7c479485174941c38f151ea7083c658da3Chet Haase break; 267f54a8d7c479485174941c38f151ea7083c658da3Chet Haase case VALUE_TYPE_INT: 268d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase valueFrom = a.getInt(com.android.internal.R.styleable.Animator_valueFrom, 0); 269d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase valueTo = a.getInt(com.android.internal.R.styleable.Animator_valueTo, 0); 270f54a8d7c479485174941c38f151ea7083c658da3Chet Haase mValueType = int.class; 271f54a8d7c479485174941c38f151ea7083c658da3Chet Haase break; 272f54a8d7c479485174941c38f151ea7083c658da3Chet Haase case VALUE_TYPE_DOUBLE: 273d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase valueFrom = (double) 274f54a8d7c479485174941c38f151ea7083c658da3Chet Haase a.getFloat(com.android.internal.R.styleable.Animator_valueFrom, 0f); 275d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase valueTo = (double) 276f54a8d7c479485174941c38f151ea7083c658da3Chet Haase a.getFloat(com.android.internal.R.styleable.Animator_valueTo, 0f); 277f54a8d7c479485174941c38f151ea7083c658da3Chet Haase mValueType = double.class; 278f54a8d7c479485174941c38f151ea7083c658da3Chet Haase break; 279f54a8d7c479485174941c38f151ea7083c658da3Chet Haase case VALUE_TYPE_COLOR: 280d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase valueFrom = a.getInt(com.android.internal.R.styleable.Animator_valueFrom, 0); 281d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase valueTo = a.getInt(com.android.internal.R.styleable.Animator_valueTo, 0); 282d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase setEvaluator(new RGBEvaluator()); 283f54a8d7c479485174941c38f151ea7083c658da3Chet Haase mValueType = int.class; 284f54a8d7c479485174941c38f151ea7083c658da3Chet Haase break; 285f54a8d7c479485174941c38f151ea7083c658da3Chet Haase case VALUE_TYPE_CUSTOM: 286f54a8d7c479485174941c38f151ea7083c658da3Chet Haase // TODO: How to get an 'Object' value? 287d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase valueFrom = a.getFloat(com.android.internal.R.styleable.Animator_valueFrom, 0f); 288d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase valueTo = a.getFloat(com.android.internal.R.styleable.Animator_valueTo, 0f); 289f54a8d7c479485174941c38f151ea7083c658da3Chet Haase mValueType = Object.class; 290f54a8d7c479485174941c38f151ea7083c658da3Chet Haase break; 291f54a8d7c479485174941c38f151ea7083c658da3Chet Haase } 292f54a8d7c479485174941c38f151ea7083c658da3Chet Haase 293d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase mValues = new HashMap<String, PropertyValuesHolder>(1); 294d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase mFirstPropertyName = ""; 295d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase PropertyValuesHolder valuesHolder = new PropertyValuesHolder(mFirstPropertyName, 296d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase valueFrom, valueTo); 297d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase mValues.put(mFirstPropertyName, valuesHolder); 298d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase 299f54a8d7c479485174941c38f151ea7083c658da3Chet Haase mRepeatCount = a.getInt(com.android.internal.R.styleable.Animator_repeatCount, mRepeatCount); 300f54a8d7c479485174941c38f151ea7083c658da3Chet Haase mRepeatMode = a.getInt(com.android.internal.R.styleable.Animator_repeatMode, RESTART); 301f54a8d7c479485174941c38f151ea7083c658da3Chet Haase 302f54a8d7c479485174941c38f151ea7083c658da3Chet Haase a.recycle(); 303f54a8d7c479485174941c38f151ea7083c658da3Chet Haase } 304f54a8d7c479485174941c38f151ea7083c658da3Chet Haase 30517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 30617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 307d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * Constructs an Animator object with the specified duration and set of 308d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * values. If the values are a set of PropertyValuesHolder objects, then these objects 309d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * define the potentially multiple properties being animated and the values the properties are 310d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * animated between. Otherwise, the values define a single set of values animated between. 3113dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase * 3123dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase * @param duration The length of the animation, in milliseconds. 313d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * @param values The set of values to animate between. If these values are not 314d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * PropertyValuesHolder objects, then there should be more than one value, since the values 315d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * determine the interval to animate between. 3163dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase */ 317d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase public Animator(long duration, T...values) { 3183dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase mDuration = duration; 319d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase if (values.length > 0) { 320d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase setValues(values); 321d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase } 322fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase } 323fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase 324d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase public void setValues(PropertyValuesHolder... values) { 325d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase int numValues = values.length; 326d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase mValues = new HashMap<String, PropertyValuesHolder>(numValues); 327d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase for (int i = 0; i < numValues; ++i) { 328d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase PropertyValuesHolder valuesHolder = (PropertyValuesHolder) values[i]; 329d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase mValues.put(valuesHolder.getPropertyName(), valuesHolder); 330d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase } 331d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase if (numValues > 0 && values[0] != null) { 332d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase mFirstPropertyName = ((PropertyValuesHolder) values[0]).getPropertyName(); 333d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase } else { 334d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase mFirstPropertyName = ""; 335d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase } 336fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase } 337fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase 338fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase /** 339d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * Sets the values to animate between for this animation. If <code>values</code> is 340d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * a set of PropertyValuesHolder objects, these objects will become the set of properties 341d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * animated and the values that those properties are animated between. Otherwise, this method 342d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * will set only one set of values for the Animator. Also, if the values are not 343d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * PropertyValuesHolder objects and if there are already multiple sets of 344d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * values defined for this Animator via 345d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * more than one PropertyValuesHolder objects, this method will set the values for 346d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * the first of those objects. 347fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase * 348d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * @param values The set of values to animate between. 349fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase */ 350d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase public void setValues(T... values) { 351d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase if (values[0] instanceof PropertyValuesHolder) { 352d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase int numValues = values.length; 353d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase mValues = new HashMap<String, PropertyValuesHolder>(numValues); 354d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase for (int i = 0; i < numValues; ++i) { 355d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase PropertyValuesHolder valuesHolder = (PropertyValuesHolder) values[i]; 356d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase mValues.put(valuesHolder.getPropertyName(), valuesHolder); 357d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase } 358d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase if (numValues > 0 && values[0] != null) { 359d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase mFirstPropertyName = ((PropertyValuesHolder) values[0]).getPropertyName(); 360d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase } else { 361d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase mFirstPropertyName = ""; 362d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase } 363d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase } else { 364d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase if (mValues == null) { 365d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase setValues(new PropertyValuesHolder[]{ 366d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase new PropertyValuesHolder("", (Object[])values)}); 367d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase } else { 368d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase PropertyValuesHolder valuesHolder = mValues.get(mFirstPropertyName); 369d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase valuesHolder.setValues(values); 370d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase } 371d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase } 372fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase } 373fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase 374fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase /** 375fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase * This function is called immediately before processing the first animation 376fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase * frame of an animation. If there is a nonzero <code>startDelay</code>, the 377fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase * function is called after that delay ends. 378fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase * It takes care of the final initialization steps for the 379fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase * animation. 380fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase * 381fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase * <p>Overrides of this method should call the superclass method to ensure 382fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase * that internal mechanisms for the animation are set up correctly.</p> 383fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase */ 384fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase void initAnimation() { 385d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase for (PropertyValuesHolder pvHolder: mValues.values()) { 386d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase pvHolder.init(); 387fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase } 388fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase mCurrentIteration = 0; 3895d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase mInitialized = true; 3905d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase } 3915d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase 3925d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase /** 3935d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase * Sets the position of the animation to the specified point in time. This time should 3945d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase * be between 0 and the total duration of the animation, including any repetition. If 3955d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase * the animation has not yet been started, then it will not advance forward after it is 3965d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase * set to this time; it will simply set the time to this value and perform any appropriate 3975d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase * actions based on that time. If the animation is already running, then seek() will 3985d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase * set the current playing time to this value and continue playing from that point. 3995d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase * 4005d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase * @param playTime The time, in milliseconds, to which the animation is advanced or rewound. 4015d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase */ 4025d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase public void setCurrentPlayTime(long playTime) { 4035d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase if (!mInitialized) { 4045d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase initAnimation(); 4055d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase } 4065d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase long currentTime = AnimationUtils.currentAnimationTimeMillis(); 4075d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase if (mPlayingState != RUNNING) { 4085d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase mSeekTime = playTime; 4095d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase mPlayingState = SEEKED; 4105d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase } 4115d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase mStartTime = currentTime - playTime; 4125d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase animationFrame(currentTime); 4135d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase } 4145d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase 4155d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase /** 4165d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase * Gets the current position of the animation in time, which is equal to the current 4175d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase * time minus the time that the animation started. An animation that is not yet started will 4185d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase * return a value of zero. 4195d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase * 4205d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase * @return The current position in time of the animation. 4215d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase */ 4225d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase public long getCurrentPlayTime() { 4235d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase if (!mInitialized) { 4245d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase return 0; 4255d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase } 4265d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase return AnimationUtils.currentAnimationTimeMillis() - mStartTime; 427fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase } 428fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase 429fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase /** 43017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * This custom, static handler handles the timing pulse that is shared by 43117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * all active animations. This approach ensures that the setting of animation 43217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * values will happen on the UI thread and that all animations will share 43317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * the same times for calculating their values, which makes synchronizing 43417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * animations possible. 43517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 43617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 43717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private static class AnimationHandler extends Handler { 43817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 43917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * There are only two messages that we care about: ANIMATION_START and 44017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * ANIMATION_FRAME. The START message is sent when an animation's start() 44117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * method is called. It cannot start synchronously when start() is called 44217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * because the call may be on the wrong thread, and it would also not be 44317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * synchronized with other animations because it would not start on a common 44417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * timing pulse. So each animation sends a START message to the handler, which 44517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * causes the handler to place the animation on the active animations queue and 44617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * start processing frames for that animation. 44717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The FRAME message is the one that is sent over and over while there are any 44817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * active animations to process. 44917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 45017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase @Override 45117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void handleMessage(Message msg) { 45217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase boolean callAgain = true; 45317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase switch (msg.what) { 45417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // TODO: should we avoid sending frame message when starting if we 45517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // were already running? 45617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase case ANIMATION_START: 45717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (sAnimations.size() > 0 || sDelayedAnims.size() > 0) { 45817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase callAgain = false; 45917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 46017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // pendingAnims holds any animations that have requested to be started 46117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // We're going to clear sPendingAnimations, but starting animation may 46217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // cause more to be added to the pending list (for example, if one animation 46317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // starting triggers another starting). So we loop until sPendingAnimations 46417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // is empty. 46517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase while (sPendingAnimations.size() > 0) { 46617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase ArrayList<Animator> pendingCopy = 46717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase (ArrayList<Animator>) sPendingAnimations.clone(); 46817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sPendingAnimations.clear(); 46917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase int count = pendingCopy.size(); 47017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase for (int i = 0; i < count; ++i) { 47117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase Animator anim = pendingCopy.get(i); 47217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // If the animation has a startDelay, place it on the delayed list 47317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (anim.mStartDelay == 0) { 47417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase anim.startAnimation(); 47517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } else { 47617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sDelayedAnims.add(anim); 47717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 47817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 47917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 48017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // fall through to process first frame of new animations 48117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase case ANIMATION_FRAME: 48217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // currentTime holds the common time for all animations processed 48317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // during this frame 48417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase long currentTime = AnimationUtils.currentAnimationTimeMillis(); 48517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 48617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // First, process animations currently sitting on the delayed queue, adding 48717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // them to the active animations if they are ready 48817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase int numDelayedAnims = sDelayedAnims.size(); 48917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase for (int i = 0; i < numDelayedAnims; ++i) { 49017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase Animator anim = sDelayedAnims.get(i); 49117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (anim.delayedAnimationFrame(currentTime)) { 49217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sReadyAnims.add(anim); 49317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 49417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 49517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase int numReadyAnims = sReadyAnims.size(); 49617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (numReadyAnims > 0) { 49717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase for (int i = 0; i < numReadyAnims; ++i) { 49817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase Animator anim = sReadyAnims.get(i); 49917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase anim.startAnimation(); 50017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sDelayedAnims.remove(anim); 50117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 50217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sReadyAnims.clear(); 50317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 50417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 50517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // Now process all active animations. The return value from animationFrame() 50617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // tells the handler whether it should now be ended 50717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase int numAnims = sAnimations.size(); 50817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase for (int i = 0; i < numAnims; ++i) { 50917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase Animator anim = sAnimations.get(i); 51017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (anim.animationFrame(currentTime)) { 51117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sEndingAnims.add(anim); 51217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 51317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 51417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (sEndingAnims.size() > 0) { 51517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase for (int i = 0; i < sEndingAnims.size(); ++i) { 51617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sEndingAnims.get(i).endAnimation(); 51717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 51817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sEndingAnims.clear(); 51917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 52017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 52117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // If there are still active or delayed animations, call the handler again 52217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // after the frameDelay 52317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (callAgain && (!sAnimations.isEmpty() || !sDelayedAnims.isEmpty())) { 52417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sendEmptyMessageDelayed(ANIMATION_FRAME, sFrameDelay); 52517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 52617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase break; 52717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 52817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 52917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 53017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 53117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 53217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The amount of time, in milliseconds, to delay starting the animation after 53317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * {@link #start()} is called. 53417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 53517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @return the number of milliseconds to delay running the animation 53617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 53717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public long getStartDelay() { 53817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase return mStartDelay; 53917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 54017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 54117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 54217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The amount of time, in milliseconds, to delay starting the animation after 54317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * {@link #start()} is called. 54417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 54517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param startDelay The amount of the delay, in milliseconds 54617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 54717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void setStartDelay(long startDelay) { 54817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase this.mStartDelay = startDelay; 54917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 55017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 55117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 55217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The amount of time, in milliseconds, between each frame of the animation. This is a 55317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * requested time that the animation will attempt to honor, but the actual delay between 55417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * frames may be different, depending on system load and capabilities. This is a static 55517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * function because the same delay will be applied to all animations, since they are all 55617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * run off of a single timing loop. 55717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 55817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @return the requested time between frames, in milliseconds 55917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 56017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public static long getFrameDelay() { 56117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase return sFrameDelay; 56217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 56317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 56417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 56517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The amount of time, in milliseconds, between each frame of the animation. This is a 56617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * requested time that the animation will attempt to honor, but the actual delay between 56717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * frames may be different, depending on system load and capabilities. This is a static 56817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * function because the same delay will be applied to all animations, since they are all 56917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * run off of a single timing loop. 57017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 57117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param frameDelay the requested time between frames, in milliseconds 57217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 57317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public static void setFrameDelay(long frameDelay) { 57417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sFrameDelay = frameDelay; 57517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 57617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 57717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 578d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * The most recent value calculated by this <code>Animator</code> when there is just one 579d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * property being animated. This value is only sensible while the animation is running. The main 58017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * purpose for this read-only property is to retrieve the value from the <code>Animator</code> 58117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * during a call to {@link AnimatorUpdateListener#onAnimationUpdate(Animator)}, which 58217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * is called during each animation frame, immediately after the value is calculated. 58317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 58417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @return animatedValue The value most recently calculated by this <code>Animator</code> for 585d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * the single property being animated. If there are several properties being animated 586d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * (specified by several PropertyValuesHolder objects in the constructor), this function 587d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * returns the animated value for the first of those objects. 58817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 58917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public Object getAnimatedValue() { 590d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase return getAnimatedValue(mFirstPropertyName); 591d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase } 592d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase 593d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase /** 594d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * The most recent value calculated by this <code>Animator</code> for <code>propertyName</code>. 595d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * The main purpose for this read-only property is to retrieve the value from the 596d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * <code>Animator</code> during a call to 597d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * {@link AnimatorUpdateListener#onAnimationUpdate(Animator)}, which 598d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * is called during each animation frame, immediately after the value is calculated. 599d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * 600d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * @return animatedValue The value most recently calculated for the named property 601d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * by this <code>Animator</code>. 602d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase */ 603d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase public Object getAnimatedValue(String propertyName) { 604d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase return mValues.get(mFirstPropertyName).getAnimatedValue(); 60517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 60617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 60717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 60817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Sets how many times the animation should be repeated. If the repeat 60917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * count is 0, the animation is never repeated. If the repeat count is 61017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * greater than 0 or {@link #INFINITE}, the repeat mode will be taken 61117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * into account. The repeat count is 0 by default. 61217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 61317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param value the number of times the animation should be repeated 61417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 61517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void setRepeatCount(int value) { 61617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mRepeatCount = value; 61717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 61817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 61917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Defines how many times the animation should repeat. The default value 62017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * is 0. 62117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 62217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @return the number of times the animation should repeat, or {@link #INFINITE} 62317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 62417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public int getRepeatCount() { 62517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase return mRepeatCount; 62617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 62717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 62817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 62917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Defines what this animation should do when it reaches the end. This 63017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * setting is applied only when the repeat count is either greater than 63117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 0 or {@link #INFINITE}. Defaults to {@link #RESTART}. 63217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 63317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param value {@link #RESTART} or {@link #REVERSE} 63417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 63517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void setRepeatMode(int value) { 63617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mRepeatMode = value; 63717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 63817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 63917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 64017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Defines what this animation should do when it reaches the end. 64117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 64217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @return either one of {@link #REVERSE} or {@link #RESTART} 64317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 64417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public int getRepeatMode() { 64517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase return mRepeatMode; 64617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 64717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 64817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 64917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Adds a listener to the set of listeners that are sent update events through the life of 65017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * an animation. This method is called on all listeners for every frame of the animation, 65117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * after the values for the animation have been calculated. 65217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 65317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param listener the listener to be added to the current set of listeners for this animation. 65417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 65517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void addUpdateListener(AnimatorUpdateListener listener) { 65617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mUpdateListeners == null) { 65717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mUpdateListeners = new ArrayList<AnimatorUpdateListener>(); 65817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 65917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mUpdateListeners.add(listener); 66017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 66117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 66217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 66317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Removes a listener from the set listening to frame updates for this animation. 66417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 66517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param listener the listener to be removed from the current set of update listeners 66617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * for this animation. 66717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 66817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void removeUpdateListener(AnimatorUpdateListener listener) { 66917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mUpdateListeners == null) { 67017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase return; 67117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 67217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mUpdateListeners.remove(listener); 67317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mUpdateListeners.size() == 0) { 67417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mUpdateListeners = null; 67517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 67617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 67717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 67817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 67917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 68017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The time interpolator used in calculating the elapsed fraction of this animation. The 68117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * interpolator determines whether the animation runs with linear or non-linear motion, 68217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * such as acceleration and deceleration. The default value is 68317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * {@link android.view.animation.AccelerateDecelerateInterpolator} 68417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 68517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param value the interpolator to be used by this animation 68617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 68717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void setInterpolator(Interpolator value) { 68817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (value != null) { 68917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mInterpolator = value; 69017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 69117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 69217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 69317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 69417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The type evaluator to be used when calculating the animated values of this animation. 69517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The system will automatically assign a float, int, or double evaluator based on the type 69617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * of <code>startValue</code> and <code>endValue</code> in the constructor. But if these values 69717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * are not one of these primitive types, or if different evaluation is desired (such as is 69817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * necessary with int values that represent colors), a custom evaluator needs to be assigned. 69917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * For example, when running an animation on color values, the {@link RGBEvaluator} 70017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * should be used to get correct RGB color interpolation. 70117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 702d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * <p>If this Animator has only one set of values being animated between, this evaluator 703d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * will be used for that set. If there are several sets of values being animated, which is 704d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * the case if PropertyValuesHOlder objects were set on the Animator, then the evaluator 705d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * is assigned just to the first PropertyValuesHolder object.</p> 706d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * 70717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param value the evaluator to be used this animation 70817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 70917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void setEvaluator(TypeEvaluator value) { 710d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase if (value != null && mValues != null) { 711d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase mValues.get(mFirstPropertyName).setEvaluator(value); 71217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 71317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 71417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 715d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase /** 716d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * Start the animation playing. This version of start() takes a boolean flag that indicates 717d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * whether the animation should play in reverse. The flag is usually false, but may be set 718d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * to true if called from the reverse() method/ 719d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * 720d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase * @param playBackwards Whether the Animator should start playing in reverse. 721d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase */ 7225c7649857246333572eb332b505ad617365ef5faChet Haase private void start(boolean playBackwards) { 7235c7649857246333572eb332b505ad617365ef5faChet Haase mPlayingBackwards = playBackwards; 7245d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase mPlayingState = STOPPED; 72517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sPendingAnimations.add(this); 72617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (sAnimationHandler == null) { 72717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sAnimationHandler = new AnimationHandler(); 72817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 72917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // TODO: does this put too many messages on the queue if the handler 73017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // is already running? 73117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sAnimationHandler.sendEmptyMessage(ANIMATION_START); 73217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 73317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 7345c7649857246333572eb332b505ad617365ef5faChet Haase @Override 7355c7649857246333572eb332b505ad617365ef5faChet Haase public void start() { 7365c7649857246333572eb332b505ad617365ef5faChet Haase start(false); 7375c7649857246333572eb332b505ad617365ef5faChet Haase } 7385c7649857246333572eb332b505ad617365ef5faChet Haase 7395c7649857246333572eb332b505ad617365ef5faChet Haase @Override 74017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void cancel() { 74117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mListeners != null) { 74217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase ArrayList<AnimatableListener> tmpListeners = 74317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase (ArrayList<AnimatableListener>) mListeners.clone(); 74417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase for (AnimatableListener listener : tmpListeners) { 74517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase listener.onAnimationCancel(this); 74617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 74717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 74817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // Just set the CANCELED flag - this causes the animation to end the next time a frame 74917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // is processed. 75017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mPlayingState = CANCELED; 75117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 75217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 7535c7649857246333572eb332b505ad617365ef5faChet Haase @Override 75417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public void end() { 75517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // Just set the ENDED flag - this causes the animation to end the next time a frame 75617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // is processed. 75717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mPlayingState = ENDED; 75817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 75917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 76017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 7615c7649857246333572eb332b505ad617365ef5faChet Haase * Returns whether this Animator is currently running (having been started and not yet ended). 7625c7649857246333572eb332b505ad617365ef5faChet Haase * @return Wehther the Animator is running. 7635c7649857246333572eb332b505ad617365ef5faChet Haase */ 7645c7649857246333572eb332b505ad617365ef5faChet Haase public boolean isRunning() { 7655c7649857246333572eb332b505ad617365ef5faChet Haase return mPlayingState == RUNNING; 7665c7649857246333572eb332b505ad617365ef5faChet Haase } 7675c7649857246333572eb332b505ad617365ef5faChet Haase 7685c7649857246333572eb332b505ad617365ef5faChet Haase /** 7695c7649857246333572eb332b505ad617365ef5faChet Haase * Plays the Animator in reverse. If the animation is already running, 7705c7649857246333572eb332b505ad617365ef5faChet Haase * it will stop itself and play backwards from the point reached when reverse was called. 7715c7649857246333572eb332b505ad617365ef5faChet Haase * If the animation is not currently running, then it will start from the end and 7725c7649857246333572eb332b505ad617365ef5faChet Haase * play backwards. This behavior is only set for the current animation; future playing 7735c7649857246333572eb332b505ad617365ef5faChet Haase * of the animation will use the default behavior of playing forward. 7745c7649857246333572eb332b505ad617365ef5faChet Haase */ 7755c7649857246333572eb332b505ad617365ef5faChet Haase public void reverse() { 7765c7649857246333572eb332b505ad617365ef5faChet Haase mPlayingBackwards = !mPlayingBackwards; 7775c7649857246333572eb332b505ad617365ef5faChet Haase if (mPlayingState == RUNNING) { 7785c7649857246333572eb332b505ad617365ef5faChet Haase long currentTime = AnimationUtils.currentAnimationTimeMillis(); 7795c7649857246333572eb332b505ad617365ef5faChet Haase long currentPlayTime = currentTime - mStartTime; 7805c7649857246333572eb332b505ad617365ef5faChet Haase long timeLeft = mDuration - currentPlayTime; 7815c7649857246333572eb332b505ad617365ef5faChet Haase mStartTime = currentTime - timeLeft; 7825c7649857246333572eb332b505ad617365ef5faChet Haase } else { 7835c7649857246333572eb332b505ad617365ef5faChet Haase start(true); 7845c7649857246333572eb332b505ad617365ef5faChet Haase } 7855c7649857246333572eb332b505ad617365ef5faChet Haase } 7865c7649857246333572eb332b505ad617365ef5faChet Haase 7875c7649857246333572eb332b505ad617365ef5faChet Haase /** 78817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Called internally to end an animation by removing it from the animations list. Must be 78917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * called on the UI thread. 79017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 79117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private void endAnimation() { 79217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sAnimations.remove(this); 79317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mListeners != null) { 79417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase ArrayList<AnimatableListener> tmpListeners = 79517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase (ArrayList<AnimatableListener>) mListeners.clone(); 79617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase for (AnimatableListener listener : tmpListeners) { 79717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase listener.onAnimationEnd(this); 79817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 79917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 80017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mPlayingState = STOPPED; 80117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 80217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 80317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 80417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Called internally to start an animation by adding it to the active animations list. Must be 80517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * called on the UI thread. 80617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 80717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private void startAnimation() { 80817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase initAnimation(); 80917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase sAnimations.add(this); 81017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mListeners != null) { 81117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase ArrayList<AnimatableListener> tmpListeners = 81217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase (ArrayList<AnimatableListener>) mListeners.clone(); 81317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase for (AnimatableListener listener : tmpListeners) { 81417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase listener.onAnimationStart(this); 81517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 81617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 81717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 81817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 81917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 82017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Internal function called to process an animation frame on an animation that is currently 82117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * sleeping through its <code>startDelay</code> phase. The return value indicates whether it 82217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * should be woken up and put on the active animations queue. 82317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 82417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param currentTime The current animation time, used to calculate whether the animation 82517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * has exceeded its <code>startDelay</code> and should be started. 82617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @return True if the animation's <code>startDelay</code> has been exceeded and the animation 82717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * should be added to the set of active animations. 82817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 82917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private boolean delayedAnimationFrame(long currentTime) { 83017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (!mStartedDelay) { 83117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mStartedDelay = true; 83217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mDelayStartTime = currentTime; 83317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } else { 83417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase long deltaTime = currentTime - mDelayStartTime; 83517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (deltaTime > mStartDelay) { 83617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // startDelay ended - start the anim and record the 83717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // mStartTime appropriately 83817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mStartTime = currentTime - (deltaTime - mStartDelay); 83917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mPlayingState = RUNNING; 84017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase return true; 84117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 84217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 84317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase return false; 84417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 84517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 84617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 84717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * This internal function processes a single animation frame for a given animation. The 84817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * currentTime parameter is the timing pulse sent by the handler, used to calculate the 84917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * elapsed duration, and therefore 85017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * the elapsed fraction, of the animation. The return value indicates whether the animation 85117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * should be ended (which happens when the elapsed time of the animation exceeds the 85217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * animation's duration, including the repeatCount). 85317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 85417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param currentTime The current time, as tracked by the static timing handler 85517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @return true if the animation's duration, including any repetitions due to 85617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <code>repeatCount</code> has been exceeded and the animation should be ended. 85717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 85817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase private boolean animationFrame(long currentTime) { 85917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase boolean done = false; 86017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 86117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mPlayingState == STOPPED) { 86217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mPlayingState = RUNNING; 8635d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase if (mSeekTime < 0) { 8645d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase mStartTime = currentTime; 8655d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase } else { 8665d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase mStartTime = currentTime - mSeekTime; 8675d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase // Now that we're playing, reset the seek time 8685d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase mSeekTime = -1; 8695d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase } 87017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 87117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase switch (mPlayingState) { 87217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase case RUNNING: 8735d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase case SEEKED: 87417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase float fraction = (float)(currentTime - mStartTime) / mDuration; 87517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (fraction >= 1f) { 87617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mCurrentIteration < mRepeatCount || mRepeatCount == INFINITE) { 87717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // Time to repeat 87817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mListeners != null) { 87917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase for (AnimatableListener listener : mListeners) { 88017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase listener.onAnimationRepeat(this); 88117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 88217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 88317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase ++mCurrentIteration; 88417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mRepeatMode == REVERSE) { 88517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mPlayingBackwards = mPlayingBackwards ? false : true; 88617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 88717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // TODO: doesn't account for fraction going Wayyyyy over 1, like 2+ 88817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase fraction = fraction - 1f; 88917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mStartTime += mDuration; 89017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } else { 89117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase done = true; 89217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase fraction = Math.min(fraction, 1.0f); 89317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 89417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 89517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mPlayingBackwards) { 89617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase fraction = 1f - fraction; 89717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 89817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase animateValue(fraction); 89917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase break; 90017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase case ENDED: 90117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // The final value set on the target varies, depending on whether the animation 90217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // was supposed to repeat an odd number of times 90317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mRepeatCount > 0 && (mRepeatCount & 0x01) == 1) { 90417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase animateValue(0f); 90517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } else { 90617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase animateValue(1f); 90717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 90817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase // Fall through to set done flag 90917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase case CANCELED: 91017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase done = true; 91117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase break; 91217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 91317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 91417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase return done; 91517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 91617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 91717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 91817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * This method is called with the elapsed fraction of the animation during every 91917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * animation frame. This function turns the elapsed fraction into an interpolated fraction 92017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * and then into an animated value (from the evaluator. The function is called mostly during 92117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * animation updates, but it is also called when the <code>end()</code> 92217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * function is called, to set the final value on the property. 92317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 92417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <p>Overrides of this method must call the superclass to perform the calculation 92517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * of the animated value.</p> 92617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 92717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param fraction The elapsed fraction of the animation. 92817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 92917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase void animateValue(float fraction) { 93017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase fraction = mInterpolator.getInterpolation(fraction); 931d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase for (PropertyValuesHolder valuesHolder : mValues.values()) { 932d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase valuesHolder.calculateValue(fraction); 9333dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase } 93417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase if (mUpdateListeners != null) { 93517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase int numListeners = mUpdateListeners.size(); 93617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase for (int i = 0; i < numListeners; ++i) { 93717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase mUpdateListeners.get(i).onAnimationUpdate(this); 93817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 93917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 94017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 94117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 94217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 94317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Implementors of this interface can add themselves as update listeners 94417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * to an <code>Animator</code> instance to receive callbacks on every animation 94517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * frame, after the current frame's values have been calculated for that 94617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <code>Animator</code>. 94717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 94817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase public static interface AnimatorUpdateListener { 94917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase /** 95017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * <p>Notifies the occurrence of another frame of the animation.</p> 95117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * 95217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * @param animation The animation which was repeated. 95317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */ 95417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase void onAnimationUpdate(Animator animation); 95517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase 95617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase } 95717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase}