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, This will re-construct the current activity and interface layout, if the camera interface, the performance of a lag or black screen for a period of time. 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 mind, we summarize the screen rotation method for Android: 1,androidmanifest.xml settings if you want to set up a horizontal or vertical screen only, you only need to add the screen code: android:screenorientation= " Landscape "horizontal screen setting; android:screenorientation=" Portrait "vertical screen setting; The advantage of this method is that the activity will not oncreate again 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 Setting this method advantage: can be set at random, to meet our human changes in the vertical and horizontal screen requirements, At the same time to meet the different design requirements of the screen UI; Cons: If you change the settings, the activity will be destroyed, rebuilt, re-oncreate;3, Rewrite onconfigurationchanged You can use this method if you don't want the activity to be constantly oncreate when you rotate the screen (which often causes a lag in screen switching): First, Add configchanges:</activity> in Androidmainfest.xml Note that the Keyboardhidden represents the keyDisk assist function is hidden, if your development API level is equal to or higher than 13, also need to set screensize, because ScreenSize will change when the screen rotates;? 1android:configchanges= "keyboardhidden| Orientation|screensize "Then, in activity, rewrite the Onconfigurationchanged method, which will be monitored when the screen rotation changes:? <pre class=" Java "name= "Code" >public void Onconfigurationchanged (Configuration newconfig) {//TODO auto-generated method Stubsuper.onc Onfigurationchanged (Newconfig); if (newconfig.orientation==configuration.orientation_landscape) {//Nothing need } else {//Nothing need to is done here}} </pre><pre class= "Brush:java; "></pre> Advantages 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 lag, black screen, and so on, you can try: First, create Orientationeventlistener object: <pre class= "Brush:java;" >private Orientationeventlistener Morientationlistener; Screen orientation Listener</pre><pre class= "Brush:java;" >private Boolean mscreenprotrait = true;private Boolean mcurrentorient = False;</pre> Then, customize the screen change callback interface <br> <pre class= "Brush:java;" >abstract protected void orientationchanged (int orientation);//screen Orientation Change event</pre> Finally, Custom Listener Class <br><pre class= "Brush:java;" >private final void Startorientationchangelistener () {morientationlistener = new Orientationeventlistener (this) {@Override public void onorientationchanged (int rotation) {if ((rotation >= 0) && (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 (); }</pre> called in OnCreate (): <pre class= "Brush:java;" >startorientationchangelistener (); </pre> 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 portrait screen, and then provide rotation monitoring, so that you can get the display rotation state , allowing you to do the right things without re-oncreate the current activity caused by the lag and the short black screen switch. <br> <br> <p> </p><p> </p>
android--Screen Rotation Method summary