如何正确等待动画结束?

Mor*_*zov 2 android android-animation lottie android-jetpack-compose

lottie我知道我可以使用 跟踪动画完成的时刻progress。但问题是我想在动画完全完成的那一刻开始一个新的屏幕。

这是我的动画的代码

@Composable
fun AnimatedScreen(
    modifier: Modifier = Modifier,
    rawId: Int
) {
    Box(
        contentAlignment = Alignment.Center,
        modifier = modifier.fillMaxSize()
    ) {
        val compositionResult: LottieCompositionResult = rememberLottieComposition(
            spec = LottieCompositionSpec.RawRes(rawId)
        )
        AnimatedScreenAnimation(compositionResult = compositionResult)
    }
}

@Composable
fun AnimatedScreenAnimation(compositionResult: LottieCompositionResult) {

    val progress by animateLottieCompositionAsState(composition = compositionResult.value)

    Column {
        if (progress < 1) {
            Text(text = "Progress: $progress")
        } else {
            Text(
                modifier = Modifier.clickable { },
                text = "Animation is done"
            )
        }
        LottieAnimation(
            composition = compositionResult.value,
            progress = progress,
            modifier = Modifier.fillMaxSize(),
            contentScale = ContentScale.FillBounds
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的屏幕代码,我想等待动画结束,然后转到新屏幕:

@Composable
fun SplashScreen(
    navController: NavController,
    modifier: Modifier = Modifier,
    viewModel: SplashScreenViewModel = getViewModel()
) {
    val resIdState = viewModel.splashScreenResId.collectAsState()

    val resId = resIdState.value
    if (resId != null) {
        AnimatedScreen(modifier = modifier, rawId = resId)
    }

    LaunchedEffect(true) {
        navigate("onboarding_route") {
            popUpTo(0)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

May*_*jra 6

我使用了progress&听了它的更新&一旦它到达1f我就会调用我的函数。

例子:

@Composable
fun Splash() {
    LottieTest {
        // Do something here
    }
}

@Composable
fun LottieTest(onComplete: () -> Unit) {
    val composition: LottieCompositionResult =
        rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.camera))
    val progress by animateLottieCompositionAsState(
        composition.value,
        iterations = 1,
    )

    LaunchedEffect(progress) {
        Log.d("MG-progress", "$progress")
        if (progress >= 1f) {
            onComplete()
        }
    }

    LottieAnimation(
        composition.value,
        progress,
    )
}
Run Code Online (Sandbox Code Playgroud)

注意:这就是我所做的。最好的方法仍然未知(至少对我来说)。我觉得它缺乏这方面的样本。

另外,您可以从中进行很多修改并专注于核心流程。