Reputation: 186
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();
Upvotes: 7
Views: 16625
Reputation: 1986
If you use jetpack compose:
// Get lottie composition
val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.idea_anim))
iterations = 3
, for infinity we use iterations = LottieConstants.IterateForever
:// State of animation for composition
val progress by animateLottieCompositionAsState(
composition = composition,
iterations = LottieConstants.IterateForever
)
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 }
)
}
Upvotes: 4
Reputation: 2173
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
Upvotes: 16
Reputation: 23394
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); }
Upvotes: 0
Reputation: 60923
Try
<com.airbnb.lottie.LottieAnimationView
...
app:lottie_repeatCount="3"
/>
Upvotes: 6