我正在尝试了解 LottieAnimationView 中的工作原理,按顺序播放作品。使用此代码时,存在一个错误,即最后一个合成的最后一帧不会消失,并且动画会留下停止播放的合成的帧痕迹。是我的代码有问题还是 Lottie Compose 尚不支持此功能?我希望将“组合”值更新为可变属性,但无济于事。
@Composable
fun Play(file: Int) {
val composition by rememberLottieComposition(
LottieCompositionSpec.RawRes(file)
)
val progress by animateLottieCompositionAsState(composition)
LottieAnimation(
composition = composition,
progress = { progress }
)
if (progress == 1f) {
// compute UI-blocking functions
Play(nextFile)
}
}
编辑:
此代码不留下尾随,但动画序列在开始下一个合成时闪烁。在 XML 中它可以完美地工作。
@Composable
fun Play() {
@Composable
fun c1(): Pair<LottieComposition?, Float> {
val composition by rememberLottieComposition(
LottieCompositionSpec.RawRes(R.raw.anim1)
)
val p by animateLottieCompositionAsState(composition)
return composition to p
}
@Composable
fun c2(): Pair<LottieComposition?, Float> {
val composition by rememberLottieComposition(
LottieCompositionSpec.RawRes(R.raw.anim2)
)
val p by animateLottieCompositionAsState(composition)
return composition to p
}
val dir = remember { mutableStateOf(false) }
val p = if (dir.value) c1() else c2()
LottieAnimation(
composition = p.first,
progress = { p.second }
)
if (p.second == 1f) {
// ui-blocking
dir.value = !dir.value
}
}
@Composable
fun Play() {
val composition1 by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.anim1))
val composition2 by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.anim2))
val state = remember { LottieAnimatable() }
LaunchedEffect(composition1, composition2) {
if (composition1 == null || composition2 == null) return@LaunchedEffect
while (<condidion>) {
state.animate(composition1)
state.animate(composition2)
}
}
LottieAnimation(state.composition, { state.progress })
if (state.progress == 1f) // blocking functions
}