This is the 7th day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

preface

Screen rotation mechanism;

By default, when the user’s phone’s gravity sensor is turned on and the screen is rotated, the current activity is destroyed -> onCreate, which restructures the current activity and interface layout. If the user is in a Camera interface, it freezes or goes black for a while.

Today, I will introduce the plane rotation;

A, screenOrientation attribute description

Android: screenOrientation attribute description:

  • Unspecified. The default value is unspecified, which is determined by the system and may differ from phone to phone
  • Landscape, mandatory landscape display, only one direction
  • Portrait: Forces portrait display in only one direction
  • Behind, in the same direction as the previous activity
  • Sensor, according to the direction of the physical sensor, the user rotates the direction of the phone 90 degrees, 180 degrees, 270 degrees, the activity will change, will restart the activity (whether the system is set to automatic screen rotation, the activity page will follow the sensor screen rotation)
  • SensorLandscape, landscape rotation, which means you can rotate up and down, in two directions, without restarting your activity
  • SensorPortrait rotation, which means you can rotate up and down in two directions without restarting your activity
  • Nosensor, the interface will not rotate when the device is rotated. The orientation of the initialization screen is controlled by the system (the activity page does not rotate regardless of whether the system is set to rotate automatically).
  • User: indicates the direction currently set by the user
  • Reverselscape, displayed opposite to normal landscape orientation (reverseLandscape)
  • ReversePortrait, shown in reverse to normal portrait (my Settings didn’t work)

Two, screen rotation details

1. Configuration file Settings

  • Androidmanifest.xml Settings;
  • The horizontal or vertical screen is fixed directly, and the rotation direction does not change. There is only one direction (meaning that the rotation direction does not change), of course, it will not restart the activity when the phone rotates.
<activity android:name=".test1" android:screenOrientation="landscape" /><! Landscape Settings - > - < activity android: name = "test2" android: screenOrientation = "portrait" / > <! Portrait Settings -->Copy the code

2. Code Settings

The effect of calling the setRequestedOrientation() function is to add and to

If the current direction is inconsistent with the set direction, the activity will restart. If the current direction is inconsistent, the activity will not restart.

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // Set setRequestedOrientation(Activityinfo. SCREEN_ORIENTATION_PORTRAIT); // Set The portrait orientation setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); // Default SettingsCopy the code

Note:

Don’t want to the activity is restarted, can add in AndroidManifest android: configChanges (orientation | screenSize the two be sure to add)

 <activity

            android:name=".MainActivity"

            android:screenOrientation="sensor"

            android:configChanges="keyboardHidden|orientation|screenSize">
Copy the code

3. Monitor screen rotation changes

Override the onConfigurationChanged method

    @Override

    public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);

        Log.d(TAG, "onConfigurationChanged");

    }
Copy the code

This method will be called when the screen rotation changes and can do what we want to do when the screen changes without restarting the activity. But it can only be rotated 90 degrees at a time. If rotated 180 degrees at a time, the onConfigurationChanged function is not called;

4. Customize rotation listening Settings

Want to be more perfect, more complete control monitor screen rotation changes, on the custom rotation monitor

(1) Create a class to inherit OrientationEventListener

(2) Enable and disable monitoring

You can create an object of the MyOrientationDetector class in your activity. Note that the enable() and disable() methods of the parent class enable and disable the listener.

Therefore, you can call the Enable method of the MyOrientationDetector object in the activity’s onResume(), Call the Disable method of the MyOrientationDetector object in onPause() to finish the vehicle function;

(3) Monitor the specified screen rotation Angle

The onOrientationChanged parameter orientation of MyOrientationDetector is a variable ranging from 0 to 359. If you only want to process four directions, add a judgment:

OrientationEventListener mOrientationListener; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mOrientationListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) { @Override public void onOrientationChanged(int orientation) { if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) { return; / / phone flat, the detection Angle less than effective} / / only have four Angle change if (orientation > 350 | | orientation < 10) {/ / 0 degrees orientation = 0; } else if (orientation > 80 && orientation < 100) {//90 ° orientation = 90; } else if (orientation > 170 && orientation < 190) {// orientation = 180; } else if (orientation > 260 && orientation < 280) {// orientation = 270; } else { return; } Log.v(DEBUG_TAG,"Orientation changed to " + orientation); }}; if (mOrientationListener.canDetectOrientation()) { Log.v(DEBUG_TAG, "Can detect orientation"); mOrientationListener.enable(); } else { Log.v(DEBUG_TAG, "Cannot detect orientation"); mOrientationListener.disable(); } } @Override protected void onDestroy() { super.onDestroy(); mOrientationListener.disable(); }Copy the code

conclusion

At the end of the year, many people need to find jobs or write graduation projects. If you don’t understand, send me a private letter. Maybe I can give you some help and suggestions.

We work together to make progress;