如何相对于设备的主体获得活动的真实轮换

时间:2014-10-20 13:53:31

标签: android rotation orientation screen-orientation

我的应用程序使用相机。要以正确的方式显示相机的预览,我必须考虑相对于物理设备方向的活动方向。 I. e。如果活动方向被锁定且永不改变,我不需要采取任何进一步的步骤 - 随着设备的旋转,预览图像将相应地旋转。但是,想象一下我的活动可以改变方向。您旋转设备 - 并预览 - 直到您达到纵向模式(假设它原来是风景),此时活动旋转以适应新的方向。但是预览图像随之旋转,现在它与相机和周围的现实不同步。我必须做的是确定活动方向并相应地旋转图像。

似乎可以使用Display.getRotation()。但显然,它不能:https://groups.google.com/forum/#!topic/android-developers/ij_0QbApKKc

问题是Android API并未解决原点问题。某些平板电脑在正常方向上返回旋转0(横向,音量按钮向上),而其他平板电脑(如我的Nexus 7 2013)则返回1。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您可以使用OrientationEventListener像这样的东西确定方向

    mOrientationEventListener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_NORMAL) {

            @Override
            public void onOrientationChanged(int orientation) {

                // determine our orientation based on sensor response

                Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();                                        

                if (display.getOrientation() == Surface.ROTATION_0) {   // landscape oriented devices
                    isLandscapeOriented = true;
                    if (orientation >= 315 || orientation < 45) {
                        if (mOrientation != ORIENTATION_LANDSCAPE_NORMAL) {                         
                            mOrientation = ORIENTATION_LANDSCAPE_NORMAL;
                        }
                    } else if (orientation < 315 && orientation >= 225) {
                        if (mOrientation != ORIENTATION_PORTRAIT_INVERTED) {
                            mOrientation = ORIENTATION_PORTRAIT_INVERTED;
                        }                       
                    } else if (orientation < 225 && orientation >= 135) {
                        if (mOrientation != ORIENTATION_LANDSCAPE_INVERTED) {
                            mOrientation = ORIENTATION_LANDSCAPE_INVERTED;
                        }                       
                    } else if (orientation <135 && orientation > 45) { 
                        if (mOrientation != ORIENTATION_PORTRAIT_NORMAL) {
                            mOrientation = ORIENTATION_PORTRAIT_NORMAL;
                        }                       
                    }                       
                } else {  // portrait oriented devices

                    if (orientation >= 315 || orientation < 45) {
                        if (mOrientation != ORIENTATION_PORTRAIT_NORMAL) {                          
                            mOrientation = ORIENTATION_PORTRAIT_NORMAL;
                        }
                    } else if (orientation < 315 && orientation >= 225) {
                        if (mOrientation != ORIENTATION_LANDSCAPE_NORMAL) {
                            mOrientation = ORIENTATION_LANDSCAPE_NORMAL;
                        }                       
                    } else if (orientation < 225 && orientation >= 135) {
                        if (mOrientation != ORIENTATION_PORTRAIT_INVERTED) {
                            mOrientation = ORIENTATION_PORTRAIT_INVERTED;
                        }                       
                    } else if (orientation <135 && orientation > 45) { 
                        if (mOrientation != ORIENTATION_LANDSCAPE_INVERTED) {
                            mOrientation = ORIENTATION_LANDSCAPE_INVERTED;
                        }                       
                    }
                }


            }
        };
    }
    if (mOrientationEventListener.canDetectOrientation()) {
        mOrientationEventListener.enable();
    }

不要忘记调用OrientationEventListener.disable();

修改

    public static void setCameraDisplayOrientation(Activity activity,
         int cameraId, android.hardware.Camera camera) {

     android.hardware.Camera.CameraInfo info =
             new android.hardware.Camera.CameraInfo();
     android.hardware.Camera.getCameraInfo(cameraId, info);

     int result;
     if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
         result = (info.orientation + degrees) % 360;
         result = (360 - result) % 360;  // compensate the mirror
     } else {  // back-facing
       int rotation = activity.getWindowManager().getDefaultDisplay()
             .getRotation();
       int degrees = 0;
       switch (rotation) {
         case Surface.ROTATION_0: degrees = 0; break;
         case Surface.ROTATION_90: degrees = 90; break;
         case Surface.ROTATION_180: degrees = 180; break;
         case Surface.ROTATION_270: degrees = 270; break;
       }
         result = (info.orientation - degrees + 360) % 360;
     }
     camera.setDisplayOrientation(result);
 }