LottieAnimationView 在 setImageResource 后不启动动画

LottieAnimationView doesn't start animation after setImageResource

我有以下代码:

if (isExtracting) {
    extractionButton.cancelAnimation()
    extractionButton.repeatCount = ValueAnimator.INFINITE
    extractionButton?.let { extractionButton.setComposition(composition) }
    extractionButton.playAnimation()
} else {
    extractionButton.setImageResource(R.drawable.extraction_button_background)
}

当我首先 运行 动画时,一切正常。但是,当状态发生变化并设置了图像,之后状态再次发生变化并且动画应该重新启动时,动画将不会加载。而是显示图像。

知道如何再次 运行 动画吗?

我就是这样做的。确保 isExtracting 标志在一段时间后切换以模拟状态变化。

XML:

 <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/extractionButton"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:lottie_rawRes="@raw/camera"
        app:lottie_autoPlay="false"/>

Activity:

class MainActivity : AppCompatActivity() {

    private var isExtracting = true
    private val extractionButton: LottieAnimationView by lazy { findViewById(R.id.extractionButton) }
    private lateinit var composition: LottieComposition

    private var job: Job? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //change R.raw.camera with your lottie file
        LottieCompositionFactory.fromRawRes(this,R.raw.camera).addListener {
            it?.let {
                composition = it
                play()
            }
        }
    }

    private fun play() {
        if (isExtracting) {
            extractionButton.cancelAnimation()
            extractionButton.repeatCount = ValueAnimator.INFINITE
            extractionButton.setComposition(composition)
            extractionButton.playAnimation()
            toggleAfterDelay()
        } else {
            //change R.drawable.ic_android with your drawable
            extractionButton.setImageResource(R.drawable.ic_android)
            toggleAfterDelay()
        }
    }

    private fun toggleAfterDelay() {
        job?.cancel()
        job = MainScope().launch {
            delay(3000)
            isExtracting = !isExtracting
            play()
        }
    }



}

在隔离环境中尝试 运行(只是 Lottie 的新项目)并查看结果。

输出:

https://gifyu.com/image/SbX7U

Mayur 是对的。但是,我将图像放在回收站视图中。因此,出于这个原因,我也必须遵循 this suggestion.

所以我需要在我的 ViewHolder 中添加这个方法:

fun clearAnimation() {
    binding.extractionButton.cancelAnimation()

    val drawable: Drawable? = binding.extractionButton.drawable
    if (drawable is LottieDrawable) {
        drawable.clearComposition()
    }
}

并将此代码发送到 RecyclerAdapter:

override fun onViewRecycled(holder: RecyclerView.ViewHolder) {
    super.onViewRecycled(holder)

    if (holder is MyViewHolder) {
        holder.clearAnimation()
    }
}

但是,由于 Mayur 是对的,他的回答对我帮助很大,所以我接受了他的回答:-)