Lottie Compose: how to loop the animation indefinitely
Asked Answered
M

3

13

I have the following Lottie animation that plays only once, how can I make it play indefinitely?

val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.search_animation))
LottieAnimation(composition = composition)
Malvie answered 16/8, 2022 at 9:16 Comment(0)
M
30

Update 2023-09-05
There is an overloaded version of the LottieAnimation composable that merges the LottieAnimation and animateLottieCompositionAsState parameters.

LottieAnimation(
    composition,
    iterations = LottieConstants.IterateForever,
)

Source: https://airbnb.io/lottie/#/android-compose?id=lottieanimation-overload

Old answer
You can tell Lottie to iterate an animation forever via the progress parameter:

val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.search_animation))
val progress by animateLottieCompositionAsState(composition = composition, iterations = LottieConstants.IterateForever)
LottieAnimation(
    composition = composition,
    progress = { progress },
)

Source: https://airbnb.io/lottie/#/android-compose?id=animatelottiecompositionasstate

Malvie answered 16/8, 2022 at 9:16 Comment(0)
O
3

just to aid those future wanderers that stumble upon this... These days it's changed to something more like this:

LottieAnimation(
    composition = composition,
    iterations = LottieConstants.IterateForever,
    ...
    ```
Oneness answered 3/9, 2023 at 13:18 Comment(0)
E
-4

you can just simply add app:lottie_loop="true" at your xml for your LottieAnimationView.

Emilieemiline answered 25/5, 2023 at 18:54 Comment(1)
The question is specifically for Compose and not for XML Views.Malvie

© 2022 - 2024 — McMap. All rights reserved.