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