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