11

I'm implementing a Lottie animation and the entire animation works great! However, I would like to add a bit of code that will pause the animation after 30 frames which I can then resume after a certain amount of time. Here is the code so far:

animationView.playAnimation(0, 30)
animationView.addAnimatorListener(object : Animator.AnimatorListener {
   override fun onAnimationEnd(animation: Animator) {
      if (isLoading == false) {
         //Everything has loaded. Continue Animation 
         
         //This line has no effect. The animation does not continue
         animationView.playAnimation(30, 60)
         //Resuming the animation just makes the animation disappear
         //animation.resume()
      }
 }
TylerH
  • 20,799
  • 66
  • 75
  • 101
Andy Joyce
  • 2,802
  • 3
  • 15
  • 24

3 Answers3

16

What you can do is use progress from LottieAnimationView, threads and a flag, this will allow you to pause at a certain progress and resume exactly when you need to play your animation again.

I created the following example:

animationView.playAnimation()
animationView.loop(false)

isAnimating = true // Setup your flag

thread {
    while (isAnimating){ // Loop that checks the progress of your animation
        if (animationView.progress >= 0.5f){// If animation reaches 50%
            runOnUiThread {
                animationView.pauseAnimation()// Pause Animation
            }
            Thread.sleep(5000) // Pause for 5 seconds
            runOnUiThread {
                animationView.playAnimation(0.5f,1f) // Resume your animation from 50% to 100%
            }
            isAnimating = false
        }
        if(animationView.progress >= 1f){ // If animation reaches 100% stop animation
            runOnUiThread {
                animationView.cancelAnimation()
                isAnimating = false
            }
        }
    }
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Andre Breton
  • 1,273
  • 1
  • 9
  • 19
  • `animationView.loop(false)` is deprecated as on 2.8.0. What is the equivalent method? – hkchakladar Dec 07 '18 at 11:43
  • 3
    @hkchakladar I'm using `animationDrawable.repeatCount = LottieDrawable.INFINITE` and `animationDrawable.repeatMode = LottieDrawable.RESTART`. – ASP May 17 '19 at 11:47
  • 1
    This is not life cycle aware method and it may cause memory leaks and null pointer exceptions – Mobin Yardim Jan 12 '22 at 06:20
5
animationView.setMinAndMaxProgress(0.0f, 0.5f);//set 50% animation //lottie version 2.7.0
mr.hir
  • 533
  • 1
  • 7
  • 19
  • 3
    Hi Mr.hir, welcome to stackoverflow, a handy tip going forward, some explanations about your answer would make your post much more valued and upvoted. =) – JackDev Aug 20 '19 at 07:55
  • This works for me. Just add this line before calling `playAnimation()` – Tashila Pathum Apr 27 '21 at 09:08
0
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        animationView.pauseAnimation();
    }
}, 8000); // after 8s animation will pause/stop/cancel/resume.
patrick.elmquist
  • 2,113
  • 2
  • 21
  • 35