Horizontal and vertical screen correlation

AndroidManifest:

android:configChanges="orientation|screenSize"

At first, set this to go vertically and horizontally. onConfigurationChanged

(in fact, it doesn't matter whether this one is written or not. I haven't found anything else yet.)


Customize the myroeientationeventlistener inner class

//Inherit the OrientationEventListener class to monitor the rotation of mobile phone
    public class MyOrientationEventListener extends OrientationEventListener {

        public MyOrientationEventListener(Context context) {
            super(context);
        }

        public MyOrientationEventListener(Context context, int rate) {
            super(context, rate);
        }

        @Override
        public void onOrientationChanged(int i) {
            //i means the offset angle is - 1. If the mobile phone is placed horizontally and rotated anticlockwise, it will increase slowly
            int screenOrientation = getResources().getConfiguration().orientation;
            if (((i >= 0) && (i < 45)) || (i > 315)) {
                //Set up vertical screen
                if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
                        && i != ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                }
            } else if (i > 255 && i < 315) {
                //Setting the horizontal screen
                if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }
            } else if (i > 45 && i < 135) {
                //Set reverse screen
                if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                }
            } else if (i > 135 && i < 225) {
                //Reverse vertical screen
                if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
                }
            }


        }
    }


The top definition (this,,, which is easy for me to paste later)

//Horizontal screen correlation
    private MyOrientationEventListener mListener;
    private int orientation;

initialization:

orientation = getResources().getConfiguration().orientation;
        mListener = new MyOrientationEventListener(this);
        boolean autoRotateOn = (Settings.System.getInt(getContentResolver(),
                Settings.System.ACCELEROMETER_ROTATION, 0) == 1);
        if (autoRotateOn) {
            mListener.enable();
        }

OK, that's it

----------------------------------------------------------------------------------------------------------------------------

Suggestions:

Use more relative layout (although I've read other people's articles before, I don't care if I'm not in the actual project, hahaha)

In the Project directory, create a new layout land folder, in which you can create an xml file with the same name to drop different layouts when you switch between horizontal and vertical screens (rebind the layouts in the onConfigurationChanged callback)


Tags: Mobile Android xml

Posted on Wed, 27 May 2020 08:51:09 -0700 by kinaski