android - Jetpack Compose“最短”旋转动画
问题描述
我试图在jetpack compose中做一个指南针。但是我在制作动画时遇到了问题。我有一个@Composable
可以让用户手机旋转并以相反方向旋转指南针图像。我animateFloatAsState
这样使用:
val angle: Float by animateFloatAsState(
targetValue = -rotation, \\ rotation is retrieved as argument
animationSpec = tween(
durationMillis = UPDATE_FREQUENCY, \\ rotation is retrieved with this frequency
easing = LinearEasing
)
)
Image(
modifier = Modifier.rotate(angle),
// rest of the code for image
)
一切看起来都很好,但是当rotation
从1
to359
或以相反的方式更改时会出现问题。动画不会2
向左旋转度数,而是358
向右旋转度数,这看起来很糟糕。有没有办法制作使用最短方式的旋转动画?
解决方案
我假设您已经(或可以获得)访问当前旋转值(即当前角度)的权限,并将其存储起来。
然后,
val angle: Float by animateFloatAsState(
targetValue = if(rotation > 360 - rotation) {-(360 - rotation)} else rotation
animationSpec = tween(
durationMillis = UPDATE_FREQUENCY, \\ rotation is retrieved with this frequency
easing = LinearEasing
)
)
Image(
modifier = Modifier.rotateBy(currentAngle, angle), //Custom Modifier
// rest of the code for image
)
rotateBy 是一个自定义修饰符,应该不难实现。使用内置的旋转修改器来构建它。逻辑将保持不变
推荐阅读
- css - SVG动画不会彼此播放
- protocol-buffers - 跨微服务共享原始文件或生成的文件
- prometheus - 配置警报管理器以使用传入的 web-hook 将通知推送到我的 ring central (glip) 应用程序
- android - 连接查询时重命名列失败
- range - 由于错误,无法运行程序来更改定义的名称
- android - Android OkHttpClient - 使用 Kotlin 下载文件
- flutter - flutter firebase 云函数和逆向工程——dart编译
- android - 如何获取包裹在inkWell内的Container的索引
- html - 如何使长标题文本成为两条直线
- mobile - 在 Tableau 中创建设备特定视图(用于平板电脑或手机)