Before we introduce, we need to understand the mechanism of Android screen rotation by default:
By default, when the gravity sensor of a user's phone is turned on, rotating the screen orientation causes the current activity to ondestroy-> OnCreate, which reconstructs the current activity and interface layout, if the camera interface is a period of inactivity or a black screen. If you are in the horizontal screen UI design, then want to support screen rotation well, it is recommended to set up in res Layout-land and layout-port two folders, the layout of the screen and vertical screen files into the corresponding layouts folder.
With this in view, we summarize the screen rotation methods of Android as follows:
1,androidmanifest.xml settings
If you simply want to set up a horizontal or vertical screen, you only need to add the screen and the line code:
android:screenorientation= "Landscape" horizontal screen setting; android:screenorientation= "Portrait" vertical screen setting;
The advantage of this approach is that the activity does not re-oncreate even if the screen is rotated.
Cons: The screen has only one direction.
2, Code dynamic settings
If you need to dynamically change the screen settings, then just call the Setrequestedorientation () function in your code:
Setrequestedorientation (Activityinfo.screen_orientation_landscape);//Horizontal screen setting
Setrequestedorientation (activityinfo.screen_orientation_portrait);//Vertical screen settings
Setrequestedorientation (activityinfo.screen_orientation_unspecified);//default settings
The advantages of this method: can be arbitrarily dynamically set up to meet our human needs to change the screen, at the same time to meet the different screen UI design requirements;
Cons: If you change the settings, then the activity will be destroyed, re-built, that is, re-oncreate;
3, rewrite onconfigurationchanged
This method can be used if you do not want the activity to be constantly oncreate when rotating the screen (which often causes a lag in screen switching):
First, add the configchanges in Androidmainfest.xml:
<activity android:name= ". Test " android:configchanges=" Orientation|keyboard "></activity>
AttentionKeyboardhidden saysKeyboard accessibility is hidden, if your development API level is equal to or higher than 13, you also need to set screensize, because ScreenSize will change when the screen rotates;
Android:configchanges= "Keyboardhidden|orientation|screensize"
Then, rewrite the Onconfigurationchanged method in the activity, which will be monitored when the screen rotation changes:
<pre class= "java" name= "code" >public void Onconfigurationchanged (Configuration newconfig) { //TODO Auto-generated method Stubsuper.onconfigurationchanged (newconfig); if (newconfig.orientation==configuration.orientation_landscape) { //Nothing need to is done here } else { / /Nothing need to is done here } }
The advantage of this method: we can listen to the screen rotation changes at any time, and corresponding to the corresponding operation; disadvantage: it can only rotate 90 degrees at a time, if you rotate 180 degrees, the onconfigurationchanged function will not be called. 4, combined with orientationeventlistener, custom rotation monitoring settings if you want to be more perfect, more complete control of the monitoring screen rotation changes, for example, do not want to re-oncreate the screen, especially in the camera interface, Do not want to appear when rotating preview screen of the lag, black screen, and so on, you can try: First, create a Orientationeventlistener object:
Private Orientationeventlistener Morientationlistener; Screen Orientation Listener
Private Boolean mscreenprotrait = True;private Boolean mcurrentorient = false;
Then, customize the screen change callback interface
Abstract protected void orientationchanged (int orientation);//screen Orientation Change Event
Finally, the custom listener class
Private final void Startorientationchangelistener () {morientationlistener = new Orientationeventlistener (this) { @Override public void onorientationchanged (int rotation) {if ((rotation >= 0) &am p;& (Rotation <= 45)) | | (Rotation >= 315) | | ((rotation>=135) && (rotation<=225))) {//portrait mcurrentorient = true; if (mcurrentorient!=mscreenprotrait) {mscreenprotrait = mcurrentorient; Orientationchanged (activityinfo.screen_orientation_portrait); LOG.D (TAG, "screen orientation changed from Landscape to portrait!"); }} else if (((Rotation >) && (Rotation < 135)) | | ((rotation>225) && (rotation<315))) {//landscape mcurrentorient = false; if (mcurrentorient!=mscreenprotrait) {Mscreenprotrait = mcurrentorient; Orientationchanged (Activityinfo.screen_orientation_landscape); LOG.D (TAG, "screen orientation changed from Portrait to landscape!"); } } } }; Morientationlistener.enable (); }
Called in OnCreate ():
Startorientationchangelistener ();
The advantages of this method: You can at any time accurately monitor the state of the rotation of the screen, you can dynamically change the status of vertical and horizontal screen at any time; note: For camera, you can set the initial screen or vertical display, and then provide external rotation monitoring, so that you can get the screen rotation state, let you do the corresponding operation, There will be no re-oncreate of the current activity caused by the lag and a short black screen switch.
android--Screen Rotation Method summary