Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何获得活动相对于设备的真实旋转';身体_Android_Rotation_Orientation_Screen Orientation - Fatal编程技术网

Android 如何获得活动相对于设备的真实旋转';身体

Android 如何获得活动相对于设备的真实旋转';身体,android,rotation,orientation,screen-orientation,Android,Rotation,Orientation,Screen Orientation,我的应用程序使用照相机。要以正确的方式显示相机的预览,我必须说明相对于物理设备方向的活动方向。即。如果活动方向已锁定且从未更改,我无需采取任何进一步的步骤-随着设备旋转,预览图像将相应旋转。然而,想象一下我的活动被允许改变方向。旋转设备并预览,直到达到纵向模式(假设它最初是横向模式),此时活动将旋转以适应新方向。但预览图像会随之旋转,现在它与相机和周围现实不同步。我必须做的是确定活动方向并相应地旋转图像 似乎可以使用Display.getRotation()来实现这一点。但显然,它不能: 问题在

我的应用程序使用照相机。要以正确的方式显示相机的预览,我必须说明相对于物理设备方向的活动方向。即。如果活动方向已锁定且从未更改,我无需采取任何进一步的步骤-随着设备旋转,预览图像将相应旋转。然而,想象一下我的活动被允许改变方向。旋转设备并预览,直到达到纵向模式(假设它最初是横向模式),此时活动将旋转以适应新方向。但预览图像会随之旋转,现在它与相机和周围现实不同步。我必须做的是确定活动方向并相应地旋转图像

似乎可以使用
Display.getRotation()
来实现这一点。但显然,它不能:

问题在于,Android API并没有固定原点。一些平板电脑在正常方向(横向、音量按钮向上)返回0的旋转,而另一些平板电脑(如我的Nexus 7 2013)返回1


我怎样才能解决这个问题呢?

你可以用这样的东西来确定方向

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

如果不清楚你想要什么,不妨更新你的问题标题和内容。旋转或方向。很难弄清楚你在问什么。但听起来你们只需要知道你们的活动是以纵向还是横向模式运行。这很容易通过比较屏幕的宽度/高度来实现。如果高度>宽度,则为纵向模式,否则为横向模式。@GregMiller:对不起,我没意识到我的英语那么差。不知道如何陈述这个问题。不,你搞错了,我不是这个意思。这不是我开发Android的第一年,我当然不需要为这么简单的事情提出新的问题,更不用说悬赏了。简而言之:我需要的正是Display.getRotation()所做的。不多也不少。问题是,
Display.getRotation()
不起作用,问题中描述了它不起作用的确切方式。您是否需要知道相对于相机默认方向的当前方向?这里有一个解决了的问题似乎也有同样的问题:@GregMiller:谢谢,这就解决了问题。我应该在什么时候调用
禁用
,如果不调用,会发生什么?另外,您的代码不完整。在哪里设置此侦听器?哪个传感器,如何初始化它?实际上你不需要设置侦听器AnyWare-mOrientationEventListener.enable()就足够了。你可以把它放在你的活动中。我在onCreate中启用它,在onDestroy中禁用它。不调用disable是一种不好的做法,因为MorentationEventListener的实例将占用您的内存资源,直到应用程序关闭为止?实施中有什么好运气吗?或者你需要一些额外的指导?只是试了一下。不幸的是,这绝对不是我需要的。重申我在问题中已经说过的话:我需要将相机的图像与屏幕对齐。因此,我所需要的就是检测活动相对于相机(即设备主体)的方向。
    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);
 }