2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com
.actionbarsherlock
.internal
.nineoldandroids
.animation
;
19 import java
.util
.ArrayList
;
21 import android
.view
.animation
.Interpolator
;
24 * This is the superclass for classes which provide basic support for animations which can be
25 * started, ended, and have <code>AnimatorListeners</code> added to them.
27 public abstract class Animator
implements Cloneable
{
31 * The set of listeners to be sent events through the life of an animation.
33 ArrayList
<AnimatorListener
> mListeners
= null
;
36 * Starts this animation. If the animation has a nonzero startDelay, the animation will start
37 * running after that delay elapses. A non-delayed animation will have its initial
38 * value(s) set immediately, followed by calls to
39 * {@link AnimatorListener#onAnimationStart(Animator)} for any listeners of this animator.
41 * <p>The animation started by calling this method will be run on the thread that called
42 * this method. This thread should have a Looper on it (a runtime exception will be thrown if
43 * this is not the case). Also, if the animation will animate
44 * properties of objects in the view hierarchy, then the calling thread should be the UI
45 * thread for that view hierarchy.</p>
52 * Cancels the animation. Unlike {@link #end()}, <code>cancel()</code> causes the animation to
53 * stop in its tracks, sending an
54 * {@link android.animation.Animator.AnimatorListener#onAnimationCancel(Animator)} to
55 * its listeners, followed by an
56 * {@link android.animation.Animator.AnimatorListener#onAnimationEnd(Animator)} message.
58 * <p>This method must be called on the thread that is running the animation.</p>
60 public void cancel() {
64 * Ends the animation. This causes the animation to assign the end value of the property being
65 * animated, then calling the
66 * {@link android.animation.Animator.AnimatorListener#onAnimationEnd(Animator)} method on
69 * <p>This method must be called on the thread that is running the animation.</p>
75 * The amount of time, in milliseconds, to delay starting the animation after
76 * {@link #start()} is called.
78 * @return the number of milliseconds to delay running the animation
80 public abstract long getStartDelay();
83 * The amount of time, in milliseconds, to delay starting the animation after
84 * {@link #start()} is called.
86 * @param startDelay The amount of the delay, in milliseconds
88 public abstract void setStartDelay(long startDelay
);
92 * Sets the length of the animation.
94 * @param duration The length of the animation, in milliseconds.
96 public abstract Animator
setDuration(long duration
);
99 * Gets the length of the animation.
101 * @return The length of the animation, in milliseconds.
103 public abstract long getDuration();
106 * The time interpolator used in calculating the elapsed fraction of this animation. The
107 * interpolator determines whether the animation runs with linear or non-linear motion,
108 * such as acceleration and deceleration. The default value is
109 * {@link android.view.animation.AccelerateDecelerateInterpolator}
111 * @param value the interpolator to be used by this animation
113 public abstract void setInterpolator(/*Time*/Interpolator value
);
116 * Returns whether this Animator is currently running (having been started and gone past any
117 * initial startDelay period and not yet ended).
119 * @return Whether the Animator is running.
121 public abstract boolean isRunning();
124 * Returns whether this Animator has been started and not yet ended. This state is a superset
125 * of the state of {@link #isRunning()}, because an Animator with a nonzero
126 * {@link #getStartDelay() startDelay} will return true for {@link #isStarted()} during the
127 * delay phase, whereas {@link #isRunning()} will return true only after the delay phase
130 * @return Whether the Animator has been started and not yet ended.
132 public boolean isStarted() {
133 // Default method returns value for isRunning(). Subclasses should override to return a
139 * Adds a listener to the set of listeners that are sent events through the life of an
140 * animation, such as start, repeat, and end.
142 * @param listener the listener to be added to the current set of listeners for this animation.
144 public void addListener(AnimatorListener listener
) {
145 if (mListeners
== null
) {
146 mListeners
= new ArrayList
<AnimatorListener
>();
148 mListeners
.add(listener
);
152 * Removes a listener from the set listening to this animation.
154 * @param listener the listener to be removed from the current set of listeners for this
157 public void removeListener(AnimatorListener listener
) {
158 if (mListeners
== null
) {
161 mListeners
.remove(listener
);
162 if (mListeners
.size() == 0) {
168 * Gets the set of {@link android.animation.Animator.AnimatorListener} objects that are currently
169 * listening for events on this <code>Animator</code> object.
171 * @return ArrayList<AnimatorListener> The set of listeners.
173 public ArrayList
<AnimatorListener
> getListeners() {
178 * Removes all listeners from this object. This is equivalent to calling
179 * <code>getListeners()</code> followed by calling <code>clear()</code> on the
180 * returned list of listeners.
182 public void removeAllListeners() {
183 if (mListeners
!= null
) {
190 public Animator
clone() {
192 final Animator anim
= (Animator
) super.clone();
193 if (mListeners
!= null
) {
194 ArrayList
<AnimatorListener
> oldListeners
= mListeners
;
195 anim
.mListeners
= new ArrayList
<AnimatorListener
>();
196 int numListeners
= oldListeners
.size();
197 for (int i
= 0; i
< numListeners
; ++i
) {
198 anim
.mListeners
.add(oldListeners
.get(i
));
202 } catch (CloneNotSupportedException e
) {
203 throw new AssertionError();
208 * This method tells the object to use appropriate information to extract
209 * starting values for the animation. For example, a AnimatorSet object will pass
210 * this call to its child objects to tell them to set up the values. A
211 * ObjectAnimator object will use the information it has about its target object
212 * and PropertyValuesHolder objects to get the start values for its properties.
213 * An ValueAnimator object will ignore the request since it does not have enough
214 * information (such as a target object) to gather these values.
216 public void setupStartValues() {
220 * This method tells the object to use appropriate information to extract
221 * ending values for the animation. For example, a AnimatorSet object will pass
222 * this call to its child objects to tell them to set up the values. A
223 * ObjectAnimator object will use the information it has about its target object
224 * and PropertyValuesHolder objects to get the start values for its properties.
225 * An ValueAnimator object will ignore the request since it does not have enough
226 * information (such as a target object) to gather these values.
228 public void setupEndValues() {
232 * Sets the target object whose property will be animated by this animation. Not all subclasses
233 * operate on target objects (for example, {@link ValueAnimator}, but this method
234 * is on the superclass for the convenience of dealing generically with those subclasses
235 * that do handle targets.
237 * @param target The object being animated
239 public void setTarget(Object target
) {
243 * <p>An animation listener receives notifications from an animation.
244 * Notifications indicate animation related events, such as the end or the
245 * repetition of the animation.</p>
247 public static interface AnimatorListener
{
249 * <p>Notifies the start of the animation.</p>
251 * @param animation The started animation.
253 void onAnimationStart(Animator animation
);
256 * <p>Notifies the end of the animation. This callback is not invoked
257 * for animations with repeat count set to INFINITE.</p>
259 * @param animation The animation which reached its end.
261 void onAnimationEnd(Animator animation
);
264 * <p>Notifies the cancellation of the animation. This callback is not invoked
265 * for animations with repeat count set to INFINITE.</p>
267 * @param animation The animation which was canceled.
269 void onAnimationCancel(Animator animation
);
272 * <p>Notifies the repetition of the animation.</p>
274 * @param animation The animation which was repeated.
276 void onAnimationRepeat(Animator animation
);