检测屏幕从0旋转到180或从90旋转到270的正确方法是什么? [英] What's the correct way to detect a screen rotation from 0 to 180, or 90 to 270?

查看:757
本文介绍了检测屏幕从0旋转到180或从90旋转到270的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据屏幕的旋转方向调整相机预览的方向.

I'm attempting to orient a camera preview according to the screen's rotation.

我注意到,当在尺寸匹配的方向(例如0-> 180和90-> 270)之间直接旋转时,配置不会更改,活动也不会重新启动.

I've noticed that when rotating directly between orientations with matching dimensions (so, 0 -> 180, and 90 -> 270), the configuration is not changed and the activity is not restarted.

我当前在Activity.onCreate()中使用Display.getRotation(),但是此信息已过时.

I'm currently using Display.getRotation() within Activity.onCreate(), but this information becomes out of date.

检测此更改的最佳方法是什么,以便我可以适当地重新定向相机预览?

推荐答案

使用 OrientationEventListener

在您的SurfaceHolder.Callback

In your SurfaceHolder.Callback

    orientationListener = createOrientationListener();

    private OrientationEventListener createOrientationListener() {
            return new OrientationEventListener(getActivity()) {
                public void onOrientationChanged(int orientation) {
                    try {
                        if (orientation != OrientationEventListener.ORIENTATION_UNKNOWN) {
                            setCameraDisplayOrientation(getActivity().getWindowManager().getDefaultDisplay().getRotation());
                        }
                    } catch (Exception e) {
                        Log.w(TAG, "Error while onOrientationChanged", e);
                    }
                }
            };
        }


     @Override
     public void surfaceCreated(SurfaceHolder holder) {
         orientationListener.enable();
     }


    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        orientationListener.disable();
    }

您的变更轮换方法必须管理不必要的两次轮换

Your change rotation method has to manage unneeded double rotations

public void setCameraDisplayOrientation(int displayRotation) {
            int degrees = 0;
            switch (displayRotation) {
                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;
            }

            int result;
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                result = (cameraInfo.orientation + degrees) % 360;
                result = (360 - result) % 360;  // compensate the mirror
            } else {  // back-facing
                result = (cameraInfo.orientation - degrees + 360) % 360;
            }
            if(result != currentSetRotation) {
                    currentSetRotation = result;
                    camera.setDisplayOrientation(result);
                    Log.d(TAG,"For displayRotation "+displayRotation+" we set a camera rotation of "+result);

            }
    }

另请参阅:将手机快速旋转180度,相机预览颠倒了

这篇关于检测屏幕从0旋转到180或从90旋转到270的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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