如何检测android中的颠倒方向?

时间:2012-09-28 10:09:12

标签: android android-sensors android-orientation

在我的Android应用程序中,我有全景图像,我根据手机运动使用TYPE_ORIENTATION传感器旋转此图像,它适用于风景和potrait。这是旋转逻辑的代码。

@Override
public void onSensorChanged(SensorEvent event) {
  switch (event.sensor.getType()) {
  case Sensor.TYPE_ORIENTATION:

    float[] insideval = getValPoints();
    event.values[1]=(float) (event.values[1]*2.8);
    event.values[2]=(float) (event.values[2]*2.8);
    setValPoints(event.values.clone());
        if (insideval != null) {
            float check= insideval[0] - event.values[0];            
            if (-1<=check && check <=1) {
                //Animation stoped      
                this.stopAnimation();
            }else{
                if (getdefaltDeviceOrientation() == ORENTATION_LANDSCAPE) {
                     // Natural Orientation is landscape
                      if (getcurrentDeviceOrientation() == 1) {
                        //ORIENTATION_PORTRAIT
                        startPoint = CGPoint.CGPointMake(insideval[2], insideval[1]);
                                if (insideval[0] < event.values[0]) {
                                    endPoint = CGPoint.CGPointMake(insideval[2]+ event.values[2], insideval[1]);
                                } else if (insideval[0] > event.values[0]) {
                                    endPoint = CGPoint.CGPointMake(insideval[2]- event.values[2], insideval[1]);
                                }
                            } else if (getcurrentDeviceOrientation() == 2) {
                                //Log.i(TAG, "ORIENTATION_LANDSCAPE");
                                startPoint = CGPoint.CGPointMake(insideval[1], insideval[2]);
                                if (insideval[0] < event.values[0]) {
                                    endPoint = CGPoint.CGPointMake(insideval[1]- event.values[1], insideval[2]);
                                } else if (insideval[0] > event.values[0]) {
                                    endPoint = CGPoint.CGPointMake( insideval[1]+event.values[1] ,insideval[2]);                    
                                }
                            }
                        } else if (getdefaltDeviceOrientation() == ORENTATION_PROTRAIT) {
                            // Natural Orientation is portrait

                            if (getcurrentDeviceOrientation() == 1) {
                                //Log.i(TAG, "ORIENTATION_PORTRAIT");
                                startPoint = CGPoint.CGPointMake(insideval[1], insideval[2]);
                                if (insideval[0] < event.values[0]) {
                                    endPoint = CGPoint.CGPointMake(insideval[1]+ event.values[1], insideval[2]);
                                } else if (insideval[0] > event.values[0]) {
                                    endPoint = CGPoint.CGPointMake(insideval[1]- event.values[1], insideval[2]);
                                }
                            } else if (getcurrentDeviceOrientation() == 2) {
                                //Log.i(TAG, "ORIENTATION_LANDSCAPE");
                                startPoint = CGPoint.CGPointMake(insideval[2], insideval[1]);
                                if (insideval[0] < event.values[0]) {
                                    endPoint = CGPoint.CGPointMake(insideval[2]- event.values[2], insideval[1]);
                                } else if (insideval[0] > event.values[0]) {
                                    endPoint = CGPoint.CGPointMake(insideval[2]+event.values[2],insideval[1]);
                                }
                            }
                        }
                        this.drawView();
                    }
                }

                break;
}
}

所以我的问题是如果设备的方向上下颠倒图像将旋转到错误的方向(完全相反的方向)。我如何解决这个问题以及当设备方向发生颠倒时实际发生了什么? 感谢。

2 个答案:

答案 0 :(得分:6)

使用设备当前轮换http://developer.android.com/reference/android/view/Display.html#getRotation()

返回4个选项之一ROTATION_0,ROTATION_90,ROTATION_180,ROTATION_270

答案 1 :(得分:0)

是的,答案是正确的,但如何从链接的文档中获取展示信息并不明显。

您无法从资源/配置中获得此信息。相反,您应该使用:

int rotation = getWindowManager().getDefaultDisplay().getRotation();