Jetpack compose 如何等待动画结束

问题描述 投票:0回答:5

我有 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

下一个屏幕按钮代码:

    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
5个回答
16
投票

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

   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
    }

}

5
投票

回答你的问题:“如何等待动画结束”。

只需检查transition.targetState是否与transition.currentState相同。 如果相同,则动画结束。

AnimatedVisibility( 
        initiallyVisible= ...,
        enter = ...,
        exit = ...
    ) {
          ...<your layout>

         //to detect if your animation have completed just check the following
        if (this.transition.currentState == this.transition.targetState){
             //Animation is completed when current state is the same as target state.
             //you can call whatever you like here -> e.g. start music, show toasts, enable buttons, etc
            
             callback.invoke() //we invoke a callback as an example.

         }

          
    }

这也适用于其他动画,如 AnimatedContent 等


2
投票

交叉淡入淡出作为导航 2.4.0-alpha05 中导航的默认过渡引入。使用最新版本的 Navigation 2.4.0-alpha06,您可以通过 Accompanist

添加自定义过渡

2
投票
val animVisibleState = remember { MutableTransitionState(false) }
val nextButtonPressState = remember { mutableStateOf(false) }

LaunchedEffect(key1 = true) {
    animVisibleState.targetState = true
}

if ( !animVisibleState.targetState &&
    !animVisibleState.currentState &&
    nextButtonPressState.value
) {
    navController.navigate(ROUTE_MAIN_LIST)
}

AnimatedVisibility(
    visibleState = animVisibleState,
    enter = fadeIn(),
    exit = fadeOut()
) {
    NextButton() {
        nextButtonPressState.value = true
    }
}

.apply {animVisibleState.target = true} 在变量的声明中将在每次重组时应用它,因此 LaunchedEffect 可以是一个解决方案,还有更好的方法来处理 nextButtonPressionState


0
投票

这对我有用。

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

LaunchedEffect(Unit) {
    snapshotFlow {
        Pair(visibilityTransition.currentState, visibilityTransition.targetState)
    }.collectLatest { (curr, target) ->
        if(!curr && !target) {
            onDismiss.invoke()
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.