【问题标题】:Pause and Resume Lottie Animation暂停和恢复 Lottie 动画
【发布时间】:2018-04-21 11:46:07
【问题描述】:

我正在实现 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()
      }
 }

【问题讨论】:

    标签: android kotlin android-animation lottie


    【解决方案1】:

    您可以做的是使用 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 我正在使用animationDrawable.repeatCount = LottieDrawable.INFINITE 和animationDrawable.repeatMode = LottieDrawable.RESTART。
    • 这不是生命周期感知方法,可能会导致内存泄漏和空指针异常
    【解决方案2】:
    animationView.setMinAndMaxProgress(0.0f, 0.5f);//set 50% animation //lottie version 2.7.0
    

    【讨论】:

    • 您好 Mr.hir,欢迎来到 stackoverflow,这是一个方便的提示,对您的答案的一些解释将使您的帖子更有价值和更受欢迎。 =)
    • 这对我有用。只需在调用 playAnimation() 之前添加此行
    【解决方案3】:
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            animationView.pauseAnimation();
        }
    }, 8000); // after 8s animation will pause/stop/cancel/resume.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-17
      • 2015-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多