VectorizedAnimationSpec

Interface

Common
@JvmDefaultWithCompatibility
public interface VectorizedAnimationSpec<V : AnimationVector>

VectorizedAnimationSpecs are stateless vector based animation specifications. They do not assume any starting/ending conditions. Nor do they manage a lifecycle. All it stores is the configuration that is particular to the type of the animation. easing and duration for VectorizedTweenSpecs, or spring constants for VectorizedSpringSpecs. Its stateless nature allows the same VectorizedAnimationSpec to be reused by a few different running animations with different starting and ending values. More importantly, it allows the system to reuse the same animation spec when the animation target changes in-flight.

Since VectorizedAnimationSpecs are stateless, it requires starting value/velocity and ending value to be passed in, along with playtime, to calculate the value or velocity at that time. Play time here is the progress of the animation in terms of milliseconds, where 0 means the start of the animation and getDurationNanos returns the play time for the end of the animation.

Note: For use cases where the starting values/velocity and ending values aren't expected to change, it is recommended to use Animation that caches these static values and hence does not require them to be supplied in the value/velocity calculation.

Functions

public fun getValueFromNanos(
        playTimeNanos: Long,
        initialValue: V,
        targetValue: V,
        initialVelocity: V,
    ): V

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

Parameters

playTimeNanostime since the start of the animation
initialValuestart value of the animation
targetValueend value of the animation
initialVelocitystart velocity of the animation
public fun getVelocityFromNanos(
        playTimeNanos: Long,
        initialValue: V,
        targetValue: V,
        initialVelocity: V,
    ): V

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

Parameters

playTimeNanostime since the start of the animation
initialValuestart value of the animation
targetValueend value of the animation
initialVelocitystart velocity of the animation
public fun getDurationNanos(initialValue: V, targetValue: V, initialVelocity: V): Long

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.

Parameters

initialValuestart value of the animation
targetValueend value of the animation
initialVelocitystart velocity of the animation
public fun getEndVelocity(initialValue: V, targetValue: V, initialVelocity: V): V

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 physics-based animations, end velocity is an AnimationVector of 0s.

Parameters

initialValuestart value of the animation
targetValueend value of the animation
initialVelocitystart velocity of the animation