Animator.java revision f54a8d7c479485174941c38f151ea7083c658da3
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;
2917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
3017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase/**
3117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * This class provides a simple timing engine for running animations
3217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * which calculate animated values and set them on target objects.
3317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase *
34010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * <p>There is a single timing pulse that all animations use. It runs in a
35010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * custom handler to ensure that property changes happen on the UI thread.</p>
36010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase *
37010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * <p>By default, Animator uses non-linear time interpolation, via the
38010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * {@link AccelerateDecelerateInterpolator} class, which accelerates into and decelerates
39010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * out of an animation. This behavior can be changed by calling
40010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * {@link Animator#setInterpolator(Interpolator)}.</p>
4117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */
4217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haasepublic class Animator extends Animatable {
4317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
4417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
4517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Internal constants
4617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
4717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
4817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /*
4917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The default amount of time in ms between animation frames
5017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
5117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final long DEFAULT_FRAME_DELAY = 30;
5217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
5317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
5417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Messages sent to timing handler: START is sent when an animation first begins, FRAME is sent
5517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * by the handler to itself to process the next animation frame
5617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
5717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final int ANIMATION_START = 0;
5817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final int ANIMATION_FRAME = 1;
5917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
6017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
6117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Values used with internal variable mPlayingState to indicate the current state of an
6217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * animation.
6317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
6417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final int STOPPED    = 0; // Not yet playing
6517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final int RUNNING    = 1; // Playing normally
6617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final int CANCELED   = 2; // cancel() called - need to end it
6717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final int ENDED      = 3; // end() called - need to end it
6817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
6917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
70f54a8d7c479485174941c38f151ea7083c658da3Chet Haase     * Enum values used in XML attributes to indicate the value for mValueType
71f54a8d7c479485174941c38f151ea7083c658da3Chet Haase     */
72f54a8d7c479485174941c38f151ea7083c658da3Chet Haase    private static final int VALUE_TYPE_FLOAT       = 0;
73f54a8d7c479485174941c38f151ea7083c658da3Chet Haase    private static final int VALUE_TYPE_INT         = 1;
74f54a8d7c479485174941c38f151ea7083c658da3Chet Haase    private static final int VALUE_TYPE_DOUBLE      = 2;
75f54a8d7c479485174941c38f151ea7083c658da3Chet Haase    private static final int VALUE_TYPE_COLOR       = 3;
76f54a8d7c479485174941c38f151ea7083c658da3Chet Haase    private static final int VALUE_TYPE_CUSTOM      = 4;
77f54a8d7c479485174941c38f151ea7083c658da3Chet Haase
78f54a8d7c479485174941c38f151ea7083c658da3Chet Haase    /**
7917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Internal variables
8017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
8117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
8217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
8317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // The first time that the animation's animateFrame() method is called. This time is used to
8417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // determine elapsed time (and therefore the elapsed fraction) in subsequent calls
8517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // to animateFrame()
8617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private long mStartTime;
8717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
8817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // The static sAnimationHandler processes the internal timing loop on which all animations
8917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // are based
9017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static AnimationHandler sAnimationHandler;
9117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
9217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // The static list of all active animations
9317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final ArrayList<Animator> sAnimations = new ArrayList<Animator>();
9417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
9517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // The set of animations to be started on the next animation frame
9617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final ArrayList<Animator> sPendingAnimations = new ArrayList<Animator>();
9717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
9817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // The time interpolator to be used if none is set on the animation
9917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final Interpolator sDefaultInterpolator = new AccelerateDecelerateInterpolator();
10017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
10117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // type evaluators for the three primitive types handled by this implementation
10217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final TypeEvaluator sIntEvaluator = new IntEvaluator();
10317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final TypeEvaluator sFloatEvaluator = new FloatEvaluator();
10417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final TypeEvaluator sDoubleEvaluator = new DoubleEvaluator();
10517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
10617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
10717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Used to indicate whether the animation is currently playing in reverse. This causes the
10817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * elapsed fraction to be inverted to calculate the appropriate values.
10917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
11017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private boolean mPlayingBackwards = false;
11117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
11217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
11317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * This variable tracks the current iteration that is playing. When mCurrentIteration exceeds the
11417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * repeatCount (if repeatCount!=INFINITE), the animation ends
11517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
11617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private int mCurrentIteration = 0;
11717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
11817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
11917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Tracks whether a startDelay'd animation has begun playing through the startDelay.
12017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
12117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private boolean mStartedDelay = false;
12217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
12317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
12417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Tracks the time at which the animation began playing through its startDelay. This is
12517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * different from the mStartTime variable, which is used to track when the animation became
12617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * active (which is when the startDelay expired and the animation was added to the active
12717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * animations list).
12817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
12917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private long mDelayStartTime;
13017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
13117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
13217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Flag that represents the current state of the animation. Used to figure out when to start
13317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * an animation (if state == STOPPED). Also used to end an animation that
13417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * has been cancel()'d or end()'d since the last animation frame. Possible values are
13517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * STOPPED, RUNNING, ENDED, CANCELED.
13617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
13717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private int mPlayingState = STOPPED;
13817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
13917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
14017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Internal collections used to avoid set collisions as animations start and end while being
14117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * processed.
14217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
14317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final ArrayList<Animator> sEndingAnims = new ArrayList<Animator>();
14417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final ArrayList<Animator> sDelayedAnims = new ArrayList<Animator>();
14517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final ArrayList<Animator> sReadyAnims = new ArrayList<Animator>();
14617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
14717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    //
14817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // Backing variables
14917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    //
15017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
15117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // How long the animation should last in ms
15217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private long mDuration;
15317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
15417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // The value that the animation should start from, set in the constructor
15517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private Object mValueFrom;
15617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
15717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // The value that the animation should animate to, set in the constructor
15817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private Object mValueTo;
15917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
16017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // The amount of time in ms to delay starting the animation after start() is called
16117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private long mStartDelay = 0;
16217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
16317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // The number of milliseconds between animation frames
16417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static long sFrameDelay = DEFAULT_FRAME_DELAY;
16517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
16617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // The number of times the animation will repeat. The default is 0, which means the animation
16717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // will play only once
16817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private int mRepeatCount = 0;
16917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
17017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
17117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The type of repetition that will occur when repeatMode is nonzero. RESTART means the
17217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * animation will start from the beginning on every new cycle. REVERSE means the animation
17317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * will reverse directions on each iteration.
17417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
17517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private int mRepeatMode = RESTART;
17617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
17717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
17817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The time interpolator to be used. The elapsed fraction of the animation will be passed
17917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * through this interpolator to calculate the interpolated fraction, which is then used to
18017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * calculate the animated values.
18117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
18217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private Interpolator mInterpolator = sDefaultInterpolator;
18317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
18417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
18517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The type evaluator used to calculate the animated values. This evaluator is determined
18617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * automatically based on the type of the start/end objects passed into the constructor,
18717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * but the system only knows about the primitive types int, double, and float. Any other
18817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * type will need to set the evaluator to a custom evaluator for that type.
18917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
19017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private TypeEvaluator mEvaluator;
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    /**
19817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The current value calculated by the animation. The value is calculated in animateFraction(),
19917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * prior to calling the setter (if set) and sending out the onAnimationUpdate() callback
20017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * to the update listeners.
20117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
20217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private Object mAnimatedValue = null;
20317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
20417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
2053dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * The set of keyframes (time/value pairs) that define this animation.
2063dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     */
2073dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    private KeyframeSet mKeyframeSet = null;
2083dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase
2093dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    /**
21017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The type of the values, as determined by the valueFrom/valueTo properties.
21117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
21217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    Class mValueType;
21317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
21417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
21517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Public constants
21617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
21717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
21817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
21917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * When the animation reaches the end and <code>repeatCount</code> is INFINITE
22017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * or a positive value, the animation restarts from the beginning.
22117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
22217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public static final int RESTART = 1;
22317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
22417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * When the animation reaches the end and <code>repeatCount</code> is INFINITE
22517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * or a positive value, the animation reverses direction on every iteration.
22617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
22717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public static final int REVERSE = 2;
22817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
22917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * This value used used with the {@link #setRepeatCount(int)} property to repeat
23017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * the animation indefinitely.
23117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
23217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public static final int INFINITE = -1;
23317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
234f54a8d7c479485174941c38f151ea7083c658da3Chet Haase    /**
235f54a8d7c479485174941c38f151ea7083c658da3Chet Haase     * Creates a new animation whose parameters come from the specified context and
236f54a8d7c479485174941c38f151ea7083c658da3Chet Haase     * attributes set.
237f54a8d7c479485174941c38f151ea7083c658da3Chet Haase     *
238f54a8d7c479485174941c38f151ea7083c658da3Chet Haase     * @param context the application environment
239f54a8d7c479485174941c38f151ea7083c658da3Chet Haase     * @param attrs the set of attributes holding the animation parameters
240f54a8d7c479485174941c38f151ea7083c658da3Chet Haase     */
241f54a8d7c479485174941c38f151ea7083c658da3Chet Haase    public Animator(Context context, AttributeSet attrs) {
242f54a8d7c479485174941c38f151ea7083c658da3Chet Haase        TypedArray a =
243f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Animator);
244f54a8d7c479485174941c38f151ea7083c658da3Chet Haase
245f54a8d7c479485174941c38f151ea7083c658da3Chet Haase        mDuration = (long) a.getInt(com.android.internal.R.styleable.Animator_duration, 0);
246f54a8d7c479485174941c38f151ea7083c658da3Chet Haase
247f54a8d7c479485174941c38f151ea7083c658da3Chet Haase        mStartDelay = (long) a.getInt(com.android.internal.R.styleable.Animator_startOffset, 0);
248f54a8d7c479485174941c38f151ea7083c658da3Chet Haase
249f54a8d7c479485174941c38f151ea7083c658da3Chet Haase        final int resID =
250f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                a.getResourceId(com.android.internal.R.styleable.Animator_interpolator, 0);
251f54a8d7c479485174941c38f151ea7083c658da3Chet Haase        if (resID > 0) {
252f54a8d7c479485174941c38f151ea7083c658da3Chet Haase            setInterpolator(AnimationUtils.loadInterpolator(context, resID));
253f54a8d7c479485174941c38f151ea7083c658da3Chet Haase        }
254f54a8d7c479485174941c38f151ea7083c658da3Chet Haase        int valueType = a.getInt(com.android.internal.R.styleable.Animator_valueType,
255f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                VALUE_TYPE_FLOAT);
256f54a8d7c479485174941c38f151ea7083c658da3Chet Haase
257f54a8d7c479485174941c38f151ea7083c658da3Chet Haase        switch (valueType) {
258f54a8d7c479485174941c38f151ea7083c658da3Chet Haase            case VALUE_TYPE_FLOAT:
259f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                mValueFrom = a.getFloat(com.android.internal.R.styleable.Animator_valueFrom, 0f);
260f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                mValueTo = a.getFloat(com.android.internal.R.styleable.Animator_valueTo, 0f);
261f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                mValueType = float.class;
262f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                break;
263f54a8d7c479485174941c38f151ea7083c658da3Chet Haase            case VALUE_TYPE_INT:
264f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                mValueFrom = a.getInt(com.android.internal.R.styleable.Animator_valueFrom, 0);
265f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                mValueTo = a.getInt(com.android.internal.R.styleable.Animator_valueTo, 0);
266f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                mValueType = int.class;
267f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                break;
268f54a8d7c479485174941c38f151ea7083c658da3Chet Haase            case VALUE_TYPE_DOUBLE:
269f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                mValueFrom = (double)
270f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                        a.getFloat(com.android.internal.R.styleable.Animator_valueFrom, 0f);
271f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                mValueTo = (double)
272f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                        a.getFloat(com.android.internal.R.styleable.Animator_valueTo, 0f);
273f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                mValueType = double.class;
274f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                break;
275f54a8d7c479485174941c38f151ea7083c658da3Chet Haase            case VALUE_TYPE_COLOR:
276f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                mValueFrom = a.getInt(com.android.internal.R.styleable.Animator_valueFrom, 0);
277f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                mValueTo = a.getInt(com.android.internal.R.styleable.Animator_valueTo, 0);
278f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                mEvaluator = new RGBEvaluator();
279f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                mValueType = int.class;
280f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                break;
281f54a8d7c479485174941c38f151ea7083c658da3Chet Haase            case VALUE_TYPE_CUSTOM:
282f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                // TODO: How to get an 'Object' value?
283f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                mValueFrom = a.getFloat(com.android.internal.R.styleable.Animator_valueFrom, 0f);
284f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                mValueTo = a.getFloat(com.android.internal.R.styleable.Animator_valueTo, 0f);
285f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                mValueType = Object.class;
286f54a8d7c479485174941c38f151ea7083c658da3Chet Haase                break;
287f54a8d7c479485174941c38f151ea7083c658da3Chet Haase        }
288f54a8d7c479485174941c38f151ea7083c658da3Chet Haase
289f54a8d7c479485174941c38f151ea7083c658da3Chet Haase        mRepeatCount = a.getInt(com.android.internal.R.styleable.Animator_repeatCount, mRepeatCount);
290f54a8d7c479485174941c38f151ea7083c658da3Chet Haase        mRepeatMode = a.getInt(com.android.internal.R.styleable.Animator_repeatMode, RESTART);
291f54a8d7c479485174941c38f151ea7083c658da3Chet Haase
292f54a8d7c479485174941c38f151ea7083c658da3Chet Haase        a.recycle();
293f54a8d7c479485174941c38f151ea7083c658da3Chet Haase    }
294f54a8d7c479485174941c38f151ea7083c658da3Chet Haase
29517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private Animator(long duration, Object valueFrom, Object valueTo, Class valueType) {
29617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        mDuration = duration;
29717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        mValueFrom = valueFrom;
29817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        mValueTo= valueTo;
29917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        this.mValueType = valueType;
30017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
30117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
30217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
3033dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * This constructor takes a set of {@link Keyframe} objects that define the values
3043dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * for the animation, along with the times at which those values will hold true during
3053dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * the animation.
3063dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     *
3073dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * @param duration The length of the animation, in milliseconds.
3083dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * @param keyframes The set of keyframes that define the time/value pairs for the animation.
3093dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     */
3103dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    public Animator(long duration, Keyframe...keyframes) {
3113dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase        mDuration = duration;
3123dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase        mKeyframeSet = new KeyframeSet(keyframes);
3133dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase        mValueType = keyframes[0].getType();
3143dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    }
3153dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase
3163dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    /**
31717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * A constructor that takes <code>float</code> values.
31817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
31917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param duration The length of the animation, in milliseconds.
32017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param valueFrom The initial value of the property when the animation begins.
32117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param valueTo The value to which the property will animate.
32217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
32317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public Animator(long duration, float valueFrom, float valueTo) {
32417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        this(duration, valueFrom, valueTo, float.class);
32517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
32617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
32717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
32817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * A constructor that takes <code>int</code> values.
32917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
33017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param duration The length of the animation, in milliseconds.
33117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param valueFrom The initial value of the property when the animation begins.
33217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param valueTo The value to which the property will animate.
33317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
33417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public Animator(long duration, int valueFrom, int valueTo) {
33517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        this(duration, valueFrom, valueTo, int.class);
33617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
33717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
33817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
33917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * A constructor that takes <code>double</code> values.
34017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
34117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param duration The length of the animation, in milliseconds.
34217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param valueFrom The initial value of the property when the animation begins.
34317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param valueTo The value to which the property will animate.
34417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
34517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public Animator(long duration, double valueFrom, double valueTo) {
34617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        this(duration, valueFrom, valueTo, double.class);
34717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
34817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
34917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
35017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * A constructor that takes <code>Object</code> values.
35117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
35217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param duration The length of the animation, in milliseconds.
35317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param valueFrom The initial value of the property when the animation begins.
35417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param valueTo The value to which the property will animate.
35517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
35617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public Animator(long duration, Object valueFrom, Object valueTo) {
35717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        this(duration, valueFrom, valueTo,
35817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                (valueFrom != null) ? valueFrom.getClass() : valueTo.getClass());
35917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
36017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
36117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
362fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * Internal constructor that takes a single <code>float</code> value.
363fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * This constructor is called by PropertyAnimator.
364fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     *
365fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * @param duration The length of the animation, in milliseconds.
366fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * @param valueFrom The initial value of the property when the animation begins.
367fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * @param valueTo The value to which the property will animate.
368fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     */
369fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    Animator(long duration, float valueTo) {
370fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        this(duration, null, valueTo, float.class);
371fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    }
372fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase
373fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    /**
374fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * Internal constructor that takes a single <code>int</code> value.
375fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * This constructor is called by PropertyAnimator.
376fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     *
377fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * @param duration The length of the animation, in milliseconds.
378fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * @param valueFrom The initial value of the property when the animation begins.
379fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * @param valueTo The value to which the property will animate.
380fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     */
381fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    Animator(long duration, int valueTo) {
382fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        this(duration, null, valueTo, int.class);
383fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    }
384fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase
385fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    /**
386fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * Internal constructor that takes a single <code>double</code> value.
387fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * This constructor is called by PropertyAnimator.
388fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     *
389fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * @param duration The length of the animation, in milliseconds.
390fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * @param valueFrom The initial value of the property when the animation begins.
391fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * @param valueTo The value to which the property will animate.
392fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     */
393fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    Animator(long duration, double valueTo) {
394fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        this(duration, null, valueTo, double.class);
395fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    }
396fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase
397fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    /**
398fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * This function is called immediately before processing the first animation
399fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * frame of an animation. If there is a nonzero <code>startDelay</code>, the
400fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * function is called after that delay ends.
401fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * It takes care of the final initialization steps for the
402fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * animation.
403fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     *
404fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     *  <p>Overrides of this method should call the superclass method to ensure
405fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     *  that internal mechanisms for the animation are set up correctly.</p>
406fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     */
407fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    void initAnimation() {
408fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        if (mEvaluator == null) {
409fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase            mEvaluator = (mValueType == int.class) ? sIntEvaluator :
410fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase                (mValueType == double.class) ? sDoubleEvaluator : sFloatEvaluator;
411fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        }
412fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        mPlayingBackwards = false;
413fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        mCurrentIteration = 0;
414fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    }
415fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase
416fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    /**
41717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * This custom, static handler handles the timing pulse that is shared by
41817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * all active animations. This approach ensures that the setting of animation
41917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * values will happen on the UI thread and that all animations will share
42017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * the same times for calculating their values, which makes synchronizing
42117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * animations possible.
42217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
42317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
42417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static class AnimationHandler extends Handler {
42517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
42617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * There are only two messages that we care about: ANIMATION_START and
42717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * ANIMATION_FRAME. The START message is sent when an animation's start()
42817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * method is called. It cannot start synchronously when start() is called
42917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * because the call may be on the wrong thread, and it would also not be
43017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * synchronized with other animations because it would not start on a common
43117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * timing pulse. So each animation sends a START message to the handler, which
43217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * causes the handler to place the animation on the active animations queue and
43317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * start processing frames for that animation.
43417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * The FRAME message is the one that is sent over and over while there are any
43517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * active animations to process.
43617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
43717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        @Override
43817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public void handleMessage(Message msg) {
43917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            boolean callAgain = true;
44017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            switch (msg.what) {
44117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                // TODO: should we avoid sending frame message when starting if we
44217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                // were already running?
44317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                case ANIMATION_START:
44417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    if (sAnimations.size() > 0 || sDelayedAnims.size() > 0) {
44517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        callAgain = false;
44617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
44717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // pendingAnims holds any animations that have requested to be started
44817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // We're going to clear sPendingAnimations, but starting animation may
44917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // cause more to be added to the pending list (for example, if one animation
45017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // starting triggers another starting). So we loop until sPendingAnimations
45117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // is empty.
45217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    while (sPendingAnimations.size() > 0) {
45317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        ArrayList<Animator> pendingCopy =
45417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                                (ArrayList<Animator>) sPendingAnimations.clone();
45517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        sPendingAnimations.clear();
45617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        int count = pendingCopy.size();
45717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        for (int i = 0; i < count; ++i) {
45817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            Animator anim = pendingCopy.get(i);
45917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            // If the animation has a startDelay, place it on the delayed list
46017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            if (anim.mStartDelay == 0) {
46117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                                anim.startAnimation();
46217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            } else {
46317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                                sDelayedAnims.add(anim);
46417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            }
46517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        }
46617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
46717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // fall through to process first frame of new animations
46817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                case ANIMATION_FRAME:
46917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // currentTime holds the common time for all animations processed
47017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // during this frame
47117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    long currentTime = AnimationUtils.currentAnimationTimeMillis();
47217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
47317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // First, process animations currently sitting on the delayed queue, adding
47417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // them to the active animations if they are ready
47517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    int numDelayedAnims = sDelayedAnims.size();
47617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    for (int i = 0; i < numDelayedAnims; ++i) {
47717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        Animator anim = sDelayedAnims.get(i);
47817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        if (anim.delayedAnimationFrame(currentTime)) {
47917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            sReadyAnims.add(anim);
48017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        }
48117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
48217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    int numReadyAnims = sReadyAnims.size();
48317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    if (numReadyAnims > 0) {
48417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        for (int i = 0; i < numReadyAnims; ++i) {
48517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            Animator anim = sReadyAnims.get(i);
48617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            anim.startAnimation();
48717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            sDelayedAnims.remove(anim);
48817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        }
48917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        sReadyAnims.clear();
49017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
49117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
49217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // Now process all active animations. The return value from animationFrame()
49317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // tells the handler whether it should now be ended
49417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    int numAnims = sAnimations.size();
49517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    for (int i = 0; i < numAnims; ++i) {
49617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        Animator anim = sAnimations.get(i);
49717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        if (anim.animationFrame(currentTime)) {
49817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            sEndingAnims.add(anim);
49917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        }
50017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
50117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    if (sEndingAnims.size() > 0) {
50217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        for (int i = 0; i < sEndingAnims.size(); ++i) {
50317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            sEndingAnims.get(i).endAnimation();
50417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        }
50517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        sEndingAnims.clear();
50617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
50717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
50817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // If there are still active or delayed animations, call the handler again
50917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // after the frameDelay
51017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    if (callAgain && (!sAnimations.isEmpty() || !sDelayedAnims.isEmpty())) {
51117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        sendEmptyMessageDelayed(ANIMATION_FRAME, sFrameDelay);
51217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
51317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    break;
51417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
51517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
51617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
51717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
51817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
51917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The amount of time, in milliseconds, to delay starting the animation after
52017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * {@link #start()} is called.
52117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
52217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @return the number of milliseconds to delay running the animation
52317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
52417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public long getStartDelay() {
52517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        return mStartDelay;
52617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
52717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
52817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
52917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The amount of time, in milliseconds, to delay starting the animation after
53017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * {@link #start()} is called.
53117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
53217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param startDelay The amount of the delay, in milliseconds
53317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
53417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void setStartDelay(long startDelay) {
53517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        this.mStartDelay = startDelay;
53617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
53717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
53817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
53917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The amount of time, in milliseconds, between each frame of the animation. This is a
54017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * requested time that the animation will attempt to honor, but the actual delay between
54117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * frames may be different, depending on system load and capabilities. This is a static
54217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * function because the same delay will be applied to all animations, since they are all
54317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * run off of a single timing loop.
54417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
54517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @return the requested time between frames, in milliseconds
54617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
54717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public static long getFrameDelay() {
54817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        return sFrameDelay;
54917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
55017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
55117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
5523dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * Gets the set of keyframes that define this animation.
5533dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     *
5543dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * @return KeyframeSet The set of keyframes for this animation.
5553dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     */
5563dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    KeyframeSet getKeyframes() {
5573dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase        return mKeyframeSet;
5583dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    }
5593dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase
5603dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    /**
56117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Gets the value that this animation will start from.
56217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
56317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @return Object The starting value for the animation.
56417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
56517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public Object getValueFrom() {
566fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        if (mKeyframeSet != null) {
567fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase            return mKeyframeSet.mKeyframes.get(0).getValue();
568fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        }
56917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        return mValueFrom;
57017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
57117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
57217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
57317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Sets the value that this animation will start from.
57417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
57517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void setValueFrom(Object valueFrom) {
576fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        if (mKeyframeSet != null) {
577fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase            Keyframe kf = mKeyframeSet.mKeyframes.get(0);
578fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase            kf.setValue(valueFrom);
579fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        } else {
580fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase            mValueFrom = valueFrom;
581fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        }
58217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
58317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
58417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
58517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Gets the value that this animation will animate to.
58617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
58717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @return Object The ending value for the animation.
58817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
58917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public Object getValueTo() {
590fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        if (mKeyframeSet != null) {
591fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase            int numKeyframes = mKeyframeSet.mKeyframes.size();
592fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase            return mKeyframeSet.mKeyframes.get(numKeyframes - 1).getValue();
593fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        }
59417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        return mValueTo;
59517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
59617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
59717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
59817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Sets the value that this animation will animate to.
59917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
60017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @return Object The ending value for the animation.
60117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
60217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void setValueTo(Object valueTo) {
603fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        if (mKeyframeSet != null) {
604fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase            int numKeyframes = mKeyframeSet.mKeyframes.size();
605fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase            Keyframe kf = mKeyframeSet.mKeyframes.get(numKeyframes - 1);
606fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase            kf.setValue(valueTo);
607fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        } else {
608fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase            mValueTo = valueTo;
609fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        }
61017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
61117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
61217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
61317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The amount of time, in milliseconds, between each frame of the animation. This is a
61417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * requested time that the animation will attempt to honor, but the actual delay between
61517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * frames may be different, depending on system load and capabilities. This is a static
61617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * function because the same delay will be applied to all animations, since they are all
61717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * run off of a single timing loop.
61817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
61917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param frameDelay the requested time between frames, in milliseconds
62017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
62117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public static void setFrameDelay(long frameDelay) {
62217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        sFrameDelay = frameDelay;
62317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
62417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
62517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
62617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The most recent value calculated by this <code>Animator</code> for the property
62717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * being animated. This value is only sensible while the animation is running. The main
62817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * purpose for this read-only property is to retrieve the value from the <code>Animator</code>
62917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * during a call to {@link AnimatorUpdateListener#onAnimationUpdate(Animator)}, which
63017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * is called during each animation frame, immediately after the value is calculated.
63117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
63217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @return animatedValue The value most recently calculated by this <code>Animator</code> for
63317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * the property specified in the constructor.
63417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
63517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public Object getAnimatedValue() {
63617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        return mAnimatedValue;
63717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
63817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
63917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
64017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Sets how many times the animation should be repeated. If the repeat
64117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * count is 0, the animation is never repeated. If the repeat count is
64217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * greater than 0 or {@link #INFINITE}, the repeat mode will be taken
64317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * into account. The repeat count is 0 by default.
64417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
64517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param value the number of times the animation should be repeated
64617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
64717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void setRepeatCount(int value) {
64817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        mRepeatCount = value;
64917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
65017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
65117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Defines how many times the animation should repeat. The default value
65217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * is 0.
65317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
65417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @return the number of times the animation should repeat, or {@link #INFINITE}
65517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
65617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public int getRepeatCount() {
65717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        return mRepeatCount;
65817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
65917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
66017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
66117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Defines what this animation should do when it reaches the end. This
66217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * setting is applied only when the repeat count is either greater than
66317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * 0 or {@link #INFINITE}. Defaults to {@link #RESTART}.
66417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
66517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param value {@link #RESTART} or {@link #REVERSE}
66617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
66717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void setRepeatMode(int value) {
66817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        mRepeatMode = value;
66917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
67017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
67117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
67217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Defines what this animation should do when it reaches the end.
67317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
67417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @return either one of {@link #REVERSE} or {@link #RESTART}
67517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
67617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public int getRepeatMode() {
67717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        return mRepeatMode;
67817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
67917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
68017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
68117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Adds a listener to the set of listeners that are sent update events through the life of
68217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * an animation. This method is called on all listeners for every frame of the animation,
68317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * after the values for the animation have been calculated.
68417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
68517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param listener the listener to be added to the current set of listeners for this animation.
68617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
68717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void addUpdateListener(AnimatorUpdateListener listener) {
68817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mUpdateListeners == null) {
68917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mUpdateListeners = new ArrayList<AnimatorUpdateListener>();
69017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
69117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        mUpdateListeners.add(listener);
69217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
69317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
69417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
69517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Removes a listener from the set listening to frame updates for this animation.
69617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
69717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param listener the listener to be removed from the current set of update listeners
69817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * for this animation.
69917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
70017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void removeUpdateListener(AnimatorUpdateListener listener) {
70117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mUpdateListeners == null) {
70217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            return;
70317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
70417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        mUpdateListeners.remove(listener);
70517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mUpdateListeners.size() == 0) {
70617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mUpdateListeners = null;
70717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
70817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
70917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
71017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
71117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
71217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The time interpolator used in calculating the elapsed fraction of this animation. The
71317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * interpolator determines whether the animation runs with linear or non-linear motion,
71417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * such as acceleration and deceleration. The default value is
71517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * {@link android.view.animation.AccelerateDecelerateInterpolator}
71617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
71717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param value the interpolator to be used by this animation
71817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
71917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void setInterpolator(Interpolator value) {
72017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (value != null) {
72117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mInterpolator = value;
72217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
72317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
72417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
72517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
72617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The type evaluator to be used when calculating the animated values of this animation.
72717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The system will automatically assign a float, int, or double evaluator based on the type
72817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * of <code>startValue</code> and <code>endValue</code> in the constructor. But if these values
72917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * are not one of these primitive types, or if different evaluation is desired (such as is
73017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * necessary with int values that represent colors), a custom evaluator needs to be assigned.
73117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * For example, when running an animation on color values, the {@link RGBEvaluator}
73217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * should be used to get correct RGB color interpolation.
73317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
73417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param value the evaluator to be used this animation
73517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
73617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void setEvaluator(TypeEvaluator value) {
73717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (value != null) {
73817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mEvaluator = value;
73917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
74017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
74117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
74217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void start() {
74317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        sPendingAnimations.add(this);
74417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (sAnimationHandler == null) {
74517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            sAnimationHandler = new AnimationHandler();
74617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
74717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // TODO: does this put too many messages on the queue if the handler
74817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // is already running?
74917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        sAnimationHandler.sendEmptyMessage(ANIMATION_START);
75017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
75117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
75217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void cancel() {
75317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mListeners != null) {
75417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            ArrayList<AnimatableListener> tmpListeners =
75517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    (ArrayList<AnimatableListener>) mListeners.clone();
75617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            for (AnimatableListener listener : tmpListeners) {
75717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                listener.onAnimationCancel(this);
75817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
75917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
76017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // Just set the CANCELED flag - this causes the animation to end the next time a frame
76117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // is processed.
76217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        mPlayingState = CANCELED;
76317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
76417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
76517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void end() {
76617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // Just set the ENDED flag - this causes the animation to end the next time a frame
76717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // is processed.
76817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        mPlayingState = ENDED;
76917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
77017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
77117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
77217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Called internally to end an animation by removing it from the animations list. Must be
77317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * called on the UI thread.
77417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
77517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private void endAnimation() {
77617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        sAnimations.remove(this);
77717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mListeners != null) {
77817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            ArrayList<AnimatableListener> tmpListeners =
77917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    (ArrayList<AnimatableListener>) mListeners.clone();
78017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            for (AnimatableListener listener : tmpListeners) {
78117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                listener.onAnimationEnd(this);
78217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
78317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
78417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        mPlayingState = STOPPED;
78517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
78617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
78717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
78817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Called internally to start an animation by adding it to the active animations list. Must be
78917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * called on the UI thread.
79017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
79117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private void startAnimation() {
79217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        initAnimation();
79317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        sAnimations.add(this);
79417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mListeners != null) {
79517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            ArrayList<AnimatableListener> tmpListeners =
79617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    (ArrayList<AnimatableListener>) mListeners.clone();
79717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            for (AnimatableListener listener : tmpListeners) {
79817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                listener.onAnimationStart(this);
79917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
80017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
80117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
80217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
80317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
80417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Internal function called to process an animation frame on an animation that is currently
80517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * sleeping through its <code>startDelay</code> phase. The return value indicates whether it
80617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * should be woken up and put on the active animations queue.
80717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
80817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param currentTime The current animation time, used to calculate whether the animation
80917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * has exceeded its <code>startDelay</code> and should be started.
81017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @return True if the animation's <code>startDelay</code> has been exceeded and the animation
81117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * should be added to the set of active animations.
81217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
81317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private boolean delayedAnimationFrame(long currentTime) {
81417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (!mStartedDelay) {
81517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mStartedDelay = true;
81617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mDelayStartTime = currentTime;
81717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        } else {
81817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            long deltaTime = currentTime - mDelayStartTime;
81917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (deltaTime > mStartDelay) {
82017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                // startDelay ended - start the anim and record the
82117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                // mStartTime appropriately
82217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mStartTime = currentTime - (deltaTime - mStartDelay);
82317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mPlayingState = RUNNING;
82417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                return true;
82517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
82617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
82717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        return false;
82817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
82917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
83017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
83117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * This internal function processes a single animation frame for a given animation. The
83217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * currentTime parameter is the timing pulse sent by the handler, used to calculate the
83317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * elapsed duration, and therefore
83417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * the elapsed fraction, of the animation. The return value indicates whether the animation
83517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * should be ended (which happens when the elapsed time of the animation exceeds the
83617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * animation's duration, including the repeatCount).
83717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
83817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param currentTime The current time, as tracked by the static timing handler
83917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @return true if the animation's duration, including any repetitions due to
84017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>repeatCount</code> has been exceeded and the animation should be ended.
84117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
84217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private boolean animationFrame(long currentTime) {
84317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
84417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        boolean done = false;
84517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
84617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mPlayingState == STOPPED) {
84717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mPlayingState = RUNNING;
84817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mStartTime = currentTime;
84917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
85017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        switch (mPlayingState) {
85117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        case RUNNING:
85217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            float fraction = (float)(currentTime - mStartTime) / mDuration;
85317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (fraction >= 1f) {
85417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                if (mCurrentIteration < mRepeatCount || mRepeatCount == INFINITE) {
85517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // Time to repeat
85617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    if (mListeners != null) {
85717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        for (AnimatableListener listener : mListeners) {
85817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            listener.onAnimationRepeat(this);
85917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        }
86017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
86117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    ++mCurrentIteration;
86217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    if (mRepeatMode == REVERSE) {
86317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        mPlayingBackwards = mPlayingBackwards ? false : true;
86417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
86517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // TODO: doesn't account for fraction going Wayyyyy over 1, like 2+
86617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    fraction = fraction - 1f;
86717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    mStartTime += mDuration;
86817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                } else {
86917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    done = true;
87017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    fraction = Math.min(fraction, 1.0f);
87117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
87217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
87317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (mPlayingBackwards) {
87417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                fraction = 1f - fraction;
87517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
87617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            animateValue(fraction);
87717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            break;
87817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        case ENDED:
87917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            // The final value set on the target varies, depending on whether the animation
88017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            // was supposed to repeat an odd number of times
88117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (mRepeatCount > 0 && (mRepeatCount & 0x01) == 1) {
88217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                animateValue(0f);
88317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            } else {
88417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                animateValue(1f);
88517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
88617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            // Fall through to set done flag
88717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        case CANCELED:
88817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            done = true;
88917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            break;
89017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
89117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
89217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        return done;
89317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
89417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
89517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
89617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * This method is called with the elapsed fraction of the animation during every
89717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * animation frame. This function turns the elapsed fraction into an interpolated fraction
89817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * and then into an animated value (from the evaluator. The function is called mostly during
89917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * animation updates, but it is also called when the <code>end()</code>
90017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * function is called, to set the final value on the property.
90117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
90217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p>Overrides of this method must call the superclass to perform the calculation
90317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * of the animated value.</p>
90417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
90517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param fraction The elapsed fraction of the animation.
90617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
90717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    void animateValue(float fraction) {
90817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        fraction = mInterpolator.getInterpolation(fraction);
9093dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase        if (mKeyframeSet != null) {
9103dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase            mAnimatedValue = mKeyframeSet.getValue(fraction, mEvaluator);
9113dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase        } else {
9123dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase            mAnimatedValue = mEvaluator.evaluate(fraction, mValueFrom, mValueTo);
9133dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase        }
91417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mUpdateListeners != null) {
91517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            int numListeners = mUpdateListeners.size();
91617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            for (int i = 0; i < numListeners; ++i) {
91717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mUpdateListeners.get(i).onAnimationUpdate(this);
91817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
91917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
92017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
92117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
92217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
92317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Implementors of this interface can add themselves as update listeners
92417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * to an <code>Animator</code> instance to receive callbacks on every animation
92517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * frame, after the current frame's values have been calculated for that
92617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>Animator</code>.
92717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
92817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public static interface AnimatorUpdateListener {
92917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
93017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * <p>Notifies the occurrence of another frame of the animation.</p>
93117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *
93217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param animation The animation which was repeated.
93317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
93417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        void onAnimationUpdate(Animator animation);
93517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
93617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
93717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase}