Android View Rotation Animation 和 Set Rotation 未按预期工作
Posted
技术标签:
【中文标题】Android View Rotation Animation 和 Set Rotation 未按预期工作【英文标题】:Android View Rotation Animation and Set Rotation not working as intended 【发布时间】:2016-11-08 22:58:10 【问题描述】:我试图将视图旋转 90 度,但每次调用此方法时,视图都会向前跳 90 度,然后再旋转 90 度。
我试过玩数字但没有成功。这段代码的工作原理似乎有些我不明白的地方。
有人可以解释一下目前正在发生的事情,以及需要改变什么才能获得预期的结果吗?
public void RotatePoint(float toDegrees)
final RotateAnimation rotate =
new RotateAnimation(pt.getRotation(), pt.getRotation() + toDegrees,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(1000);
rotate.setFillEnabled(true);
rotate.setFillAfter(true);
pt.startAnimation(rotate);
pt.setRotation(pt.getRotation()+toDegrees);
if (pt.getRotation() == 360)
pt.setRotation(0);
【问题讨论】:
更正了以下行 pt.setRotation(pt.getRotation()+toDegrees);它曾经读取 +90 而不是 +toDegrees,在这种情况下,toDegrees 始终设置为 90 可能很重要。 【参考方案1】:取全局变量-float rotation = 0,fromRotation=0;
在点击时
fromRotation=rotation;
rotation = rotation + 90;
RotatePoint(rotation);
和 RotatePoint(float toDegrees)
RotateAnimation rotate = new RotateAnimation(fromRotation, toDegrees,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(1000);
rotate.setFillAfter(true);
pt.startAnimation(rotate);
【讨论】:
这是部分解决方案。在我的应用程序中,我进行验证以查看该点所面向的方向,但是旋转会不断增加。我重新添加了 If(rotation==360)rotation=0; ,这在第一次旋转后有效,但在每个点的第一次旋转时它仍然随机移动。我相信这是因为每个点都以随机旋转(0、90、180 或 270)开始。似乎这些点被赋予了随机旋转(视觉上),但旋转与存储在其变量中的旋转在某种程度上不同。也感谢您花时间回答我的问题。 改为更改旋转和 fromRotation 时 if(fromRotation==270)fromRotation=0;rotation=0; fromRotation 值将为您提供旋转前点的初始位置,Rotation 将为您提供旋转后的值 好的,谢谢,所以我进行了建议的更改,如果我摆脱了随机开始旋转,除了一个问题之外,它工作正常。我在表格布局中旋转这些点,所以有多个。如果我将一个点从 0 旋转到 270,一切正常。但是如果我去一个不同的点并旋转它,它会像从 270 开始一样旋转。这在每种情况下都会发生。 Rotation 和 fromRotation 值传递到不同的对象。有什么想法吗?以上是关于Android View Rotation Animation 和 Set Rotation 未按预期工作的主要内容,如果未能解决你的问题,请参考以下文章
Android GAME_ROTATION_VECTOR 反向移植
Android Lock Screen Orientation
Android Rotation 重定向到 webview 的根 URL