我刚开始使用Jetpack Compose。我想使用NavController在页面之间切换,但奇怪的是,在进行此转换时,fps感觉非常低和缓慢。我没有添加任何动画,默认动画可用。我试图删除此默认动画,但没有成功。
是什么原因造成的?我做错了什么?
显示我的问题的视频:https://www.youtube.com/shorts/Cj1vDcmhRYc
- 注意:我在几个设备上试过,问题都是一样的。*
我的版本;
implementation 'androidx.activity:activity-compose:1.8.2'
implementation "androidx.navigation:navigation-compose:2.7.6"
implementation "androidx.compose.material:material:1.5.4"
implementation 'androidx.compose.material3:material3'
implementation 'androidx.core:core-ktx:1.12.0'
implementation platform('org.jetbrains.kotlin:kotlin-bom:1.8.0')
字符串
我的主活动:
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
CarCareTrackerTheme {
val navController = rememberNavController()
MyBottomNavigation(navController)
}
}
}
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MyBottomNavigation(navController: NavController) {
Scaffold(
modifier = Modifier,
floatingActionButton = {
Box() {
FloatingActionButton(
onClick = { /* stub */},
containerColor = MainBlue,
shape = CircleShape,
modifier =
Modifier.align(Alignment.Center)
.size(80.dp)
.offset(y = 50.dp)
.shadow(
elevation = 10.dp,
shape = CircleShape,
),
) {
Icon(Icons.Filled.Add, contentDescription = "Add", tint = Color.White)
}
}
},
floatingActionButtonPosition = FabPosition.Center,
content = { innerPadding ->
NavHost(
navController as NavHostController,
startDestination = "home",
Modifier.padding(innerPadding),
) {
composable("home") { HomeScreen(navController) }
composable("settings") { SettingsScreen(navController) }
}
},
bottomBar = {
BottomNavigation(backgroundColor = Color.White, contentColor = MainBlue) {
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry?.destination
BottomNavigationItem(
icon = {
val isSelected =
currentDestination?.hierarchy?.any { it.route == "home" } == true
Icon(
Icons.Filled.Home,
contentDescription = null,
tint = if (isSelected) MainBlue else LightGray
)
},
label = {
val isSelected =
currentDestination?.hierarchy?.any { it.route == "home" } == true
Text(
stringResource(R.string.home),
color = if (isSelected) MainBlue else LightGray
)
},
selected = currentDestination?.hierarchy?.any { it.route == "home" } == true,
selectedContentColor = MainBlue,
onClick = {
navController.navigate("home") {
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
}
)
BottomNavigationItem(
icon = {
val isSelected =
currentDestination?.hierarchy?.any { it.route == "settings" } == true
Icon(
Icons.Filled.Settings,
contentDescription = null,
tint = if (isSelected) MainBlue else LightGray
)
},
label = {
val isSelected =
currentDestination?.hierarchy?.any { it.route == "settings" } == true
Text(
stringResource(R.string.settings),
color = if (isSelected) MainBlue else LightGray
)
},
selected =
currentDestination?.hierarchy?.any { it.route == "settings" } == true,
selectedContentColor = MainBlue,
unselectedContentColor = LightGray,
onClick = {
navController.navigate("settings") {
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
}
)
}
}
)
}
型
设置画面:
@Composable
fun SettingsScreen (
navController: NavController
){
Column() {
Text(text = "SettingsScreen")
Text(text = "SettingsScreen")
}
}
型
主屏幕:
@Composable
fun HomeScreen(
navController: NavController
) {
Column() {
Text(text = "HomeScreen")
Text(text = "HomeScreen")
}
}
型
2条答案
按热度按时间hgb9j2n61#
从2.7.0版本开始,Google将陪伴导航动画转换回了导航合成,并将陪伴中包含的动画功能带回来。
您提到您尝试禁用此默认动画,但没有说明您尝试了什么。为了实现此目的,导航库现在提供了使用
EnterTransition.None
和ExitTransition.None.
禁用默认淡入淡出过渡的功能字符串
chhqkbe12#
我认为,它看起来像有一个FPS下降的原因是动画,我试图关闭动画。然而,虽然我关闭了enterTransition和exitTransition动画,缩放动画仍然可见。
根据StackOverFlow上的另一个问题的评论;从2.7.0版本的导航,即使你试图关闭动画,缩放动画仍然存在。所以我把我的版本从2.7.6改为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的
议题追踪