animateValueAsState

Composable Function

Common
@Composable
public fun <T, V : AnimationVector> animateValueAsState(
    targetValue: T,
    typeConverter: TwoWayConverter<T, V>,
    animationSpec: AnimationSpec<T> = remember { spring() },
    visibilityThreshold: T? = null,
    label: String = "ValueAnimation",
    finishedListener: ((T) -> Unit)? = null,
): State<T>

Fire-and-forget animation function for any value. This Composable function is overloaded for different parameter types such as Dp, Color, Offset, etc. When the provided targetValue is changed, the animation will run automatically. If there is already an animation in-flight when targetValue changes, the on-going animation will adjust course to animate towards the new target value.

animateValueAsState returns a State object. The value of the state object will continuously be updated by the animation until the animation finishes.

Note, animateValueAsState cannot be canceled/stopped without removing this composable function from the tree. See Animatable for cancelable animations.

data class MySize(val width: Dp, val height: Dp)

Parameters

targetValueTarget value of the animation
typeConverterA TwoWayConverter to convert from the animation value from and to an AnimationVector
animationSpecThe animation that will be used to change the value through time. Physics animation will be used by default.
visibilityThresholdAn optional threshold to define when the animation value can be considered close enough to the targetValue to end the animation.
labelAn optional label to differentiate from other animations in Android Studio.
finishedListenerAn optional end listener to get notified when the animation is finished.

Returns

A State object, the value of which is updated by animation.
Common

Deprecated animate*AsState APIs now have a new label parameter added.

@Composable
public fun <T, V : AnimationVector> animateValueAsState(
    targetValue: T,
    typeConverter: TwoWayConverter<T, V>,
    animationSpec: AnimationSpec<T> = remember { spring() },
    visibilityThreshold: T? = null,
    finishedListener: ((T) -> Unit)? = null,
): State<T>