What's the correct way to detect a screen rotation from 0 to 180, or 90 to 270?

夙愿已清 提交于 2019-12-11 05:59:08

问题


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

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.

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

What's the best way to detect this change so that I can appropriately re-orient my camera preview?


回答1:


Use an OrientationEventListener

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

            }
    }

See also: Rotating phone quickly 180 degrees, camera preview turns upside down



来源:https://stackoverflow.com/questions/20542756/whats-the-correct-way-to-detect-a-screen-rotation-from-0-to-180-or-90-to-270

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!