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

AnimationKt

public final class AnimationKt


Summary

Public methods

static final @NonNull DecayAnimation<@NonNull Float, @NonNull AnimationVector1D>
DecayAnimation(
    @NonNull FloatDecayAnimationSpec animationSpec,
    float initialValue,
    float initialVelocity
)

DecayAnimation is an animation that slows down from initialVelocity as time goes on.

static final @NonNull TargetBasedAnimation<@NonNull T, @NonNull V>
<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,
    @NonNull T initialVelocity
)

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

static final @NonNull T
<T extends Object, V extends AnimationVector> getVelocityFromNanos(
    @NonNull Animation<@NonNull T, @NonNull V> receiver,
    long playTimeNanos
)

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

Public methods

DecayAnimation

public static final @NonNull DecayAnimation<@NonNull Float, @NonNull AnimationVector1DDecayAnimation(
    @NonNull FloatDecayAnimationSpec animationSpec,
    float initialValue,
    float initialVelocity
)

DecayAnimation is an animation that slows down from initialVelocity as time goes on. DecayAnimation is stateless, and it does not have any concept of lifecycle. It serves as an animation calculation engine that supports convenient query of value/velocity given a play time. To achieve that, DecayAnimation stores all the animation related information: initialValue, initialVelocity, decay animation spec.

Note: Unless there's a need to control the timing manually, it's generally recommended to use higher level animation APIs that build on top DecayAnimation, such as Animatable.animateDecay, animateDecay, etc.

Parameters
@NonNull FloatDecayAnimationSpec animationSpec

decay animation that will be used

float initialValue

starting value that will be passed to the decay animation

float initialVelocity

starting velocity for the decay animation, 0f by default

TargetBasedAnimation

public static final @NonNull TargetBasedAnimation<@NonNull T, @NonNull V> <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,
    @NonNull T initialVelocity
)

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

@NonNull T initialVelocity

the start velocity (of type T of the animation

getVelocityFromNanos

public static final @NonNull T <T extends Object, V extends AnimationVector> getVelocityFromNanos(
    @NonNull Animation<@NonNull T, @NonNull V> receiver,
    long playTimeNanos
)

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

Parameters
long playTimeNanos

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