暂停和恢复 Lottie 动画

13

我正在实现一个 Lottie 动画,整个动画效果都很棒!但是,我希望添加一些代码,在 30 帧后暂停动画,然后在一定时间后恢复。目前的代码如下:

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()
      }
 }
3个回答

18

你可以使用LottieAnimationView中的progress、线程和标志,这将允许你在某个进度处暂停并在需要播放动画时恢复

我创建了以下示例:

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
            }
        }
    }
}

animationView.loop(false)在2.8.0版本已被弃用。有什么等效的方法? - hkchakladar
4
@hkchakladar 我正在使用 animationDrawable.repeatCount = LottieDrawable.INFINITEanimationDrawable.repeatMode = LottieDrawable.RESTART - ASP
1
这不是生命周期感知方法,可能会导致内存泄漏和空指针异常。 - Mobin Yardim

6
animationView.setMinAndMaxProgress(0.0f, 0.5f);//set 50% animation //lottie version 2.7.0

3
你好,Hir先生,欢迎来到StackOverflow。一个有用的提示是,今后在回答问题时,给出一些解释会让你的帖子更受重视和点赞。 =) - JackDev
这对我有效。只需在调用 playAnimation() 之前添加此行即可。 - Tashila Pathum

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.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接