如何旋转从不同角度的看法? [英] How to rotate a view from different angles?

查看:196
本文介绍了如何旋转从不同角度的看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想180度,因此反向纵向匹配来旋转的ImageButton。当我这样做的方式一样的其他取向的变化,其结果是完美的,但不是动画

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

我认为出现这种情况,因为浮子MyButtonCurrentAngle得到用作MyButton旋转角的值(0或不旋转)从DeviceAngle 350和10(0或360,纵向取向)之间,并使用它作为参考。

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.

尽管我还在怀疑,我丢弃previous情况。浮球似乎与其他方位很好地工作,我认为这个问题是反向纵向方向动画。按钮不应由180旋转,而是由90或-90度。这是因为你不能从纵向旋转装置,纵向反转,而无需通过任何景观选项去。 (不能旋转肖像直接纵向反转)。

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).

多次尝试失败后,我来到了,我不能用用作MyButton得到角度值的结论,按钮被旋转和检测到的方向是横向或横向反转之后。我想过创造另一个浮动来获得反向肖像用作MyButton角度值,但这一活动的方向设置为纵向,所以这是没有意义的。

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.

因此​​,我需要得到用作MyButton转角它被旋转之后与浮子,使用该值作为一个循环条件,并根据结果,90度或在两个不同的动画-90度旋转。这是我最新的方法问题:

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

基本上,这种处理来自景观和景观反向扭转纵向设备的方向。这并不会触发任何动画,所以MyButtonCurrentAngle浮动角度值从来没有改变或无法被检测到?为什么不能if语句读它?我不知道,我想知道我做错了。先谢谢了。

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.

推荐答案

解决方案:

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

这篇关于如何旋转从不同角度的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆