android 为什么NavController的页面转换如此缓慢

sg2wtvxw  于 2024-01-04  发布在  Android
关注(0)|答案(2)|浏览(180)

我刚开始使用Jetpack Compose。我想使用NavController在页面之间切换,但奇怪的是,在进行此转换时,fps感觉非常低和缓慢。我没有添加任何动画,默认动画可用。我试图删除此默认动画,但没有成功。
是什么原因造成的?我做错了什么?

显示我的问题的视频:https://www.youtube.com/shorts/Cj1vDcmhRYc

  • 注意:我在几个设备上试过,问题都是一样的。*
    我的版本;
  1. implementation 'androidx.activity:activity-compose:1.8.2'
  2. implementation "androidx.navigation:navigation-compose:2.7.6"
  3. implementation "androidx.compose.material:material:1.5.4"
  4. implementation 'androidx.compose.material3:material3'
  5. implementation 'androidx.core:core-ktx:1.12.0'
  6. implementation platform('org.jetbrains.kotlin:kotlin-bom:1.8.0')

字符串

我的主活动:

  1. class MainActivity : ComponentActivity() {
  2. override fun onCreate(savedInstanceState: Bundle?) {
  3. super.onCreate(savedInstanceState)
  4. setContent {
  5. CarCareTrackerTheme {
  6. val navController = rememberNavController()
  7. MyBottomNavigation(navController)
  8. }
  9. }
  10. }
  11. }
  12. @OptIn(ExperimentalMaterial3Api::class)
  13. @Composable
  14. fun MyBottomNavigation(navController: NavController) {
  15. Scaffold(
  16. modifier = Modifier,
  17. floatingActionButton = {
  18. Box() {
  19. FloatingActionButton(
  20. onClick = { /* stub */},
  21. containerColor = MainBlue,
  22. shape = CircleShape,
  23. modifier =
  24. Modifier.align(Alignment.Center)
  25. .size(80.dp)
  26. .offset(y = 50.dp)
  27. .shadow(
  28. elevation = 10.dp,
  29. shape = CircleShape,
  30. ),
  31. ) {
  32. Icon(Icons.Filled.Add, contentDescription = "Add", tint = Color.White)
  33. }
  34. }
  35. },
  36. floatingActionButtonPosition = FabPosition.Center,
  37. content = { innerPadding ->
  38. NavHost(
  39. navController as NavHostController,
  40. startDestination = "home",
  41. Modifier.padding(innerPadding),
  42. ) {
  43. composable("home") { HomeScreen(navController) }
  44. composable("settings") { SettingsScreen(navController) }
  45. }
  46. },
  47. bottomBar = {
  48. BottomNavigation(backgroundColor = Color.White, contentColor = MainBlue) {
  49. val navBackStackEntry by navController.currentBackStackEntryAsState()
  50. val currentDestination = navBackStackEntry?.destination
  51. BottomNavigationItem(
  52. icon = {
  53. val isSelected =
  54. currentDestination?.hierarchy?.any { it.route == "home" } == true
  55. Icon(
  56. Icons.Filled.Home,
  57. contentDescription = null,
  58. tint = if (isSelected) MainBlue else LightGray
  59. )
  60. },
  61. label = {
  62. val isSelected =
  63. currentDestination?.hierarchy?.any { it.route == "home" } == true
  64. Text(
  65. stringResource(R.string.home),
  66. color = if (isSelected) MainBlue else LightGray
  67. )
  68. },
  69. selected = currentDestination?.hierarchy?.any { it.route == "home" } == true,
  70. selectedContentColor = MainBlue,
  71. onClick = {
  72. navController.navigate("home") {
  73. popUpTo(navController.graph.findStartDestination().id) {
  74. saveState = true
  75. }
  76. launchSingleTop = true
  77. restoreState = true
  78. }
  79. }
  80. )
  81. BottomNavigationItem(
  82. icon = {
  83. val isSelected =
  84. currentDestination?.hierarchy?.any { it.route == "settings" } == true
  85. Icon(
  86. Icons.Filled.Settings,
  87. contentDescription = null,
  88. tint = if (isSelected) MainBlue else LightGray
  89. )
  90. },
  91. label = {
  92. val isSelected =
  93. currentDestination?.hierarchy?.any { it.route == "settings" } == true
  94. Text(
  95. stringResource(R.string.settings),
  96. color = if (isSelected) MainBlue else LightGray
  97. )
  98. },
  99. selected =
  100. currentDestination?.hierarchy?.any { it.route == "settings" } == true,
  101. selectedContentColor = MainBlue,
  102. unselectedContentColor = LightGray,
  103. onClick = {
  104. navController.navigate("settings") {
  105. popUpTo(navController.graph.findStartDestination().id) {
  106. saveState = true
  107. }
  108. launchSingleTop = true
  109. restoreState = true
  110. }
  111. }
  112. )
  113. }
  114. }
  115. )
  116. }

设置画面:

  1. @Composable
  2. fun SettingsScreen (
  3. navController: NavController
  4. ){
  5. Column() {
  6. Text(text = "SettingsScreen")
  7. Text(text = "SettingsScreen")
  8. }
  9. }

主屏幕:

  1. @Composable
  2. fun HomeScreen(
  3. navController: NavController
  4. ) {
  5. Column() {
  6. Text(text = "HomeScreen")
  7. Text(text = "HomeScreen")
  8. }
  9. }

hgb9j2n6

hgb9j2n61#

从2.7.0版本开始,Google将陪伴导航动画转换回了导航合成,并将陪伴中包含的动画功能带回来。
您提到您尝试禁用此默认动画,但没有说明您尝试了什么。为了实现此目的,导航库现在提供了使用EnterTransition.NoneExitTransition.None.禁用默认淡入淡出过渡的功能

  1. NavHost(navController = rememberNavigationBarController,
  2. startDestination = "home",
  3. modifier = Modifier.padding(it),
  4. enterTransition = {
  5. // you can change whatever you want transition
  6. EnterTransition.None
  7. },
  8. exitTransition = {
  9. // you can change whatever you want transition
  10. ExitTransition.None
  11. }

字符串

chhqkbe1

chhqkbe12#

我认为,它看起来像有一个FPS下降的原因是动画,我试图关闭动画。然而,虽然我关闭了enterTransition和exitTransition动画,缩放动画仍然可见。
根据StackOverFlow上的另一个问题的评论;从2.7.0版本的导航,即使你试图关闭动画,缩放动画仍然存在。所以我把我的版本从2.7.6改为2.6.0。

  • 现在您可以在没有动画的情况下切换,并且没有糟糕的FPS下降外观(我知道没有FPS下降,但动画让它感觉像它)。
  • 解决方案:使用Navigation Compose 2.6.0版本。*

版本2.7.6和2.6.0之间的差异**(视频)**:
WITH version 2.7.6 of Navigation Compose(Even if I turn off animations)
When I use Version 2.6.0
资源:
Comment from midFang on StackOverFlow
议题追踪

相关问题