kotlin 重新启动lottie-animation一遍又一遍

d5vmydt9  于 2023-06-24  发布在  Kotlin
关注(0)|答案(1)|浏览(137)

我尝试在每次amount变量的值发生变化时重新启动Lottie动画。
第一次更改值是成功的,但对该值的后续更改不会触发动画。

@Composable
private fun Lottie(
    amount: Long
) {
    var oldAmount by remember { mutableStateOf(0L) }
    val currentAmount by remember { mutableStateOf(amount) }
    val playAnimation = currentAmount != amount && oldAmount != 0L
    oldAmount = currentAmount

    val composition by rememberLottieComposition(
        LottieCompositionSpec.RawRes(R.raw.my_animation)
    )

    val progress by animateLottieCompositionAsState(
        composition,
        isPlaying = playAnimation,
        speed = 1f,
        restartOnPlay = false,
        cancellationBehavior = LottieCancellationBehavior.OnIterationFinish
    )

    LottieAnimation(composition = composition, progress = progress)
}
xwbd5t1u

xwbd5t1u1#

找到解决方案

用关键元素 Package 可组合件就成功了

...
key(amount) {
        val composition by rememberLottieComposition(
            LottieCompositionSpec.RawRes(R.raw.my_animation)
        )

        val progress by animateLottieCompositionAsState(
            composition,
            isPlaying = playAnimation,
            speed = 1f,
            restartOnPlay = false,
            cancellationBehavior = LottieCancellationBehavior.OnIterationFinish
        )

        LottieAnimation(composition = composition, progress = progress)
    }
...

相关问题