如何从 Android 中的 Lottie json 获取动画时间?

How to take animation time from Lottie json in Android?

目前我将延迟设置为常数,而不是我想占用 json 动画中使用的时间,这怎么办?

这是我使用常量的代码:

val composableScope = rememberCoroutineScope()

Box(
    contentAlignment = Alignment.Center,
    modifier = Modifier.fillMaxSize()
) {

    LottieExample()
    composableScope.launch {
        delay(2000L)
        navController.navigate(viewModel.nextRoute) {
            popUpTo(0)
        }
    }
}

这里是动画代码本身:

@Composable
fun LottieExample() {

    val compositionResult: LottieCompositionResult = rememberLottieComposition(
        spec = LottieCompositionSpec.RawRes(R.raw.splash_with_background)
    )
    val progress by animateLottieCompositionAsState(
        composition = compositionResult.value,
        isPlaying = true,
        iterations = LottieConstants.IterateForever,
        speed = 1.0f
    )

    LottieAnimation(
        composition = compositionResult.value,
        progress = progress,
        modifier = Modifier.padding(all = 12.dp)
    )
}

compositionResult出界,拉出动画的duration。 使用下一个解决方案解决:

val composableScope = rememberCoroutineScope()

Box(
    contentAlignment = Alignment.Center,
    modifier = Modifier.fillMaxSize()
) {
    val compositionResult: LottieCompositionResult = rememberLottieComposition(
        spec = LottieCompositionSpec.RawRes(R.raw.splash_with_background)
    )
    SplashScreenAnimation(compositionResult = compositionResult)
    composableScope.launch {
        delay(compositionResult.value?.duration?.toLong() ?: SPLASH_SCREEN_DURATION)
        viewModel.destination.firstOrNull()?.let { route ->
            navController.clearBackStackAndNavigateTo(route)
        }
    }
}