android - 旋转动画在 Jetpack Compose 中不起作用
问题描述
我在 Jetpack Compose 中尝试过动画。我在旋转动画中遇到问题。
似乎一切都很好。但不知道为什么它不起作用。
我的代码:
@Composable
private fun RotateAnimationContent() {
val isRotated = rememberSaveable { mutableStateOf(false) }
val rotationAngle by animateFloatAsState(
targetValue = if (isRotated.value) 360F else 0F,
animationSpec = tween(durationMillis = 1500,easing = FastOutLinearInEasing)
)
Column {
Box(modifier = Modifier.background(Color.Red).size(100.dp).rotate(rotationAngle))
Button(
onClick = { isRotated.value = !isRotated.value },
modifier = Modifier.padding(10.dp)
) {
Text(text = "Rotate Box")
}
}
}
解决方案
将.rotate(rotationAngle)
作为 的第一个修饰符Box
。在背景和大小之前。
Column {
Box(modifier = Modifier.rotate(rotationAngle).background(Color.Red).size(100.dp))
Button(
onClick = { isRotated.value = !isRotated.value },
modifier = Modifier.padding(10.dp)
) {
Text(text = "Rotate Box")
}
}
检查这个非常详细的答案,以更好地理解为什么修饰符的顺序很重要。
推荐阅读
- list - Flutter:通过在构建小部件时创建一个大列表来提高性能
- sql - 为什么 Postgres 不接受我的计数列?
- python - 当我在 Python 3.9 中使用 Requests 请求文档页面时,我会发出多少个 http 请求?
- smtp - StmpClient 使用适用于通过 VPN 的远程用户,但不适用于办公室、网络上的用户
- excel - 使用 VBA 提取条形码数据
- swift - 嵌套的 ObservableObject 如何引用父 EnvironmentObject - SwiftUI
- sql - 返回具有多个值之一的行
- xcode - 公证应用程序时如何阻止 Xcode 崩溃?
- c# - 将包含数组/列表的 JSON 响应存储到数据库中
- c - 使用 SDL 表面进行像素操作?