Target based animation that takes the value and velocity from the [AnimationState] as the starting condition, and animate to the [targetValue], using the [animationSpec].
animateToOnAnimationState
fun animateToOnAnimationState() {
@Composable
fun simpleAnimate(target: Float): Float {
// Create an AnimationState to be updated by the animation.
val animationState = remember { AnimationState(target) }
// Launch the suspend animation into the composition's CoroutineContext, and pass
// `target` to LaunchedEffect so that when`target` changes the old animation job is
// canceled, and a new animation is created with a new target.
LaunchedEffect(target) {
// This starts an animation that updates the animationState on each frame
animationState.animateTo(
targetValue = target,
// Use a low stiffness spring. This can be replaced with any type of `AnimationSpec`
animationSpec = spring(stiffness = Spring.StiffnessLow),
// If the previous animation was interrupted (i.e. not finished), configure the
// animation as a sequential animation to continue from the time the animation was
// interrupted.
sequentialAnimation = !animationState.isFinished,
)
// When the function above returns, the animation has finished.
}
// Return the value updated by the animation.
return animationState.value
}
}