gpt4 book ai didi

android - 使用 RotateAnimation 的最短路径动画

转载 作者:太空宇宙 更新时间:2023-11-03 13:32:37 25 4
gpt4 key购买 nike

我想做两个角度之间的最短路径旋转动画。我正在使用 RotateAnimation(fromDegrees, toDegrees, pivotX, pivotY) 方法为图像设置动画。

问题是当 fromDegrees 为 350 且 toDegrees 为 10 时,它会旋转整个圆(340 度)而不是顺时针旋转 20 度。

有没有办法强制进行最小旋转?

最佳答案

您可以使用 ViewPropertyAnimator 而不是 RotateAnimation它有一个方法 rotationBy ,将 View 旋转 BY 指定值,因此您不需要“从”和“到”值。

这两个动画之间的主要区别主要在于,RotateAnimation 仅移动屏幕上的像素,因此例如 OnClickListener 不会使用像素进行动画处理,而 ViewPropertyAnimator 会更改 View 在屏幕上的实际属性/位置.

ViewPropertyAnimator 也非常有用,因为它很容易实现。顺时针旋转 20 度的 ViewPropertyAnimator 的最小实现如下所示:

yourImage.animate().rotationBy(20); 

设置持续时间永远不会错,也建议设置一个插值器,因为它可以使任何动画看起来更逼真:

yourImage.animate()
.rotationBy(20) -> degrees -20 would be counter clockwise
.setDuration(1000) -> ms
.setInterpolator(new AccelerateDecelerateInterpolator());

一定要检查 docs对于所有可用的方法。

关于android - 使用 RotateAnimation 的最短路径动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11098116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com