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
}
}
}
}
}
}
泰寻求帮助。
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 - 在 Room 中使用成员变量进行查询
- java - 从 json 中读取日期为数组整数,并将 POJO 映射为整数
- swiftui - 如何在 swiftui 中创建键盘应用程序扩展?
- python-3.x - 在 Ubuntu 中读取 Python Logging 日志文件
- python - 怎么不打印两次
- sql-server - 抛出异常:system.data.dll 中的 system.data.sqlclient.sqlexception
- excel - Excel表格数据转置
- java - 用放心断言错误请求时失败
- r - 控制悬停文本的按钮
- postgresql - 需要减少postgres中的查询优化时间