How to set loop number of lottiefiles animation in android(Java)?
Asked Answered
M

4

7

I am facing a problem using Lottie files as an animation. I can not set loop number while after loading it is looping continuously but I want to set fixed loop number.

Activity XML

 <com.airbnb.lottie.LottieAnimationView
        android:layout_centerInParent="true"
        android:id="@+id/animation_view_1"
        android:layout_width="150dp"
        android:layout_height="150dp"
        app:lottie_autoPlay="true"
        app:lottie_loop="true" />

Activity Java

animationView.setVisibility(View.VISIBLE);
        animationView.setAnimation(fileName);
        animationView.loop(true);
        animationView.playAnimation();
Milano answered 18/7, 2019 at 5:48 Comment(0)
W
16

As animationView.loop(true); is deprecated. In addition to Phan Van Linh asnwer, Using .xml file

<com.airbnb.lottie.LottieAnimationView
        ...
        app:lottie_repeatCount="3"
        />

Using java you can use

animationView.setRepeatCount(LottieDrawable.INFINITE);// for Infinite loops

OR

animationView.setRepeatCount(3);// for 3 loops
Wifeless answered 18/7, 2019 at 6:15 Comment(2)
The Above answers are perfect but there is one more easier way, This is deprecated but it works for now animationView.loop(true);Terricolous
yup, for the future purpose we must not use animationView.loop(true);Wifeless
B
6

Try

<com.airbnb.lottie.LottieAnimationView
        ...
        app:lottie_repeatCount="3"
        />
Blowfish answered 18/7, 2019 at 5:58 Comment(0)
U
4

If you use jetpack compose:

  1. Get lottie composition from resources:
// Get lottie composition
val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.idea_anim))
  1. Create state of animation for composition, to set number of iterations use iterations = 3, for infinity we use iterations = LottieConstants.IterateForever:
// State of animation for composition
val progress by animateLottieCompositionAsState(
    composition = composition,
    iterations = LottieConstants.IterateForever
)
  1. Create LottieAnimation composable:
LottieAnimation(
    composition = composition,
    progress = { progress }
)

Full code:

@Composable
fun AnyScreen() {
    // Get lottie composition
    val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.idea_anim))

    // State of animation for composition
    val progress by animateLottieCompositionAsState(
        composition = composition,
        iterations = LottieConstants.IterateForever
    )

    LottieAnimation(
        composition = composition,
        progress = { progress }
    )
}
Underhung answered 3/9, 2022 at 22:41 Comment(0)
V
0

Also if you want to do programatically there is method setRepeatCount()

animationView.setRepeatCount(count)

/** * Sets how many times the animation should be repeated. If the repeat * count is 0, the animation is never repeated. If the repeat count is * greater than 0 or {@link LottieDrawable#INFINITE}, the repeat mode will be taken * into account. The repeat count is 0 by default. * * @param count the number of times the animation should be repeated */

public void setRepeatCount(int count) { lottieDrawable.setRepeatCount(count); }

Vaudeville answered 18/7, 2019 at 6:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.