LottieAnimation 与组合在动画更改时不会重新启动

问题描述 投票:0回答:1

我目前正在构建一个带有动画的可组合组件,该动画应该根据从 ViewModel 作为流传递的某些状态进行更改。对于动画,我使用 LottieCompose v.5.2.0。

我的要求是运行的动画应该无限期地运行,并且当动画被交换时,新的动画从头开始。

我现在的问题是,当动画更改时,新动画似乎继承了前一个动画的进度。

{
    val composition by rememberLottieComposition(spec = LottieCompositionSpec.Asset(state.animation))

    LottieAnimation(
        composition = composition,
        restartOnPlay = true,
        iterations = LottieConstants.IterateForever,
    )
}

state.animation
是引用动画资源的字符串。

从源代码中我可以看到应用了下面的逻辑,因此考虑到这一点,我假设每当我换出动画时动画都会重新启动。

{
    if (isPlaying && !wasPlaying && restartOnPlay) {
        animatable.resetToBeginning()
    }

非常感谢任何帮助。

android android-jetpack-compose android-animation lottie
1个回答
0
投票

只是在这里插话以防对其他人有帮助 - 我以为我遇到了同样的问题,但我就是问题所在;D

当设置你的构图并遵循基本用法时,你使用

animateLottieCompositionAsState
的进度如下:

val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.message_from_bottle))
    val progress: Float by animateLottieCompositionAsState(
        composition = composition,
        isPlaying = isPlaying,
        speed = 1f,
        restartOnPlay = true,
        iterations = 1,
        cancellationBehavior = LottieCancellationBehavior.OnIterationFinish
    )

您需要确保在配置 LottieAnimation 时使用该结果

progress
,例如:

LottieAnimation(
            composition = composition,
            progress = { progress }, // <-- if you don't do this, your animation will NOT restart 🚀
            ...,
        )
    ```
© www.soinside.com 2019 - 2024. All rights reserved.