java.lang.Object | ||
↳ | android.animation.Animator | |
↳ | android.animation.ValueAnimator |
Known Direct Subclasses |
This class provides a simple timing engine for running animations which calculate animated values and set them on target objects.
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
setInterpolator(TimeInterpolator)
.
For more information about animating with ValueAnimator
, read the
Property
Animation developer guide.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
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 . |
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | INFINITE | This value used used with the setRepeatCount(int) property to repeat
the animation indefinitely. |
|||||||||
int | RESTART | When the animation reaches the end and repeatCount is INFINITE
or a positive value, the animation restarts from the beginning. |
|||||||||
int | REVERSE | When the animation reaches the end and repeatCount is INFINITE
or a positive value, the animation reverses direction on every iteration. |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Creates a new ValueAnimator object.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Adds a listener to the set of listeners that are sent update events through the life of
an animation.
| |||||||||||
Cancels the animation.
| |||||||||||
Creates and returns a copy of this
Object . | |||||||||||
Ends the animation.
| |||||||||||
Returns the current animation fraction, which is the elapsed/interpolated fraction used in
the most recent frame update on the animation.
| |||||||||||
The most recent value calculated by this
ValueAnimator when there is just one
property being animated. | |||||||||||
The most recent value calculated by this
ValueAnimator for propertyName . | |||||||||||
Gets the current position of the animation in time, which is equal to the current
time minus the time that the animation started.
| |||||||||||
Gets the length of the animation.
| |||||||||||
The amount of time, in milliseconds, between each frame of the animation.
| |||||||||||
Returns the timing interpolator that this ValueAnimator uses.
| |||||||||||
Defines how many times the animation should repeat.
| |||||||||||
Defines what this animation should do when it reaches the end.
| |||||||||||
The amount of time, in milliseconds, to delay starting the animation after
start() is called. | |||||||||||
Returns the values that this ValueAnimator animates between.
| |||||||||||
Returns whether this Animator is currently running (having been started and gone past any
initial startDelay period and not yet ended).
| |||||||||||
Returns whether this Animator has been started and not yet ended.
| |||||||||||
Constructs and returns a ValueAnimator that animates between float values.
| |||||||||||
Constructs and returns a ValueAnimator that animates between int values.
| |||||||||||
Constructs and returns a ValueAnimator that animates between Object values.
| |||||||||||
Constructs and returns a ValueAnimator that animates between the values
specified in the PropertyValuesHolder objects.
| |||||||||||
Pauses a running animation.
| |||||||||||
Removes all listeners from the set listening to frame updates for this animation.
| |||||||||||
Removes a listener from the set listening to frame updates for this animation.
| |||||||||||
Resumes a paused animation, causing the animator to pick up where it left off
when it was paused.
| |||||||||||
Plays the ValueAnimator in reverse.
| |||||||||||
Sets the position of the animation to the specified point in time.
| |||||||||||
Sets the length of the animation.
| |||||||||||
The type evaluator to be used when calculating the animated values of this animation.
| |||||||||||
Sets float values that will be animated between.
| |||||||||||
The amount of time, in milliseconds, between each frame of the animation.
| |||||||||||
Sets int values that will be animated between.
| |||||||||||
The time interpolator used in calculating the elapsed fraction of this animation.
| |||||||||||
Sets the values to animate between for this animation.
| |||||||||||
Sets how many times the animation should be repeated.
| |||||||||||
Defines what this animation should do when it reaches the end.
| |||||||||||
The amount of time, in milliseconds, to delay starting the animation after
start() is called. | |||||||||||
Sets the values, per property, being animated between.
| |||||||||||
Starts this animation.
| |||||||||||
Returns a string containing a concise, human-readable description of this
object.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.animation.Animator
| |||||||||||
From class
java.lang.Object
|
This value used used with the setRepeatCount(int)
property to repeat
the animation indefinitely.
When the animation reaches the end and repeatCount
is INFINITE
or a positive value, the animation restarts from the beginning.
When the animation reaches the end and repeatCount
is INFINITE
or a positive value, the animation reverses direction on every iteration.
Creates a new ValueAnimator object. This default constructor is primarily for use internally; the factory methods which take parameters are more generally useful.
Adds a listener to the set of listeners that are sent update events through the life of an animation. This method is called on all listeners for every frame of the animation, after the values for the animation have been calculated.
listener | the listener to be added to the current set of listeners for this animation. |
---|
Cancels the animation. Unlike end()
, cancel()
causes the animation to
stop in its tracks, sending an
onAnimationCancel(Animator)
to
its listeners, followed by an
onAnimationEnd(Animator)
message.
This method must be called on the thread that is running the animation.
Creates and returns a copy of this Object
. The default
implementation returns a so-called "shallow" copy: It creates a new
instance of the same class and then copies the field values (including
object references) from this instance to the new instance. A "deep" copy,
in contrast, would also recursively clone nested objects. A subclass that
needs to implement this kind of cloning should call super.clone()
to create the new instance and then create deep copies of the nested,
mutable objects.
Ends the animation. This causes the animation to assign the end value of the property being
animated, then calling the
onAnimationEnd(Animator)
method on
its listeners.
This method must be called on the thread that is running the animation.
Returns the current animation fraction, which is the elapsed/interpolated fraction used in the most recent frame update on the animation.
The most recent value calculated by this 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 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.
The most recent value calculated by this ValueAnimator
for propertyName
.
The main purpose for this read-only property is to retrieve the value from the
ValueAnimator
during a call to
onAnimationUpdate(ValueAnimator)
, which
is called during each animation frame, immediately after the value is calculated.
ValueAnimator
.
Gets the current position of the animation in time, which is equal to the current time minus the time that the animation started. An animation that is not yet started will return a value of zero.
Gets the length of the animation. The default duration is 300 milliseconds.
The amount of time, in milliseconds, between each frame of the animation. This is a requested time that the animation will attempt to honor, but the actual delay between frames may be different, depending on system load and capabilities. This is a static function because the same delay will be applied to all animations, since they are all run off of a single timing loop. The frame delay may be ignored when the animation system uses an external timing source, such as the display refresh rate (vsync), to govern animations.
Returns the timing interpolator that this ValueAnimator uses.
Defines how many times the animation should repeat. The default value is 0.
INFINITE
The amount of time, in milliseconds, to delay starting the animation after
start()
is called.
Returns the values that this ValueAnimator animates between. These values are stored in PropertyValuesHolder objects, even if the ValueAnimator was created with a simple list of value objects instead.
Returns whether this Animator is currently running (having been started and gone past any initial startDelay period and not yet ended).
Returns whether this Animator has been started and not yet ended. This state is a superset
of the state of isRunning()
, because an Animator with a nonzero
startDelay
will return true for isStarted()
during the
delay phase, whereas isRunning()
will return true only after the delay phase
is complete.
Constructs and returns a ValueAnimator that animates between float values. A single value implies that that value is the one being animated to. However, this is not typically useful in a ValueAnimator object because there is no way for the object to determine the starting value for the animation (unlike ObjectAnimator, which can derive that value from the target object and property being animated). Therefore, there should typically be two or more values.
values | A set of values that the animation will animate between over time. |
---|
Constructs and returns a ValueAnimator that animates between int values. A single value implies that that value is the one being animated to. However, this is not typically useful in a ValueAnimator object because there is no way for the object to determine the starting value for the animation (unlike ObjectAnimator, which can derive that value from the target object and property being animated). Therefore, there should typically be two or more values.
values | A set of values that the animation will animate between over time. |
---|
Constructs and returns a ValueAnimator that animates between Object values. A single value implies that that value is the one being animated to. However, this is not typically useful in a ValueAnimator object because there is no way for the object to determine the starting value for the animation (unlike ObjectAnimator, which can derive that value from the target object and property being animated). Therefore, there should typically be two or more 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. |
Constructs and returns a ValueAnimator that animates between the values specified in the PropertyValuesHolder objects.
values | A set of PropertyValuesHolder objects whose values will be animated between over time. |
---|
Removes all listeners from the set listening to frame updates for this animation.
Removes a listener from the set listening to frame updates for this animation.
listener | the listener to be removed from the current set of update listeners for this animation. |
---|
Resumes a paused animation, causing the animator to pick up where it left off when it was paused. This method should only be called on the same thread on which the animation was started. Calls to resume() on an animator that is not currently paused will be ignored.
Plays the ValueAnimator in reverse. If the animation is already running, it will stop itself and play backwards from the point reached when reverse was called. If the animation is not currently running, then it will start from the end and play backwards. This behavior is only set for the current animation; future playing of the animation will use the default behavior of playing forward.
Sets the position of the animation to the specified point in time. This time should be between 0 and the total duration of the animation, including any repetition. If the animation has not yet been started, then it will not advance forward after it is set to this time; it will simply set the time to this value and perform any appropriate actions based on that time. If the animation is already running, then setCurrentPlayTime() will set the current playing time to this value and continue playing from that point.
playTime | The time, in milliseconds, to which the animation is advanced or rewound. |
---|
Sets the length of the animation. The default duration is 300 milliseconds.
duration | The length of the animation, in milliseconds. This value cannot be negative. |
---|
ValueAnimator.ofInt(0, 10).setDuration(500).start()
.
The type evaluator to be used when calculating the animated values of this animation.
The system will automatically assign a float or int evaluator based on the type
of 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 animation |
---|
Sets float values that will be animated between. A single value implies that that value is the one being animated to. However, this is not typically useful in a ValueAnimator object because there is no way for the object to determine the starting value for the animation (unlike ObjectAnimator, which can derive that value from the target object and property being animated). Therefore, there should typically be two or more 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. |
---|
The amount of time, in milliseconds, between each frame of the animation. This is a requested time that the animation will attempt to honor, but the actual delay between frames may be different, depending on system load and capabilities. This is a static function because the same delay will be applied to all animations, since they are all run off of a single timing loop. The frame delay may be ignored when the animation system uses an external timing source, such as the display refresh rate (vsync), to govern animations.
frameDelay | the requested time between frames, in milliseconds |
---|
Sets int values that will be animated between. A single value implies that that value is the one being animated to. However, this is not typically useful in a ValueAnimator object because there is no way for the object to determine the starting value for the animation (unlike ObjectAnimator, which can derive that value from the target object and property being animated). Therefore, there should typically be two or more 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. |
---|
The time interpolator used in calculating the elapsed fraction of this animation. The
interpolator determines whether the animation runs with linear or non-linear motion,
such as acceleration and deceleration. The default value is
AccelerateDecelerateInterpolator
value | the interpolator to be used by this animation. A value of null
will result in linear interpolation.
|
---|
Sets the values to animate between for this animation. A single value implies that that value is the one being animated to. However, this is not typically useful in a ValueAnimator object because there is no way for the object to determine the starting value for the animation (unlike ObjectAnimator, which can derive that value from the target object and property being animated). Therefore, there should typically be two or more 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. |
---|
Sets how many times the animation should be repeated. If the repeat
count is 0, the animation is never repeated. If the repeat count is
greater than 0 or 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 repeated |
---|
The amount of time, in milliseconds, to delay starting the animation after
start()
is called.
startDelay | The amount of the delay, in milliseconds |
---|
Sets the values, per property, being animated between. This function is called internally by the constructors of ValueAnimator that take a list of values. But a ValueAnimator can be constructed without values and this method can be called to set the values manually instead.
values | The set of values, per property, being animated between. |
---|
Starts this animation. If the animation has a nonzero startDelay, the animation will start
running after that delay elapses. A non-delayed animation will have its initial
value(s) set immediately, followed by calls to
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.
Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:
getClass().getName() + '@' + Integer.toHexString(hashCode())
See Writing a useful
toString
method
if you intend implementing your own toString
method.