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

FloatSpringSpec

public final class FloatSpringSpec implements FloatAnimationSpec


FloatSpringSpec animation uses a spring animation to animate a Float value. Its configuration can be tuned via adjusting the spring parameters, namely damping ratio and stiffness.

Summary

Public constructors

FloatSpringSpec(
    float dampingRatio,
    float stiffness,
    float visibilityThreshold
)

Public methods

final float

damping ratio of the spring.

long
getDurationNanos(
    float initialValue,
    float targetValue,
    float initialVelocity
)

Calculates the duration of an animation.

float
getEndVelocity(
    float initialValue,
    float targetValue,
    float initialVelocity
)

Calculates the end velocity of the animation with the provided start/end values, and start velocity.

final float

Stiffness of the spring.

float
getValueFromNanos(
    long playTimeNanos,
    float initialValue,
    float targetValue,
    float initialVelocity
)

Calculates the value of the animation at given the playtime, with the provided start/end values, and start velocity.

float
getVelocityFromNanos(
    long playTimeNanos,
    float initialValue,
    float targetValue,
    float initialVelocity
)

Calculates the velocity of the animation at given the playtime, with the provided start/end values, and start velocity.

Inherited methods

From androidx.compose.animation.core.FloatAnimationSpec

Public constructors

FloatSpringSpec

public FloatSpringSpec(
    float dampingRatio,
    float stiffness,
    float visibilityThreshold
)
Parameters
float dampingRatio

damping ratio of the spring. Defaults to Spring.DampingRatioNoBouncy

float stiffness

Stiffness of the spring. Defaults to Spring.StiffnessMedium

float visibilityThreshold

The value threshold such that the animation is no longer significant. e.g. 1px for translation animations. Defaults to Spring.DefaultDisplacementThreshold

Public methods

getDampingRatio

public final float getDampingRatio()

damping ratio of the spring. Defaults to Spring.DampingRatioNoBouncy

getDurationNanos

public long getDurationNanos(
    float initialValue,
    float targetValue,
    float initialVelocity
)

Calculates the duration of an animation. For duration-based animations, this will return the pre-defined duration. For physics-based animations, the duration will be estimated based on the physics configuration (such as spring stiffness, damping ratio, visibility threshold) as well as the initialValue, targetValue values, and initialVelocity.

Note: this may be a computation that is expensive - especially with spring based animations

Parameters
float initialValue

start value of the animation

float targetValue

end value of the animation

float initialVelocity

start velocity of the animation

getEndVelocity

public float getEndVelocity(
    float initialValue,
    float targetValue,
    float initialVelocity
)

Calculates the end velocity of the animation with the provided start/end values, and start velocity. For duration-based animations, end velocity will be the velocity of the animation at the duration time. This is also the default assumption. However, for spring animations, the transient trailing velocity will be snapped to zero.

Parameters
float initialValue

start value of the animation

float targetValue

end value of the animation

float initialVelocity

start velocity of the animation

getStiffness

public final float getStiffness()

Stiffness of the spring. Defaults to Spring.StiffnessMedium

getValueFromNanos

public float getValueFromNanos(
    long playTimeNanos,
    float initialValue,
    float targetValue,
    float initialVelocity
)

Calculates the value of the animation at given the playtime, with the provided start/end values, and start velocity.

Parameters
long playTimeNanos

time since the start of the animation

float initialValue

start value of the animation

float targetValue

end value of the animation

float initialVelocity

start velocity of the animation

getVelocityFromNanos

public float getVelocityFromNanos(
    long playTimeNanos,
    float initialValue,
    float targetValue,
    float initialVelocity
)

Calculates the velocity of the animation at given the playtime, with the provided start/end values, and start velocity.

Parameters
long playTimeNanos

time since the start of the animation

float initialValue

start value of the animation

float targetValue

end value of the animation

float initialVelocity

start velocity of the animation