android - Jetpack compose 如何等待动画结束

标签 android android-animation android-jetpack-compose

我有 AnimatedVisibility (slideInVertically/SlideOut) 的简单动画。
当我按下“nextScreenButton”时,我通过 navController 进行新的导航。
过渡立即完成,因此没有时间退出动画。
如何让等到动画结束
我可以为动画时间输入一些延迟,但这不是一个好方法。
代码:

      Scaffold() {
        AnimatedVisibility(
            //Boolean State for open animation 
            OpenChooseProfilePageAnim.value,
            
            initiallyVisible= false,
            enter = slideInVertically(
                initialOffsetY = { fullHeight -> fullHeight },
                animationSpec = tween(
                    durationMillis = 3000,
                    easing = LinearOutSlowInEasing
                )
            ),
            exit = slideOutVertically(
                targetOffsetY = { fullHeight -> fullHeight },
                animationSpec = tween(
                    durationMillis = 3000,
                    easing = LinearOutSlowInEasing
                )
            )
        ) {
            ConstraintLayout() {
                Card() {
                    Column() {
                        
                        //Some other Composable items
                        
                        //Composable button
                        NextScreenButton() {
                            //calling navigation here
                        }
                    }
                }
            }
        }
    }
泰寻求帮助。
enter image description here
NextScreenButton 代码:
    fun navigateToMainListPage(navController: NavController) {
        //change State of visibility for "AnimatedVisibility"
        AnimationsState.OpenChooseProfilePageAnim.value = false
        //navigate to another route in NavHost
        navController.navigate(ROUTE_MAIN_LIST)
    }

导航主机:

    @Composable
    fun LoginGroupNavigation(startDestination: String) {
        val navController = rememberNavController()
        NavHost(navController, startDestination = startDestination) {
            composable(LoginScreens.LoginScreen.route) {
                LoginMainPage(navController)
            }
            composable(LoginScreens.EnteringPhoneNumScreen.route,
                arguments = listOf(navArgument("title") { type = NavType.StringType },
            )) {
                val title =  it.arguments?.getString("title") ?: ""
                EnterPhoneNumberForSmsPage(
                    navController = navController,
                    title
                )
            }
    //more composable screens

   

最佳答案

这是执行此操作的主要思想:

   val animVisibleState = remember { MutableTransitionState(false) }
    .apply { targetState = true }

//Note: Once the exit transition is finished,
//the content composable will be removed from the tree, 
//and disposed.
//Both currentState and targetState will be false for 
//visibleState.
if (!animVisibleState.targetState &&
    !animVisibleState.currentState
) {
    //navigate to another route in NavHost
    navController.navigate(ROUTE_MAIN_LIST)
    return
}


AnimatedVisibility(
    visibleState = animVisibleState,
    enter = fadeIn(
        animationSpec = tween(durationMillis = 200)
    ),
    exit = fadeOut(
        animationSpec = tween(durationMillis = 200)
    )
) {
    NextButton() {
        //start exit animation
        animVisibleState.targetState = false
    }

}

关于android - Jetpack compose 如何等待动画结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67516606/

相关文章:

Android点击EditText不会显示软键盘

java - SoundPool 文件队列

android - 旋转动画保持回到默认状态

android - 使用 jetpack compose 突出顶部应用栏

android - 无法在 Jetpack Compose 中创建按钮

android - 如何在 Xamarin Imageview 中显示 MySQL BLOB 图像?

android - 如何在 Delphi 中使用自定义 debug.keystore?

java - Android Activity 转换动画仅适用于被调用的 Activity

android - android中的自定义进度条,如gif

android - 如何从 Jetpack Compose 中的 drawable 加载图像?