How to animate or update Lottie progress
standalone-mainCompottie provides two primary ways to control animation progress, designed to be analogous to Jetpack Compose APIs. Both implement LottieAnimationState (which is a State<Float>).
Use animateLottieCompositionAsState() for simple animations
Use this when your animation is simple or is a direct function of other state properties. It is analogous to animate*AsState.
val progress by animateLottieCompositionAsState(composition)
// With iterations
val progress by animateLottieCompositionAsState(
composition,
iterations = Compottie.IterateForever,
)
// With clipping
val progress by animateLottieCompositionAsState(
composition,
clipSpec = LottieClipSpec.Progress(0.5f, 0.75f),
)Use LottieAnimatable for imperative control
Use this when you need to manually trigger animations (e.g., calling animate or snapTo inside a LaunchedEffect). It is analogous to Animatable.
val lottieAnimatable = rememberLottieAnimatable()
LaunchedEffect(composition) {
lottieAnimatable.animate(
composition,
iterations = Compottie.IterateForever,
clipSpec = LottieClipSpec.Progress(0.5f, 0.75f),
)
}// animateLottieCompositionAsState example
val progress by animateLottieCompositionAsState(composition)
// LottieAnimatable example
val lottieAnimatable = rememberLottieAnimatable()
LaunchedEffect(Unit) {
lottieAnimatable.animate(
composition,
iterations = Compottie.IterateForever,
clipSpec = LottieClipSpec.Progress(0.5f, 0.75f),
)
}