{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

TargetBasedAnimation

public final class TargetBasedAnimation<T extends Object, V extends AnimationVector> implements Animation


This is a convenient animation wrapper class that works for all target based animations, i.e. animations that has a pre-defined end value, unlike decay.

It assumes that the starting value and velocity, as well as ending value do not change throughout the animation, and cache these values. This caching enables much more convenient query for animation value and velocity (where only playtime needs to be passed into the methods).

Note: When interruptions happen to the TargetBasedAnimation, a new instance should be created that use the current value and velocity as the starting conditions. This type of interruption handling is the default behavior for both Animatable and Transition. Consider using those APIs for the interruption handling, as well as built-in animation lifecycle management.

See also
Transition
updateTransition
Animatable

Summary

Public constructors

<T extends Object, V extends AnimationVector> TargetBasedAnimation(
    @NonNull AnimationSpec<@NonNull T> animationSpec,
    @NonNull TwoWayConverter<@NonNull T, @NonNull V> typeConverter,
    @NonNull T initialValue,
    @NonNull T targetValue,
    V initialVelocityVector
)

Creates a TargetBasedAnimation with the given start/end conditions of the animation, and the provided animationSpec.

Public methods

long

This amount of time in nanoseconds that the animation will run before it finishes

final @NonNull T

the start value of the animation

@NonNull T

the end value of the animation

@NonNull TwoWayConverter<@NonNull T, @NonNull V>

the TwoWayConverter that is used to convert animation type T from/to V

@NonNull T
getValueFromNanos(long playTimeNanos)

Returns the value of the animation at the given play time.

@NonNull V
getVelocityVectorFromNanos(long playTimeNanos)

Returns the velocity (in AnimationVector form) of the animation at the given play time.

boolean

Whether or not the Animation represents an infinite animation.

@NonNull String

Inherited methods

From androidx.compose.animation.core.Animation
boolean
isFinishedFromNanos(long playTimeNanos)

Returns whether the animation is finished at the given play time.

Public constructors

TargetBasedAnimation

public <T extends Object, V extends AnimationVector> TargetBasedAnimation(
    @NonNull AnimationSpec<@NonNull T> animationSpec,
    @NonNull TwoWayConverter<@NonNull T, @NonNull V> typeConverter,
    @NonNull T initialValue,
    @NonNull T targetValue,
    V initialVelocityVector
)

Creates a TargetBasedAnimation with the given start/end conditions of the animation, and the provided animationSpec.

The resulting Animation assumes that the start value and velocity, as well as end value do not change throughout the animation, and cache these values. This caching enables much more convenient query for animation value and velocity (where only playtime needs to be passed into the methods).

Note: When interruptions happen to the TargetBasedAnimation, a new instance should be created that use the current value and velocity as the starting conditions. This type of interruption handling is the default behavior for both Animatable and Transition. Consider using those APIs for the interruption handling, as well as built-in animation lifecycle management.

Parameters
@NonNull AnimationSpec<@NonNull T> animationSpec

the AnimationSpec that will be used to calculate value/velocity

@NonNull TwoWayConverter<@NonNull T, @NonNull V> typeConverter

the TwoWayConverter that is used to convert animation type T from/to V

@NonNull T initialValue

the start value of the animation

@NonNull T targetValue

the end value of the animation

V initialVelocityVector

the start velocity vector, null by default (meaning 0 velocity).

Public methods

getDurationNanos

public long getDurationNanos()

This amount of time in nanoseconds that the animation will run before it finishes

getInitialValue

public final @NonNullgetInitialValue()

the start value of the animation

getTargetValue

public @NonNullgetTargetValue()

the end value of the animation

getTypeConverter

public @NonNull TwoWayConverter<@NonNull T, @NonNull V> getTypeConverter()

the TwoWayConverter that is used to convert animation type T from/to V

getValueFromNanos

public @NonNullgetValueFromNanos(long playTimeNanos)

Returns the value of the animation at the given play time.

Parameters
long playTimeNanos

the play time that is used to determine the value of the animation.

getVelocityVectorFromNanos

public @NonNullgetVelocityVectorFromNanos(long playTimeNanos)

Returns the velocity (in AnimationVector form) of the animation at the given play time.

Parameters
long playTimeNanos

the play time that is used to calculate the velocity of the animation.

isInfinite

public boolean isInfinite()

Whether or not the Animation represents an infinite animation. That is, one that will not finish by itself, one that needs an external action to stop. For examples, an indeterminate progress bar, which will only stop when it is removed from the composition.

toString

public @NonNull String toString()