我想旋转一个ImageButton 180度,所以它匹配反向纵向。当我按照与其他方向变化相同的方式进行时,结果是完美的,但不是动画。如何从不同角度旋转视图?
public void onOrientationChanged(int DeviceAngle) {
float MyButtonCurrentAngle = MyButton.getRotation(); //Gets portrait rotation (0)
if(DeviceAngle > 350 || DeviceAngle < 10) { //Portrait
MyButton.animate().rotationBy(- MyButtonCurrentAngle).setDuration(100).start();
} else if(DeviceAngle > 80 && DeviceAngle < 100) { //Reverse Landscape
MyButton.animate().rotationBy(-90 - MyButtonCurrentAngle).setDuration(100).start();
} else if(DeviceAngle > 170 && DeviceAngle < 190) { //Reverse Portrait
MyButton.animate().rotationBy(180 - MyButtonCurrentAngle).setDuration(100).start();
} else if(DeviceAngle > 260 && DeviceAngle < 280) { //Landscape
MyButton.animate().rotationBy(90 - MyButtonCurrentAngle).setDuration(100).start();
}
我认为出现这种情况,因为浮子MyButtonCurrentAngle获取从DeviceAngle 350和10(0或360,纵向方向)之间myButton的旋转角的值(0或不旋转),并使用它作为参考。
即使我仍然怀疑,我放弃了以前的情况。浮动似乎与其他方向很好地工作,我认为问题是反向纵向方向的动画。该按钮不应该旋转180度,而是90度或-90度。这是因为无法通过任何横向选项将设备从纵向旋转到纵向纵向。 (无法直接将肖像旋转到反转肖像)。
经过许多不成功的尝试后,我得出结论,我无法使用MyButton获得的角度值,在按钮旋转后,检测到的方向是横向或反向横向。我想过创建另一个浮动图像来获得反向肖像MyButton角度值,但是此活动的方向设置为纵向,所以这没有意义。
因此,我需要获取MyButton的旋转角度,然后将其作为循环条件使用,并根据结果将其旋转90度或-90度。这是我对这个问题的最新方法:
while (MyButtonCurrentAngle==90) {
if (DeviceAngle > 170 && DeviceAngle < 190) {
MyButton.animate().rotationBy(90 - MyButtonCurrentAngle).setDuration(100).start();
}
}
while (MyButtonCurrentAngle==270) {
if (DeviceAngle > 170 && DeviceAngle < 190) {
MyButton.animate().rotationBy(-90 - MyButtonCurrentAngle).setDuration(100).start();
}
}
基本上,这处理从横向和反向景观的设备方向来反转纵向。这没有触发任何动画,所以MyButtonCurrentAngle浮点角度值永不改变或无法检测到?为什么if语句不能读取它?我不知道,我想知道我做错了什么。提前致谢。
发布XML,什么是'MyButtonCurrentAngle'和'ButtonsAngle' –
确保代码中的'ifs'可达。尝试在'ifs'内打印一些日志。 –
MyButtonCurrentAngle是我的按钮float的一个示例,它通过getRotation获取当前的旋转值。 ButtonsAngle是onOrientationChanged int,它们都不在xml文件中。一切正常,不会崩溃。问题是我想为我的按钮创建一个不同的动画,从90到180以及从270到180.(换句话说,我希望从横向到纵向的逆转以及逆向横向的逆转,使纵向看起来和它的效果一样好对应[人像])。 –