我收到了多个需要使用Lottie库按顺序播放的动画文件
目前我正在尝试重复使用相同的LottieAnimationView,当第一个动画结束时,只需设置下一段动画即可。 代码如下:
private fun playFirstAnimation() {
binding.animation.setAnimation(R.raw.progress_1)
binding.animation.repeatCount = 2
binding.animation.addAnimatorListener(object : Animator.AnimatorListener {
override fun onAnimationRepeat(animation: Animator?) = Unit
override fun onAnimationCancel(animation: Animator?) = Unit
override fun onAnimationStart(animation: Animator?) = Unit
override fun onAnimationEnd(animation: Animator?) {
binding.animation.removeAnimatorListener(this)
notifyFirstAnimationFinished()
playSecondAnimation()
}
})
binding.animation.playAnimation()
}
private fun playSecondAnimation() {
binding.animation.setAnimation(R.raw.progress_2)
binding.animation.repeatCount = 0
binding.animation.addAnimatorListener(object : Animator.AnimatorListener {
override fun onAnimationRepeat(animation: Animator?) = Unit
override fun onAnimationCancel(animation: Animator?) = Unit
override fun onAnimationStart(animation: Animator?) = Unit
override fun onAnimationEnd(animation: Animator?) {
binding.animation.removeAnimatorListener(this)
notifySecondAnimationFinished()
playThirdAnimation()
}
})
binding.animation.playAnimation()
}
这里的问题(除了丑陋的代码)是当短时间内更改动画时会出现小闪烁(动画完全消失,其位置上没有任何内容显示)。我不能将所有动画合并成一个,因为我需要知道一个动画何时结束。而且我真的不想为每个动画都有一个单独的LottieAnimationView,并在每次更改时替换它们,因为我可能有很多动画。
在这种情况下,我有哪些选择?