Animatable

class Animatable<T, V : AnimationVector>(initialValue: T, val typeConverter: TwoWayConverter<T, V>, visibilityThreshold: T? = null, val label: String = "Animatable")

Animatable is a value holder that automatically animates its value when the value is changed via animateTo. If animateTo is invoked during an ongoing value change animation, a new animation will transition Animatable from its current value (i.e. value at the point of interruption) to the new targetValue. This ensures that the value change is always continuous using animateTo. If a spring animation (e.g. default animation) is used with animateTo, the velocity change will guarantee to be continuous as well.

Unlike AnimationState, Animatable ensures mutual exclusiveness on its animations. To achieve this, when a new animation is started via animateTo (or animateDecay), any ongoing animation will be canceled via a CancellationException.

Parameters

initialValue

initial value of the animatable value holder

typeConverter

A two-way converter that converts the given type T from and to AnimationVector

visibilityThreshold

Threshold at which the animation may round off to its target value.

label

An optional label for differentiating this animation from others.

See also

Constructors

Link copied to clipboard
constructor(initialValue: T, typeConverter: TwoWayConverter<T, V>, visibilityThreshold: T? = null, label: String = "Animatable")

Properties

Link copied to clipboard

Indicates whether the animation is running.

Link copied to clipboard
Link copied to clipboard

Lower bound of the animation, null by default (meaning no lower bound). Bounds can be changed using updateBounds.

Link copied to clipboard

The target of the current animation. If the animation finishes un-interrupted, it will reach this target value.

Link copied to clipboard
Link copied to clipboard

Upper bound of the animation, null by default (meaning no upper bound). Bounds can be changed using updateBounds.

Link copied to clipboard
val value: T

Current value of the animation.

Link copied to clipboard
val velocity: T

Returns the velocity, converted from velocityVector.

Link copied to clipboard

Velocity vector of the animation (in the form of AnimationVector.

Functions

Link copied to clipboard
suspend fun animateDecay(initialVelocity: T, animationSpec: DecayAnimationSpec<T>, block: Animatable<T, V>.() -> Unit? = null): AnimationResult<T, V>

Start a decay animation (i.e. an animation that slows down from the given initialVelocity starting at current Animatable.value until the velocity reaches 0. If there's already an ongoing animation, the animation in-flight will be immediately cancelled. Decay animation is often used after a fling gesture.

Link copied to clipboard
suspend fun animateTo(targetValue: T, animationSpec: AnimationSpec<T> = defaultSpringSpec, initialVelocity: T = velocity, block: Animatable<T, V>.() -> Unit? = null): AnimationResult<T, V>

Starts an animation to animate from value to the provided targetValue. If there is already an animation in-flight, this method will cancel the ongoing animation before starting a new animation continuing the current value and velocity. It's recommended to set the optional initialVelocity only when animateTo is used immediately after a fling. In most of the other cases, altering velocity would result in visual discontinuity.

Link copied to clipboard
fun asState(): State<T>

Returns a State representing the current value of this animation. This allows hoisting the animation's current value without causing unnecessary recompositions when the value changes.

Link copied to clipboard
suspend fun snapTo(targetValue: T)

Sets the current value to the target value, without any animation. This will also cancel any on-going animation with a CancellationException. This function will return after canceling any on-going animation and updating the Animatable.value and Animatable.targetValue to the provided targetValue.

Link copied to clipboard
suspend fun stop()

Stops any on-going animation with a CancellationException.

Link copied to clipboard
fun updateBounds(lowerBound: T? = this.lowerBound, upperBound: T? = this.upperBound)

Updates either lowerBound or upperBound, or both. This will update Animatable.lowerBound and/or Animatable.upperBound accordingly after a check to ensure the provided lowerBound is no greater than upperBound in any dimension.