public class ValueAnimator extends Animator
There is a single timing pulse that all animations use. It runs in a custom handler to ensure that property changes happen on the UI thread.
By default, ValueAnimator uses non-linear time interpolation, via the
AccelerateDecelerateInterpolator
class, which accelerates into and decelerates
out of an animation. This behavior can be changed by calling
ValueAnimator#setInterpolator(TimeInterpolator)
.
Modifier and Type | Class and Description |
---|---|
static interface |
ValueAnimator.AnimatorUpdateListener
Implementors of this interface can add themselves as update listeners
to an
ValueAnimator instance to receive callbacks on every animation
frame, after the current frame's values have been calculated for that
ValueAnimator . |
Animator.AnimatorListener
Modifier and Type | Field and Description |
---|---|
static int |
INFINITE
This value used used with the
setRepeatCount(int) property to repeat
the animation indefinitely. |
static int |
RESTART
When the animation reaches the end and
repeatCount is INFINITE
or a positive value, the animation restarts from the beginning. |
static int |
REVERSE
When the animation reaches the end and
repeatCount is INFINITE
or a positive value, the animation reverses direction on every iteration. |
Constructor and Description |
---|
ValueAnimator()
Creates a new ValueAnimator object.
|
Modifier and Type | Method and Description |
---|---|
void |
addUpdateListener(ValueAnimator.AnimatorUpdateListener listener)
Adds a listener to the set of listeners that are sent update events through the life of
an animation.
|
void |
cancel()
Cancels the animation.
|
static void |
clearAllAnimations()
Clear all animations on this thread, without canceling or ending them.
|
ValueAnimator |
clone() |
void |
end()
Ends the animation.
|
float |
getAnimatedFraction()
Returns the current animation fraction, which is the elapsed/interpolated fraction used in
the most recent frame update on the animation.
|
Object |
getAnimatedValue()
The most recent value calculated by this
ValueAnimator when there is just one
property being animated. |
Object |
getAnimatedValue(String propertyName)
The most recent value calculated by this
ValueAnimator for propertyName . |
static int |
getCurrentAnimationsCount()
Return the number of animations currently running.
|
long |
getCurrentPlayTime()
Gets the current position of the animation in time, which is equal to the current
time minus the time that the animation started.
|
long |
getDuration()
Gets the length of the animation.
|
static long |
getFrameDelay()
The amount of time, in milliseconds, between each frame of the animation.
|
android.view.animation.Interpolator |
getInterpolator()
Returns the timing interpolator that this ValueAnimator uses.
|
int |
getRepeatCount()
Defines how many times the animation should repeat.
|
int |
getRepeatMode()
Defines what this animation should do when it reaches the end.
|
long |
getStartDelay()
The amount of time, in milliseconds, to delay starting the animation after
start() is called. |
PropertyValuesHolder[] |
getValues()
Returns the values that this ValueAnimator animates between.
|
boolean |
isRunning()
Returns whether this Animator is currently running (having been started and gone past any
initial startDelay period and not yet ended).
|
boolean |
isStarted()
Returns whether this Animator has been started and not yet ended.
|
static ValueAnimator |
ofFloat(float... values)
Constructs and returns a ValueAnimator that animates between float values.
|
static ValueAnimator |
ofInt(int... values)
Constructs and returns a ValueAnimator that animates between int values.
|
static ValueAnimator |
ofObject(TypeEvaluator evaluator,
Object... values)
Constructs and returns a ValueAnimator that animates between Object values.
|
static ValueAnimator |
ofPropertyValuesHolder(PropertyValuesHolder... values)
Constructs and returns a ValueAnimator that animates between the values
specified in the PropertyValuesHolder objects.
|
void |
removeAllUpdateListeners()
Removes all listeners from the set listening to frame updates for this animation.
|
void |
removeUpdateListener(ValueAnimator.AnimatorUpdateListener listener)
Removes a listener from the set listening to frame updates for this animation.
|
void |
reverse()
Plays the ValueAnimator in reverse.
|
void |
setCurrentPlayTime(long playTime)
Sets the position of the animation to the specified point in time.
|
ValueAnimator |
setDuration(long duration)
Sets the length of the animation.
|
void |
setEvaluator(TypeEvaluator value)
The type evaluator to be used when calculating the animated values of this animation.
|
void |
setFloatValues(float... values)
Sets float values that will be animated between.
|
static void |
setFrameDelay(long frameDelay)
The amount of time, in milliseconds, between each frame of the animation.
|
void |
setInterpolator(android.view.animation.Interpolator value)
The time interpolator used in calculating the elapsed fraction of this animation.
|
void |
setIntValues(int... values)
Sets int values that will be animated between.
|
void |
setObjectValues(Object... values)
Sets the values to animate between for this animation.
|
void |
setRepeatCount(int value)
Sets how many times the animation should be repeated.
|
void |
setRepeatMode(int value)
Defines what this animation should do when it reaches the end.
|
void |
setStartDelay(long startDelay)
The amount of time, in milliseconds, to delay starting the animation after
start() is called. |
void |
setValues(PropertyValuesHolder... values)
Sets the values, per property, being animated between.
|
void |
start()
Starts this animation.
|
String |
toString() |
addListener, getListeners, removeAllListeners, removeListener, setTarget, setupEndValues, setupStartValues
public static final int RESTART
repeatCount
is INFINITE
or a positive value, the animation restarts from the beginning.public static final int REVERSE
repeatCount
is INFINITE
or a positive value, the animation reverses direction on every iteration.public static final int INFINITE
setRepeatCount(int)
property to repeat
the animation indefinitely.public ValueAnimator()
public static ValueAnimator ofInt(int... values)
values
- A set of values that the animation will animate between over time.public static ValueAnimator ofFloat(float... values)
values
- A set of values that the animation will animate between over time.public static ValueAnimator ofPropertyValuesHolder(PropertyValuesHolder... values)
values
- A set of PropertyValuesHolder objects whose values will be animated
between over time.public static ValueAnimator ofObject(TypeEvaluator evaluator, Object... values)
Since ValueAnimator does not know how to animate between arbitrary Objects, this factory method also takes a TypeEvaluator object that the ValueAnimator will use to perform that interpolation.
evaluator
- A TypeEvaluator that will be called on each animation frame to
provide the ncessry interpolation between the Object values to derive the animated
value.values
- A set of values that the animation will animate between over time.public void setIntValues(int... values)
If there are already multiple sets of values defined for this ValueAnimator via more than one PropertyValuesHolder object, this method will set the values for the first of those objects.
values
- A set of values that the animation will animate between over time.public void setFloatValues(float... values)
If there are already multiple sets of values defined for this ValueAnimator via more than one PropertyValuesHolder object, this method will set the values for the first of those objects.
values
- A set of values that the animation will animate between over time.public void setObjectValues(Object... values)
If there are already multiple sets of values defined for this ValueAnimator via more than one PropertyValuesHolder object, this method will set the values for the first of those objects.
There should be a TypeEvaluator set on the ValueAnimator that knows how to interpolate between these value objects. ValueAnimator only knows how to interpolate between the primitive types specified in the other setValues() methods.
values
- The set of values to animate between.public void setValues(PropertyValuesHolder... values)
values
- The set of values, per property, being animated between.public PropertyValuesHolder[] getValues()
public ValueAnimator setDuration(long duration)
setDuration
in class Animator
duration
- The length of the animation, in milliseconds. This value cannot
be negative.ValueAnimator.ofInt(0, 10).setDuration(500).start()
.public long getDuration()
getDuration
in class Animator
public void setCurrentPlayTime(long playTime)
playTime
- The time, in milliseconds, to which the animation is advanced or rewound.public long getCurrentPlayTime()
public long getStartDelay()
start()
is called.getStartDelay
in class Animator
public void setStartDelay(long startDelay)
start()
is called.setStartDelay
in class Animator
startDelay
- The amount of the delay, in millisecondspublic static long getFrameDelay()
public static void setFrameDelay(long frameDelay)
frameDelay
- the requested time between frames, in millisecondspublic Object getAnimatedValue()
ValueAnimator
when there is just one
property being animated. This value is only sensible while the animation is running. The main
purpose for this read-only property is to retrieve the value from the ValueAnimator
during a call to ValueAnimator.AnimatorUpdateListener.onAnimationUpdate(ValueAnimator)
, which
is called during each animation frame, immediately after the value is calculated.ValueAnimator
for
the single property being animated. If there are several properties being animated
(specified by several PropertyValuesHolder objects in the constructor), this function
returns the animated value for the first of those objects.public Object getAnimatedValue(String propertyName)
ValueAnimator
for propertyName
.
The main purpose for this read-only property is to retrieve the value from the
ValueAnimator
during a call to
ValueAnimator.AnimatorUpdateListener.onAnimationUpdate(ValueAnimator)
, which
is called during each animation frame, immediately after the value is calculated.ValueAnimator
.public void setRepeatCount(int value)
INFINITE
, the repeat mode will be taken
into account. The repeat count is 0 by default.value
- the number of times the animation should be repeatedpublic int getRepeatCount()
INFINITE
public void setRepeatMode(int value)
public int getRepeatMode()
public void addUpdateListener(ValueAnimator.AnimatorUpdateListener listener)
listener
- the listener to be added to the current set of listeners for this animation.public void removeAllUpdateListeners()
public void removeUpdateListener(ValueAnimator.AnimatorUpdateListener listener)
listener
- the listener to be removed from the current set of update listeners
for this animation.public void setInterpolator(android.view.animation.Interpolator value)
AccelerateDecelerateInterpolator
setInterpolator
in class Animator
value
- the interpolator to be used by this animation. A value of null
will result in linear interpolation.public android.view.animation.Interpolator getInterpolator()
public void setEvaluator(TypeEvaluator value)
startValue
and endValue
in the constructor. But if these values
are not one of these primitive types, or if different evaluation is desired (such as is
necessary with int values that represent colors), a custom evaluator needs to be assigned.
For example, when running an animation on color values, the ArgbEvaluator
should be used to get correct RGB color interpolation.
If this ValueAnimator has only one set of values being animated between, this evaluator will be used for that set. If there are several sets of values being animated, which is the case if PropertyValuesHOlder objects were set on the ValueAnimator, then the evaluator is assigned just to the first PropertyValuesHolder object.
value
- the evaluator to be used this animationpublic void start()
Animator
Animator.AnimatorListener.onAnimationStart(Animator)
for any listeners of this animator.
The animation started by calling this method will be run on the thread that called this method. This thread should have a Looper on it (a runtime exception will be thrown if this is not the case). Also, if the animation will animate properties of objects in the view hierarchy, then the calling thread should be the UI thread for that view hierarchy.
public void cancel()
Animator
Animator.end()
, cancel()
causes the animation to
stop in its tracks, sending an
android.animation.Animator.AnimatorListener#onAnimationCancel(Animator)
to
its listeners, followed by an
android.animation.Animator.AnimatorListener#onAnimationEnd(Animator)
message.
This method must be called on the thread that is running the animation.
public void end()
Animator
android.animation.Animator.AnimatorListener#onAnimationEnd(Animator)
method on
its listeners.
This method must be called on the thread that is running the animation.
public boolean isRunning()
Animator
public boolean isStarted()
Animator
Animator.isRunning()
, because an Animator with a nonzero
startDelay
will return true for Animator.isStarted()
during the
delay phase, whereas Animator.isRunning()
will return true only after the delay phase
is complete.public void reverse()
public float getAnimatedFraction()
public ValueAnimator clone()
public static int getCurrentAnimationsCount()
public static void clearAllAnimations()
Copyright © 2011–2014. All rights reserved.