【发布时间】:2015-11-03 03:33:06
【问题描述】:
我正在尝试将 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 从 350 到 10(0 或 360,纵向)之间的 DeviceAngle 获取 MyButton 旋转角度值(0 或未旋转)并将其用作参考。
即使我仍然怀疑,我还是放弃了之前的案例。浮动似乎适用于其他方向,我认为问题在于反向纵向方向的动画。按钮不应旋转 180 度,而应旋转 90 度或 -90 度。这是因为您无法在不通过任一横向选项的情况下将设备从纵向旋转到反向纵向。 (不能直接将 Portrait 旋转到 Reverse Portrait)。
经过多次不成功的尝试,我得出的结论是,在按钮旋转并且检测到的方向是横向或反向横向之后,我无法使用 MyButton 获取角度值。我想过创建另一个浮动来获取 Reverse Portrait MyButton 角度值,但是这个活动的方向设置为 Portrait,所以这没有意义。
因此,我需要在使用浮点数旋转 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 语句不能读取它?我不知道,我想知道我做错了什么。提前致谢。
【问题讨论】:
标签: android loops android-animation screen-orientation android-imagebutton