Animator.java revision d51d368f2d512ab657b8ae45780c82c0dbea94c3
117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase/*
217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Copyright (C) 2010 The Android Open Source Project
317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase *
417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Licensed under the Apache License, Version 2.0 (the "License");
517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * you may not use this file except in compliance with the License.
617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * You may obtain a copy of the License at
717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase *
817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase *      http://www.apache.org/licenses/LICENSE-2.0
917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase *
1017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Unless required by applicable law or agreed to in writing, software
1117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * distributed under the License is distributed on an "AS IS" BASIS,
1217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * See the License for the specific language governing permissions and
1417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * limitations under the License.
1517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */
1617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
1717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haasepackage android.animation;
1817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
19f54a8d7c479485174941c38f151ea7083c658da3Chet Haaseimport android.content.Context;
2017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport android.os.Handler;
21602e4d3824bf8b9cb9f817375d195b969712176aChet Haaseimport android.os.Looper;
2217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport android.os.Message;
2317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport android.view.animation.AccelerateDecelerateInterpolator;
2417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport android.view.animation.AnimationUtils;
2517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport android.view.animation.Interpolator;
2617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
2717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport java.util.ArrayList;
28d953d08e9299072130d9f4411cbcf6678bbce822Chet Haaseimport java.util.HashMap;
2917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
3017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase/**
3117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * This class provides a simple timing engine for running animations
3217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * which calculate animated values and set them on target objects.
3317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase *
34010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * <p>There is a single timing pulse that all animations use. It runs in a
35010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * custom handler to ensure that property changes happen on the UI thread.</p>
36010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase *
37010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * <p>By default, Animator uses non-linear time interpolation, via the
38010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * {@link AccelerateDecelerateInterpolator} class, which accelerates into and decelerates
39010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * out of an animation. This behavior can be changed by calling
40010dbaa1236cf2dcdc62c29049468e90188acaaeChet Haase * {@link Animator#setInterpolator(Interpolator)}.</p>
4117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */
42d953d08e9299072130d9f4411cbcf6678bbce822Chet Haasepublic class Animator<T> extends Animatable {
4317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
4417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
4517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Internal constants
4617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
4717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
4817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /*
4917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The default amount of time in ms between animation frames
5017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
5117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final long DEFAULT_FRAME_DELAY = 30;
5217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
5317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
5417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Messages sent to timing handler: START is sent when an animation first begins, FRAME is sent
5517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * by the handler to itself to process the next animation frame
5617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
5717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final int ANIMATION_START = 0;
5817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final int ANIMATION_FRAME = 1;
5917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
6017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
6117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Values used with internal variable mPlayingState to indicate the current state of an
6217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * animation.
6317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
6417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final int STOPPED    = 0; // Not yet playing
6517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final int RUNNING    = 1; // Playing normally
6617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final int CANCELED   = 2; // cancel() called - need to end it
6717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final int ENDED      = 3; // end() called - need to end it
685d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase    private static final int SEEKED     = 4; // Seeked to some time value
6917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
7017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
7117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Internal variables
7249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase     * NOTE: This object implements the clone() method, making a deep copy of any referenced
7349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase     * objects. As other non-trivial fields are added to this class, make sure to add logic
7449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase     * to clone() to make deep copies of them.
7517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
7617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
7717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // The first time that the animation's animateFrame() method is called. This time is used to
7817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // determine elapsed time (and therefore the elapsed fraction) in subsequent calls
7917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // to animateFrame()
8017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private long mStartTime;
8117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
825d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase    /**
835d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase     * Set when setCurrentPlayTime() is called. If negative, animation is not currently seeked
845d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase     * to a value.
855d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase     */
865d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase    private long mSeekTime = -1;
875d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase
8817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // The static sAnimationHandler processes the internal timing loop on which all animations
8917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // are based
9017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static AnimationHandler sAnimationHandler;
9117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
9217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // The static list of all active animations
9317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final ArrayList<Animator> sAnimations = new ArrayList<Animator>();
9417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
9517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // The set of animations to be started on the next animation frame
9617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final ArrayList<Animator> sPendingAnimations = new ArrayList<Animator>();
9717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
9817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // The time interpolator to be used if none is set on the animation
9917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final Interpolator sDefaultInterpolator = new AccelerateDecelerateInterpolator();
10017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
10117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // type evaluators for the three primitive types handled by this implementation
10217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final TypeEvaluator sIntEvaluator = new IntEvaluator();
10317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final TypeEvaluator sFloatEvaluator = new FloatEvaluator();
10417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final TypeEvaluator sDoubleEvaluator = new DoubleEvaluator();
10517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
10617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
10717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Used to indicate whether the animation is currently playing in reverse. This causes the
10817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * elapsed fraction to be inverted to calculate the appropriate values.
10917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
11017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private boolean mPlayingBackwards = false;
11117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
11217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
11317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * This variable tracks the current iteration that is playing. When mCurrentIteration exceeds the
11417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * repeatCount (if repeatCount!=INFINITE), the animation ends
11517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
11617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private int mCurrentIteration = 0;
11717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
11817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
11917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Tracks whether a startDelay'd animation has begun playing through the startDelay.
12017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
12117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private boolean mStartedDelay = false;
12217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
12317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
12417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Tracks the time at which the animation began playing through its startDelay. This is
12517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * different from the mStartTime variable, which is used to track when the animation became
12617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * active (which is when the startDelay expired and the animation was added to the active
12717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * animations list).
12817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
12917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private long mDelayStartTime;
13017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
13117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
13217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Flag that represents the current state of the animation. Used to figure out when to start
13317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * an animation (if state == STOPPED). Also used to end an animation that
13417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * has been cancel()'d or end()'d since the last animation frame. Possible values are
13517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * STOPPED, RUNNING, ENDED, CANCELED.
13617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
13717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private int mPlayingState = STOPPED;
13817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
13917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
14017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Internal collections used to avoid set collisions as animations start and end while being
14117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * processed.
14217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
14317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final ArrayList<Animator> sEndingAnims = new ArrayList<Animator>();
14417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final ArrayList<Animator> sDelayedAnims = new ArrayList<Animator>();
14517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static final ArrayList<Animator> sReadyAnims = new ArrayList<Animator>();
14617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
1475d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase    /**
1485d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase     * Flag that denotes whether the animation is set up and ready to go. Used by seek() to
1495d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase     * set up animation that has not yet been started.
1505d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase     */
1515d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase    private boolean mInitialized = false;
1525d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase
15317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    //
15417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // Backing variables
15517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    //
15617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
15717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // How long the animation should last in ms
15817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private long mDuration;
15917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
16017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // The amount of time in ms to delay starting the animation after start() is called
16117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private long mStartDelay = 0;
16217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
16317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // The number of milliseconds between animation frames
16417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static long sFrameDelay = DEFAULT_FRAME_DELAY;
16517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
16617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // The number of times the animation will repeat. The default is 0, which means the animation
16717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // will play only once
16817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private int mRepeatCount = 0;
16917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
17017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
17117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The type of repetition that will occur when repeatMode is nonzero. RESTART means the
17217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * animation will start from the beginning on every new cycle. REVERSE means the animation
17317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * will reverse directions on each iteration.
17417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
17517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private int mRepeatMode = RESTART;
17617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
17717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
17817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The time interpolator to be used. The elapsed fraction of the animation will be passed
17917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * through this interpolator to calculate the interpolated fraction, which is then used to
18017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * calculate the animated values.
18117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
18217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private Interpolator mInterpolator = sDefaultInterpolator;
18317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
18417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
18517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The set of listeners to be sent events through the life of an animation.
18617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
18717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private ArrayList<AnimatorUpdateListener> mUpdateListeners = null;
18817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
18917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
190d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * The property/value sets being animated.
19117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
192602e4d3824bf8b9cb9f817375d195b969712176aChet Haase    PropertyValuesHolder[] mValues;
19317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
19417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
195602e4d3824bf8b9cb9f817375d195b969712176aChet Haase     * A hashmap of the PropertyValuesHolder objects. This map is used to lookup animated values
196602e4d3824bf8b9cb9f817375d195b969712176aChet Haase     * by property name during calls to getAnimatedValue(String).
1973dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     */
198602e4d3824bf8b9cb9f817375d195b969712176aChet Haase    HashMap<String, PropertyValuesHolder> mValuesMap;
1993dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase
2003dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    /**
20117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Public constants
20217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
20317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
20417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
20517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * When the animation reaches the end and <code>repeatCount</code> is INFINITE
20617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * or a positive value, the animation restarts from the beginning.
20717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
20817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public static final int RESTART = 1;
20917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
21017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * When the animation reaches the end and <code>repeatCount</code> is INFINITE
21117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * or a positive value, the animation reverses direction on every iteration.
21217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
21317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public static final int REVERSE = 2;
21417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
21517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * This value used used with the {@link #setRepeatCount(int)} property to repeat
21617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * the animation indefinitely.
21717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
21817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public static final int INFINITE = -1;
21917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
220f54a8d7c479485174941c38f151ea7083c658da3Chet Haase    /**
221d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase     * Creates a new Animator object. This default constructor is primarily for
222d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase     * use internally; the other constructors which take parameters are more generally
223d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase     * useful.
224f54a8d7c479485174941c38f151ea7083c658da3Chet Haase     */
225d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase    public Animator() {
226f54a8d7c479485174941c38f151ea7083c658da3Chet Haase    }
227f54a8d7c479485174941c38f151ea7083c658da3Chet Haase
22817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
229d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * Constructs an Animator object with the specified duration and set of
230d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * values. If the values are a set of PropertyValuesHolder objects, then these objects
231d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * define the potentially multiple properties being animated and the values the properties are
232d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * animated between. Otherwise, the values define a single set of values animated between.
2333dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     *
2343dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * @param duration The length of the animation, in milliseconds.
235d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * @param values The set of values to animate between. If these values are not
236d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * PropertyValuesHolder objects, then there should be more than one value, since the values
237d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * determine the interval to animate between.
2383dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     */
239d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase    public Animator(long duration, T...values) {
2403dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase        mDuration = duration;
241d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase        if (values.length > 0) {
242d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase            setValues(values);
243d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase        }
244fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    }
245fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase
246d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase    public void setValues(PropertyValuesHolder... values) {
247d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase        int numValues = values.length;
248602e4d3824bf8b9cb9f817375d195b969712176aChet Haase        mValues = values;
249602e4d3824bf8b9cb9f817375d195b969712176aChet Haase        mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues);
250d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase        for (int i = 0; i < numValues; ++i) {
251d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase            PropertyValuesHolder valuesHolder = (PropertyValuesHolder) values[i];
252602e4d3824bf8b9cb9f817375d195b969712176aChet Haase            mValuesMap.put(valuesHolder.getPropertyName(), valuesHolder);
253d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase        }
254fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    }
255fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase
256fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    /**
257d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * Sets the values to animate between for this animation. If <code>values</code> is
258d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * a set of PropertyValuesHolder objects, these objects will become the set of properties
259d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * animated and the values that those properties are animated between. Otherwise, this method
260d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * will set only one set of values for the Animator. Also, if the values are not
261d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * PropertyValuesHolder objects and if there are already multiple sets of
262d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * values defined for this Animator via
263d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * more than one PropertyValuesHolder objects, this method will set the values for
264d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * the first of those objects.
265fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     *
266d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * @param values The set of values to animate between.
267fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     */
268d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase    public void setValues(T... values) {
269602e4d3824bf8b9cb9f817375d195b969712176aChet Haase        if (mValues == null || mValues.length == 0) {
270602e4d3824bf8b9cb9f817375d195b969712176aChet Haase            setValues(new PropertyValuesHolder[]{
271602e4d3824bf8b9cb9f817375d195b969712176aChet Haase                    new PropertyValuesHolder("", (Object[])values)});
272d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase        } else {
273602e4d3824bf8b9cb9f817375d195b969712176aChet Haase            PropertyValuesHolder valuesHolder = mValues[0];
274602e4d3824bf8b9cb9f817375d195b969712176aChet Haase            valuesHolder.setValues(values);
275d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase        }
276fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    }
277fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase
278fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    /**
279fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * This function is called immediately before processing the first animation
280fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * frame of an animation. If there is a nonzero <code>startDelay</code>, the
281fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * function is called after that delay ends.
282fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * It takes care of the final initialization steps for the
283fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * animation.
284fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     *
285fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     *  <p>Overrides of this method should call the superclass method to ensure
286fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     *  that internal mechanisms for the animation are set up correctly.</p>
287fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     */
288fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    void initAnimation() {
289602e4d3824bf8b9cb9f817375d195b969712176aChet Haase        int numValues = mValues.length;
290602e4d3824bf8b9cb9f817375d195b969712176aChet Haase        for (int i = 0; i < numValues; ++i) {
291602e4d3824bf8b9cb9f817375d195b969712176aChet Haase            mValues[i].init();
292fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        }
293fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        mCurrentIteration = 0;
2945d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase        mInitialized = true;
2955d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase    }
2965d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase
297d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase
298d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase    /**
299d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase     * Sets the length of the animation.
300d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase     *
301d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase     * @param duration The length of the animation, in milliseconds.
302d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase     */
303d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase    public void setDuration(long duration) {
304d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase        mDuration = duration;
305d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase    }
306d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase
307d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase    /**
308d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase     * Gets the length of the animation.
309d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase     *
310d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase     * @return The length of the animation, in milliseconds.
311d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase     */
312d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase    public long getDuration() {
313d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase        return mDuration;
314d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase    }
315d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase
3165d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase    /**
3175d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase     * Sets the position of the animation to the specified point in time. This time should
3185d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase     * be between 0 and the total duration of the animation, including any repetition. If
3195d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase     * the animation has not yet been started, then it will not advance forward after it is
3205d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase     * set to this time; it will simply set the time to this value and perform any appropriate
321602e4d3824bf8b9cb9f817375d195b969712176aChet Haase     * actions based on that time. If the animation is already running, then setCurrentPlayTime()
322602e4d3824bf8b9cb9f817375d195b969712176aChet Haase     * will set the current playing time to this value and continue playing from that point.
3235d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase     *
3245d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase     * @param playTime The time, in milliseconds, to which the animation is advanced or rewound.
3255d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase     */
3265d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase    public void setCurrentPlayTime(long playTime) {
3275d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase        if (!mInitialized) {
3285d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase            initAnimation();
3295d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase        }
3305d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase        long currentTime = AnimationUtils.currentAnimationTimeMillis();
3315d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase        if (mPlayingState != RUNNING) {
3325d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase            mSeekTime = playTime;
3335d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase            mPlayingState = SEEKED;
3345d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase        }
3355d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase        mStartTime = currentTime - playTime;
3365d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase        animationFrame(currentTime);
3375d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase    }
3385d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase
3395d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase    /**
3405d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase     * Gets the current position of the animation in time, which is equal to the current
3415d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase     * time minus the time that the animation started. An animation that is not yet started will
3425d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase     * return a value of zero.
3435d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase     *
3445d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase     * @return The current position in time of the animation.
3455d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase     */
3465d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase    public long getCurrentPlayTime() {
34749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        if (!mInitialized || mPlayingState == STOPPED) {
3485d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase            return 0;
3495d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase        }
3505d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase        return AnimationUtils.currentAnimationTimeMillis() - mStartTime;
351fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    }
352fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase
353fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    /**
35417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * This custom, static handler handles the timing pulse that is shared by
35517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * all active animations. This approach ensures that the setting of animation
35617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * values will happen on the UI thread and that all animations will share
35717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * the same times for calculating their values, which makes synchronizing
35817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * animations possible.
35917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
36017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
36117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private static class AnimationHandler extends Handler {
36217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
36317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * There are only two messages that we care about: ANIMATION_START and
36417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * ANIMATION_FRAME. The START message is sent when an animation's start()
36517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * method is called. It cannot start synchronously when start() is called
36617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * because the call may be on the wrong thread, and it would also not be
36717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * synchronized with other animations because it would not start on a common
36817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * timing pulse. So each animation sends a START message to the handler, which
36917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * causes the handler to place the animation on the active animations queue and
37017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * start processing frames for that animation.
37117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * The FRAME message is the one that is sent over and over while there are any
37217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * active animations to process.
37317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
37417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        @Override
37517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        public void handleMessage(Message msg) {
37617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            boolean callAgain = true;
37717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            switch (msg.what) {
37817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                // TODO: should we avoid sending frame message when starting if we
37917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                // were already running?
38017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                case ANIMATION_START:
38117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    if (sAnimations.size() > 0 || sDelayedAnims.size() > 0) {
38217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        callAgain = false;
38317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
38417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // pendingAnims holds any animations that have requested to be started
38517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // We're going to clear sPendingAnimations, but starting animation may
38617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // cause more to be added to the pending list (for example, if one animation
38717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // starting triggers another starting). So we loop until sPendingAnimations
38817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // is empty.
38917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    while (sPendingAnimations.size() > 0) {
39017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        ArrayList<Animator> pendingCopy =
39117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                                (ArrayList<Animator>) sPendingAnimations.clone();
39217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        sPendingAnimations.clear();
39317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        int count = pendingCopy.size();
39417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        for (int i = 0; i < count; ++i) {
39517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            Animator anim = pendingCopy.get(i);
39617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            // If the animation has a startDelay, place it on the delayed list
39717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            if (anim.mStartDelay == 0) {
39817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                                anim.startAnimation();
39917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            } else {
40017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                                sDelayedAnims.add(anim);
40117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            }
40217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        }
40317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
40417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // fall through to process first frame of new animations
40517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                case ANIMATION_FRAME:
40617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // currentTime holds the common time for all animations processed
40717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // during this frame
40817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    long currentTime = AnimationUtils.currentAnimationTimeMillis();
40917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
41017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // First, process animations currently sitting on the delayed queue, adding
41117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // them to the active animations if they are ready
41217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    int numDelayedAnims = sDelayedAnims.size();
41317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    for (int i = 0; i < numDelayedAnims; ++i) {
41417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        Animator anim = sDelayedAnims.get(i);
41517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        if (anim.delayedAnimationFrame(currentTime)) {
41617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            sReadyAnims.add(anim);
41717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        }
41817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
41917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    int numReadyAnims = sReadyAnims.size();
42017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    if (numReadyAnims > 0) {
42117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        for (int i = 0; i < numReadyAnims; ++i) {
42217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            Animator anim = sReadyAnims.get(i);
42317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            anim.startAnimation();
42417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            sDelayedAnims.remove(anim);
42517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        }
42617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        sReadyAnims.clear();
42717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
42817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
42917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // Now process all active animations. The return value from animationFrame()
43017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // tells the handler whether it should now be ended
43117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    int numAnims = sAnimations.size();
43217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    for (int i = 0; i < numAnims; ++i) {
43317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        Animator anim = sAnimations.get(i);
43417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        if (anim.animationFrame(currentTime)) {
43517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            sEndingAnims.add(anim);
43617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        }
43717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
43817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    if (sEndingAnims.size() > 0) {
43917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        for (int i = 0; i < sEndingAnims.size(); ++i) {
44017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            sEndingAnims.get(i).endAnimation();
44117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        }
44217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        sEndingAnims.clear();
44317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
44417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
44517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // If there are still active or delayed animations, call the handler again
44617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // after the frameDelay
44717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    if (callAgain && (!sAnimations.isEmpty() || !sDelayedAnims.isEmpty())) {
44817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        sendEmptyMessageDelayed(ANIMATION_FRAME, sFrameDelay);
44917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
45017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    break;
45117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
45217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
45317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
45417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
45517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
45617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The amount of time, in milliseconds, to delay starting the animation after
45717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * {@link #start()} is called.
45817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
45917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @return the number of milliseconds to delay running the animation
46017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
46117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public long getStartDelay() {
46217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        return mStartDelay;
46317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
46417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
46517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
46617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The amount of time, in milliseconds, to delay starting the animation after
46717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * {@link #start()} is called.
46817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
46917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param startDelay The amount of the delay, in milliseconds
47017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
47117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void setStartDelay(long startDelay) {
47217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        this.mStartDelay = startDelay;
47317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
47417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
47517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
47617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The amount of time, in milliseconds, between each frame of the animation. This is a
47717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * requested time that the animation will attempt to honor, but the actual delay between
47817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * frames may be different, depending on system load and capabilities. This is a static
47917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * function because the same delay will be applied to all animations, since they are all
48017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * run off of a single timing loop.
48117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
48217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @return the requested time between frames, in milliseconds
48317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
48417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public static long getFrameDelay() {
48517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        return sFrameDelay;
48617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
48717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
48817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
48917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The amount of time, in milliseconds, between each frame of the animation. This is a
49017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * requested time that the animation will attempt to honor, but the actual delay between
49117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * frames may be different, depending on system load and capabilities. This is a static
49217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * function because the same delay will be applied to all animations, since they are all
49317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * run off of a single timing loop.
49417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
49517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param frameDelay the requested time between frames, in milliseconds
49617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
49717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public static void setFrameDelay(long frameDelay) {
49817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        sFrameDelay = frameDelay;
49917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
50017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
50117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
502d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * The most recent value calculated by this <code>Animator</code> when there is just one
503d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * property being animated. This value is only sensible while the animation is running. The main
50417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * purpose for this read-only property is to retrieve the value from the <code>Animator</code>
50517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * during a call to {@link AnimatorUpdateListener#onAnimationUpdate(Animator)}, which
50617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * is called during each animation frame, immediately after the value is calculated.
50717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
50817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @return animatedValue The value most recently calculated by this <code>Animator</code> for
509d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * the single property being animated. If there are several properties being animated
510d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * (specified by several PropertyValuesHolder objects in the constructor), this function
511d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * returns the animated value for the first of those objects.
51217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
51317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public Object getAnimatedValue() {
514602e4d3824bf8b9cb9f817375d195b969712176aChet Haase        if (mValues != null && mValues.length > 0) {
515602e4d3824bf8b9cb9f817375d195b969712176aChet Haase            return mValues[0].getAnimatedValue();
516602e4d3824bf8b9cb9f817375d195b969712176aChet Haase        }
517602e4d3824bf8b9cb9f817375d195b969712176aChet Haase        // Shouldn't get here; should always have values unless Animator was set up wrong
518602e4d3824bf8b9cb9f817375d195b969712176aChet Haase        return null;
519d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase    }
520d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase
521d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase    /**
522d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * The most recent value calculated by this <code>Animator</code> for <code>propertyName</code>.
523d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * The main purpose for this read-only property is to retrieve the value from the
524d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * <code>Animator</code> during a call to
525d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * {@link AnimatorUpdateListener#onAnimationUpdate(Animator)}, which
526d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * is called during each animation frame, immediately after the value is calculated.
527d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     *
528d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * @return animatedValue The value most recently calculated for the named property
529d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * by this <code>Animator</code>.
530d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     */
531d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase    public Object getAnimatedValue(String propertyName) {
532602e4d3824bf8b9cb9f817375d195b969712176aChet Haase        PropertyValuesHolder valuesHolder = mValuesMap.get(propertyName);
533602e4d3824bf8b9cb9f817375d195b969712176aChet Haase        if (valuesHolder != null) {
534602e4d3824bf8b9cb9f817375d195b969712176aChet Haase            return valuesHolder.getAnimatedValue();
535602e4d3824bf8b9cb9f817375d195b969712176aChet Haase        } else {
536602e4d3824bf8b9cb9f817375d195b969712176aChet Haase            // At least avoid crashing if called with bogus propertyName
537602e4d3824bf8b9cb9f817375d195b969712176aChet Haase            return null;
538602e4d3824bf8b9cb9f817375d195b969712176aChet Haase        }
53917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
54017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
54117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
54217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Sets how many times the animation should be repeated. If the repeat
54317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * count is 0, the animation is never repeated. If the repeat count is
54417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * greater than 0 or {@link #INFINITE}, the repeat mode will be taken
54517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * into account. The repeat count is 0 by default.
54617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
54717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param value the number of times the animation should be repeated
54817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
54917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void setRepeatCount(int value) {
55017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        mRepeatCount = value;
55117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
55217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
55317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Defines how many times the animation should repeat. The default value
55417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * is 0.
55517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
55617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @return the number of times the animation should repeat, or {@link #INFINITE}
55717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
55817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public int getRepeatCount() {
55917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        return mRepeatCount;
56017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
56117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
56217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
56317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Defines what this animation should do when it reaches the end. This
56417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * setting is applied only when the repeat count is either greater than
56517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * 0 or {@link #INFINITE}. Defaults to {@link #RESTART}.
56617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
56717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param value {@link #RESTART} or {@link #REVERSE}
56817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
56917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void setRepeatMode(int value) {
57017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        mRepeatMode = value;
57117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
57217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
57317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
57417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Defines what this animation should do when it reaches the end.
57517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
57617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @return either one of {@link #REVERSE} or {@link #RESTART}
57717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
57817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public int getRepeatMode() {
57917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        return mRepeatMode;
58017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
58117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
58217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
58317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Adds a listener to the set of listeners that are sent update events through the life of
58417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * an animation. This method is called on all listeners for every frame of the animation,
58517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * after the values for the animation have been calculated.
58617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
58717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param listener the listener to be added to the current set of listeners for this animation.
58817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
58917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void addUpdateListener(AnimatorUpdateListener listener) {
59017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mUpdateListeners == null) {
59117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mUpdateListeners = new ArrayList<AnimatorUpdateListener>();
59217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
59317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        mUpdateListeners.add(listener);
59417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
59517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
59617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
59717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Removes a listener from the set listening to frame updates for this animation.
59817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
59917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param listener the listener to be removed from the current set of update listeners
60017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * for this animation.
60117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
60217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void removeUpdateListener(AnimatorUpdateListener listener) {
60317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mUpdateListeners == null) {
60417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            return;
60517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
60617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        mUpdateListeners.remove(listener);
60717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mUpdateListeners.size() == 0) {
60817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mUpdateListeners = null;
60917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
61017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
61117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
61217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
61317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
61417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The time interpolator used in calculating the elapsed fraction of this animation. The
61517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * interpolator determines whether the animation runs with linear or non-linear motion,
61617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * such as acceleration and deceleration. The default value is
61717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * {@link android.view.animation.AccelerateDecelerateInterpolator}
61817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
61917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param value the interpolator to be used by this animation
62017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
62117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void setInterpolator(Interpolator value) {
62217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (value != null) {
62317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mInterpolator = value;
62417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
62517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
62617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
62717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
628602e4d3824bf8b9cb9f817375d195b969712176aChet Haase     * Returns the timing interpolator that this Animator uses.
629602e4d3824bf8b9cb9f817375d195b969712176aChet Haase     *
630602e4d3824bf8b9cb9f817375d195b969712176aChet Haase     * @return The timing interpolator for this Animator.
631602e4d3824bf8b9cb9f817375d195b969712176aChet Haase     */
632602e4d3824bf8b9cb9f817375d195b969712176aChet Haase    public Interpolator getInterpolator() {
633602e4d3824bf8b9cb9f817375d195b969712176aChet Haase        return mInterpolator;
634602e4d3824bf8b9cb9f817375d195b969712176aChet Haase    }
635602e4d3824bf8b9cb9f817375d195b969712176aChet Haase
636602e4d3824bf8b9cb9f817375d195b969712176aChet Haase    /**
63717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The type evaluator to be used when calculating the animated values of this animation.
63817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The system will automatically assign a float, int, or double evaluator based on the type
63917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * of <code>startValue</code> and <code>endValue</code> in the constructor. But if these values
64017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * are not one of these primitive types, or if different evaluation is desired (such as is
64117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * necessary with int values that represent colors), a custom evaluator needs to be assigned.
64217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * For example, when running an animation on color values, the {@link RGBEvaluator}
64317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * should be used to get correct RGB color interpolation.
64417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
645d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * <p>If this Animator has only one set of values being animated between, this evaluator
646d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * will be used for that set. If there are several sets of values being animated, which is
647d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * the case if PropertyValuesHOlder objects were set on the Animator, then the evaluator
648d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * is assigned just to the first PropertyValuesHolder object.</p>
649d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     *
65017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param value the evaluator to be used this animation
65117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
65217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void setEvaluator(TypeEvaluator value) {
653602e4d3824bf8b9cb9f817375d195b969712176aChet Haase        if (value != null && mValues != null && mValues.length > 0) {
654602e4d3824bf8b9cb9f817375d195b969712176aChet Haase            mValues[0].setEvaluator(value);
65517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
65617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
65717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
658d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase    /**
659d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * Start the animation playing. This version of start() takes a boolean flag that indicates
660d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * whether the animation should play in reverse. The flag is usually false, but may be set
661d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * to true if called from the reverse() method/
662d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     *
663d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * @param playBackwards Whether the Animator should start playing in reverse.
664d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     */
6655c7649857246333572eb332b505ad617365ef5faChet Haase    private void start(boolean playBackwards) {
666d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase        mPlayingBackwards = playBackwards;
667602e4d3824bf8b9cb9f817375d195b969712176aChet Haase        if ((mStartDelay == 0) && (Thread.currentThread() == Looper.getMainLooper().getThread())) {
668602e4d3824bf8b9cb9f817375d195b969712176aChet Haase            // This sets the initial value of the animation, prior to actually starting it running
669602e4d3824bf8b9cb9f817375d195b969712176aChet Haase            setCurrentPlayTime(getCurrentPlayTime());
670602e4d3824bf8b9cb9f817375d195b969712176aChet Haase        }
6715d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase        mPlayingState = STOPPED;
67217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        sPendingAnimations.add(this);
67317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (sAnimationHandler == null) {
67417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            sAnimationHandler = new AnimationHandler();
67517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
67617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // TODO: does this put too many messages on the queue if the handler
67717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // is already running?
67817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        sAnimationHandler.sendEmptyMessage(ANIMATION_START);
67917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
68017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
6815c7649857246333572eb332b505ad617365ef5faChet Haase    @Override
6825c7649857246333572eb332b505ad617365ef5faChet Haase    public void start() {
6835c7649857246333572eb332b505ad617365ef5faChet Haase        start(false);
6845c7649857246333572eb332b505ad617365ef5faChet Haase    }
6855c7649857246333572eb332b505ad617365ef5faChet Haase
6865c7649857246333572eb332b505ad617365ef5faChet Haase    @Override
68717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void cancel() {
68817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mListeners != null) {
68917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            ArrayList<AnimatableListener> tmpListeners =
69017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    (ArrayList<AnimatableListener>) mListeners.clone();
69117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            for (AnimatableListener listener : tmpListeners) {
69217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                listener.onAnimationCancel(this);
69317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
69417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
69517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // Just set the CANCELED flag - this causes the animation to end the next time a frame
69617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // is processed.
69717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        mPlayingState = CANCELED;
69817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
69917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
7005c7649857246333572eb332b505ad617365ef5faChet Haase    @Override
70117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void end() {
70217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // Just set the ENDED flag - this causes the animation to end the next time a frame
70317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // is processed.
70417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        mPlayingState = ENDED;
70517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
70617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
70717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
7085c7649857246333572eb332b505ad617365ef5faChet Haase     * Returns whether this Animator is currently running (having been started and not yet ended).
7095c7649857246333572eb332b505ad617365ef5faChet Haase     * @return Wehther the Animator is running.
7105c7649857246333572eb332b505ad617365ef5faChet Haase     */
7115c7649857246333572eb332b505ad617365ef5faChet Haase    public boolean isRunning() {
7125c7649857246333572eb332b505ad617365ef5faChet Haase        return mPlayingState == RUNNING;
7135c7649857246333572eb332b505ad617365ef5faChet Haase    }
7145c7649857246333572eb332b505ad617365ef5faChet Haase
7155c7649857246333572eb332b505ad617365ef5faChet Haase    /**
7165c7649857246333572eb332b505ad617365ef5faChet Haase     * Plays the Animator in reverse. If the animation is already running,
7175c7649857246333572eb332b505ad617365ef5faChet Haase     * it will stop itself and play backwards from the point reached when reverse was called.
7185c7649857246333572eb332b505ad617365ef5faChet Haase     * If the animation is not currently running, then it will start from the end and
7195c7649857246333572eb332b505ad617365ef5faChet Haase     * play backwards. This behavior is only set for the current animation; future playing
7205c7649857246333572eb332b505ad617365ef5faChet Haase     * of the animation will use the default behavior of playing forward.
7215c7649857246333572eb332b505ad617365ef5faChet Haase     */
7225c7649857246333572eb332b505ad617365ef5faChet Haase    public void reverse() {
7235c7649857246333572eb332b505ad617365ef5faChet Haase        mPlayingBackwards = !mPlayingBackwards;
7245c7649857246333572eb332b505ad617365ef5faChet Haase        if (mPlayingState == RUNNING) {
7255c7649857246333572eb332b505ad617365ef5faChet Haase            long currentTime = AnimationUtils.currentAnimationTimeMillis();
7265c7649857246333572eb332b505ad617365ef5faChet Haase            long currentPlayTime = currentTime - mStartTime;
7275c7649857246333572eb332b505ad617365ef5faChet Haase            long timeLeft = mDuration - currentPlayTime;
7285c7649857246333572eb332b505ad617365ef5faChet Haase            mStartTime = currentTime - timeLeft;
7295c7649857246333572eb332b505ad617365ef5faChet Haase        } else {
7305c7649857246333572eb332b505ad617365ef5faChet Haase            start(true);
7315c7649857246333572eb332b505ad617365ef5faChet Haase        }
7325c7649857246333572eb332b505ad617365ef5faChet Haase    }
7335c7649857246333572eb332b505ad617365ef5faChet Haase
7345c7649857246333572eb332b505ad617365ef5faChet Haase    /**
73517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Called internally to end an animation by removing it from the animations list. Must be
73617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * called on the UI thread.
73717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
73817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private void endAnimation() {
73917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        sAnimations.remove(this);
74017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mListeners != null) {
74117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            ArrayList<AnimatableListener> tmpListeners =
74217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    (ArrayList<AnimatableListener>) mListeners.clone();
74317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            for (AnimatableListener listener : tmpListeners) {
74417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                listener.onAnimationEnd(this);
74517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
74617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
74717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        mPlayingState = STOPPED;
74817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
74917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
75017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
75117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Called internally to start an animation by adding it to the active animations list. Must be
75217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * called on the UI thread.
75317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
75417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private void startAnimation() {
75517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        initAnimation();
75617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        sAnimations.add(this);
75717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mListeners != null) {
75817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            ArrayList<AnimatableListener> tmpListeners =
75917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    (ArrayList<AnimatableListener>) mListeners.clone();
76017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            for (AnimatableListener listener : tmpListeners) {
76117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                listener.onAnimationStart(this);
76217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
76317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
76417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
76517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
76617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
76717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Internal function called to process an animation frame on an animation that is currently
76817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * sleeping through its <code>startDelay</code> phase. The return value indicates whether it
76917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * should be woken up and put on the active animations queue.
77017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
77117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param currentTime The current animation time, used to calculate whether the animation
77217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * has exceeded its <code>startDelay</code> and should be started.
77317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @return True if the animation's <code>startDelay</code> has been exceeded and the animation
77417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * should be added to the set of active animations.
77517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
77617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private boolean delayedAnimationFrame(long currentTime) {
77717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (!mStartedDelay) {
77817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mStartedDelay = true;
77917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mDelayStartTime = currentTime;
78017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        } else {
78117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            long deltaTime = currentTime - mDelayStartTime;
78217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (deltaTime > mStartDelay) {
78317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                // startDelay ended - start the anim and record the
78417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                // mStartTime appropriately
78517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mStartTime = currentTime - (deltaTime - mStartDelay);
78617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mPlayingState = RUNNING;
78717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                return true;
78817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
78917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
79017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        return false;
79117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
79217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
79317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
79417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * This internal function processes a single animation frame for a given animation. The
79517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * currentTime parameter is the timing pulse sent by the handler, used to calculate the
79617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * elapsed duration, and therefore
79717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * the elapsed fraction, of the animation. The return value indicates whether the animation
79817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * should be ended (which happens when the elapsed time of the animation exceeds the
79917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * animation's duration, including the repeatCount).
80017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
80117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param currentTime The current time, as tracked by the static timing handler
80217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @return true if the animation's duration, including any repetitions due to
80317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>repeatCount</code> has been exceeded and the animation should be ended.
80417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
80517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private boolean animationFrame(long currentTime) {
80617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        boolean done = false;
80717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
80817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mPlayingState == STOPPED) {
80917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            mPlayingState = RUNNING;
8105d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase            if (mSeekTime < 0) {
8115d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase                mStartTime = currentTime;
8125d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase            } else {
8135d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase                mStartTime = currentTime - mSeekTime;
8145d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase                // Now that we're playing, reset the seek time
8155d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase                mSeekTime = -1;
8165d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase            }
81717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
81817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        switch (mPlayingState) {
81917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        case RUNNING:
8205d7b50b800b9898f5ca0b2b4d8b73ed6a4ee1749Chet Haase        case SEEKED:
82117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            float fraction = (float)(currentTime - mStartTime) / mDuration;
82217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (fraction >= 1f) {
82317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                if (mCurrentIteration < mRepeatCount || mRepeatCount == INFINITE) {
82417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // Time to repeat
82517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    if (mListeners != null) {
82617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        for (AnimatableListener listener : mListeners) {
82717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                            listener.onAnimationRepeat(this);
82817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        }
82917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
83017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    ++mCurrentIteration;
83117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    if (mRepeatMode == REVERSE) {
83217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                        mPlayingBackwards = mPlayingBackwards ? false : true;
83317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    }
83417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    // TODO: doesn't account for fraction going Wayyyyy over 1, like 2+
83517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    fraction = fraction - 1f;
83617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    mStartTime += mDuration;
83717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                } else {
83817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    done = true;
83917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                    fraction = Math.min(fraction, 1.0f);
84017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                }
84117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
84217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (mPlayingBackwards) {
84317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                fraction = 1f - fraction;
84417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
84517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            animateValue(fraction);
84617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            break;
84717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        case ENDED:
84817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            // The final value set on the target varies, depending on whether the animation
84917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            // was supposed to repeat an odd number of times
85017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            if (mRepeatCount > 0 && (mRepeatCount & 0x01) == 1) {
85117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                animateValue(0f);
85217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            } else {
85317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                animateValue(1f);
85417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
85517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            // Fall through to set done flag
85617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        case CANCELED:
85717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            done = true;
85817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            break;
85917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
86017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
86117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        return done;
86217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
86317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
86417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
86517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * This method is called with the elapsed fraction of the animation during every
86617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * animation frame. This function turns the elapsed fraction into an interpolated fraction
86717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * and then into an animated value (from the evaluator. The function is called mostly during
86817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * animation updates, but it is also called when the <code>end()</code>
86917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * function is called, to set the final value on the property.
87017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
87117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p>Overrides of this method must call the superclass to perform the calculation
87217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * of the animated value.</p>
87317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
87417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param fraction The elapsed fraction of the animation.
87517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
87617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    void animateValue(float fraction) {
87717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        fraction = mInterpolator.getInterpolation(fraction);
878602e4d3824bf8b9cb9f817375d195b969712176aChet Haase        int numValues = mValues.length;
879602e4d3824bf8b9cb9f817375d195b969712176aChet Haase        for (int i = 0; i < numValues; ++i) {
880602e4d3824bf8b9cb9f817375d195b969712176aChet Haase            mValues[i].calculateValue(fraction);
8813dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase        }
88217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        if (mUpdateListeners != null) {
88317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            int numListeners = mUpdateListeners.size();
88417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            for (int i = 0; i < numListeners; ++i) {
88517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase                mUpdateListeners.get(i).onAnimationUpdate(this);
88617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            }
88717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
88817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
88917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
89049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase    @Override
89149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase    public Animator clone() throws CloneNotSupportedException {
89249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        final Animator anim = (Animator) super.clone();
89349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        if (mUpdateListeners != null) {
89449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            ArrayList<AnimatorUpdateListener> oldListeners = mUpdateListeners;
89549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            anim.mUpdateListeners = new ArrayList<AnimatorUpdateListener>();
89649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            int numListeners = oldListeners.size();
89749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            for (int i = 0; i < numListeners; ++i) {
89849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                anim.mUpdateListeners.add(oldListeners.get(i));
89949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            }
90049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        }
90149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        anim.mSeekTime = -1;
90249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        anim.mPlayingBackwards = false;
90349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        anim.mCurrentIteration = 0;
90449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        anim.mInitialized = false;
90549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        anim.mPlayingState = STOPPED;
90649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        anim.mStartedDelay = false;
90749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        PropertyValuesHolder[] oldValues = mValues;
90849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        if (oldValues != null) {
90949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            int numValues = oldValues.length;
91049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            anim.mValues = new PropertyValuesHolder[numValues];
91149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            for (int i = 0; i < numValues; ++i) {
91249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                anim.mValues[i] = oldValues[i];
91349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            }
91449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            anim.mValuesMap = new HashMap<String, PropertyValuesHolder>(numValues);
91549afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            for (int i = 0; i < numValues; ++i) {
91649afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                PropertyValuesHolder valuesHolder = mValues[i];
91749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase                anim.mValuesMap.put(valuesHolder.getPropertyName(), valuesHolder);
91849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase            }
91949afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        }
92049afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        return anim;
92149afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase    }
92249afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase
92317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
92417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Implementors of this interface can add themselves as update listeners
92517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * to an <code>Animator</code> instance to receive callbacks on every animation
92617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * frame, after the current frame's values have been calculated for that
92717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>Animator</code>.
92817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
92917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public static interface AnimatorUpdateListener {
93017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        /**
93117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * <p>Notifies the occurrence of another frame of the animation.</p>
93217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         *
93317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         * @param animation The animation which was repeated.
93417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase         */
93517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        void onAnimationUpdate(Animator animation);
93617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
93717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
93817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase}