How to rotate a view from different angles?

浪尽此生 提交于 2019-12-13 19:36:31

问题


I am trying to rotate an ImageButton by 180 degrees so it matches the reverse portrait orientation. When I did this the same way as the other orientation changes, the result was perfect, but not the animation.

 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();
           }          

I thought this happens, because the float MyButtonCurrentAngle gets MyButton rotation angle value (0 or not rotated) from the DeviceAngle between 350 and 10 (0 or 360, Portrait orientation) and uses it as a reference.

Even though I'm still doubting, I discarded the previous case. The float seemed to work well with the other orientations and I think the problem is the animation for the Reverse Portrait orientation. The button shouldn't be rotated by 180, but by either 90 or -90 degrees. This is because you can't rotate a device from Portrait to Reverse Portrait without going through either landscape option. (Can't rotate Portrait to Reverse Portrait directly).

After many unsuccessful attempts, I came to the conclusion that I can't use MyButton gotten angle value, after the button has been rotated and the orientation detected is either Landscape or Reverse Landscape. I thought about the creation of another float to get the Reverse Portrait MyButton angle value, but this activity's orientation is set to Portrait, so this doesn't make sense.

Therefore, I need to get MyButton rotation angle after it's been rotated with a float, using this values as a loop condition, and depending on the result, rotate it by 90 degrees or -90 degrees in two different animations. This was my latest approach to the issue:

    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();
    }
}

Basically, this handles the device orientation from landscape and reverse landscape to reverse portrait. This didn't trigger any animation, so MyButtonCurrentAngle float angle value never changed or couldn't be detected? Why can't the if statements read it? I don't know and I would like to know what I am doing wrong. Thanks in advance.


回答1:


Solution:

  OrientationEventListener OrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_UI) {
        static final int DELTA = 20;

        @Override 
        public void onOrientationChanged(int Angle) {
            float AngleDestination = ButtonDestination;
            if (angle > 90 - DELTA && angle < 90 + DELTA) {
                AngleDestination = -90;
            } else if (Angle > 180 - DELTA && Angle < 180 + DELTA) {
                AngleDestination = 180;
            } else if (Angle > 270 - DELTA && Angle < 270 + DELTA) {
                AngleDestination = 90;
            } else if (Angle > 360 - DELTA || Angle < DELTA) {
                AngleDestination = 0;
            } 
            if (AngleDestination != ButtonDestination) {
                ButtonDestination = AngleDestination;
                MyButton.animate().rotation(ButtonDestination).setDuration(100).start(); 
            } 
        } 
    }; 
    orientationEventListener.enable();
}



回答2:


If you found that this code was running (either by break-point debugging or a Logcat statement) then try running the animation on the UI thread.

YourActivity.runOnUiThread(new Runnable() {
    public void run() 
        MyButton.animate().rotationBy(90 - MyButtonCurrentAngle).setDuration(100).start();
    }
});

You might also need to call start on that.



来源:https://stackoverflow.com/questions/33473879/how-to-rotate-a-view-from-different-angles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!