【问题标题】:Jetpack compose how to wait for animation endsJetpack compose 如何等待动画结束
【发布时间】:2021-08-05 21:15:24
【问题描述】:

我通过 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
                        }
                    }
                }
            }
        }
    }

求帮助。

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

   

【问题讨论】:

    标签: android android-animation android-jetpack-compose


    【解决方案1】:

    这里是这样做的主要思想:

       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
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      在 Navigation 2.4.0-alpha05 中引入了 Crossfade 作为导航的默认过渡。使用最新版本的 Navigation 2.4.0-alpha06,您可以通过 Accompanist 添加自定义过渡

      【讨论】:

        猜你喜欢
        • 2018-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多