我有 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
最佳答案
这是执行此操作的主要思想:
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/