Android screen rotation method summary
Android screen rotation method summary
This article mainly introduces how to implement screen rotation in Android. The example summarizes the techniques related to screen rotation and has some reference value. For more information, see
This example summarizes how Android can rotate the screen. Share it with you for your reference. The details are as follows:
Before introduction, we need to first understand the android screen rotation mechanism by default:
By default, when the gravity Sensor of your mobile phone is turned on, rotating the screen will lead to onDestroy-> onCreate in the current activity, which will re-construct the current activity and interface layout, if you are on the Camera interface, it may be stuck or black screen for a while. If you want to support screen rotation well in the horizontal and vertical UI design, we recommend that you create the layout-land and layout-port folders in res, place the layout files of the horizontal and vertical screens in the corresponding layout folder.
After learning about this, we will summarize the screen rotation methods of android as follows:
1. AndroidManifest. xml settings
If you only want to set the landscape or landscape, you only need to add the Code:
?
1 2 |
Android: screenOrientation = "landscape" landscape settings; Android: screenOrientation = "portrait" portrait screen setting; |
Advantages of this method: even if the screen is rotated, the Activity will not re-onCreate.
Disadvantage: the screen has only one direction.
2. Dynamic code settings
If you need to dynamically change the Screen Settings, you only need to call the setRequestedOrientation () function in the Code:
?
1 2 3 4 5 6 |
SetRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE ); // Landscape settings SetRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_PORTRAIT ); // Set the portrait Screen SetRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_UNSPECIFIED ); // Default settings |
Advantages of this method: it can be set dynamically at will to meet our requirements for manual changes to the horizontal and vertical screens, and to meet different design requirements of the horizontal and vertical screens UI;
Disadvantage: If you change the settings, the Activity will be destroyed and re-built, that is, re-onCreate;
3. Override onConfigurationChanged
If you do not want to rotate the screen when the Activity is constantly onCreate (this often causes lag during screen switching), you can use this method:
First, add configChanges in AndroidMainfest. xml:
?
1 2 3 |
<Activity android: name = ". Test" Android: configChanges = "orientation | keyboard"> </Activity> |
Note: keyboardHidden indicates that the keyboard auxiliary function is hidden. If your development API level is equal to or higher than 13, you also need to set the screenSize because the screenSize will change when the screen is rotated;
?
1 |
Android: configChanges = "keyboardHidden | orientation | screenSize" |
Then, override the onConfigurationChanged method in the Activity. This method will be listened to when the screen rotation changes:
?
1 2 3 4 5 6 7 8 |
Public void onConfigurationChanged (Configuration newConfig ){ // TODO Auto-generated method stubsuper. onConfigurationChanged (newConfig ); If (newConfig. orientation = Configuration. ORIENTATION_LANDSCAPE ){ // Nothing need to be done here } Else { // Nothing need to be done here } } |
Advantages of this method: We can monitor screen rotation changes at any time and perform corresponding operations;
Disadvantage: it can only Rotate 90 degrees at a time. If it rotates 180 degrees at a time, the onConfigurationChanged function will not be called.
4. Custom rotation listening settings with OrientationEventListener
If you want to be more perfect and have full control over the monitoring of screen rotation changes, for example, you do not want to re-create onCreate during screen conversion, especially on the Camera interface, if you do not want to encounter problems such as choppy screen and black screen during preview rotation, try:
First, create the OrientationEventListener object:
?
1 2 3 4 |
Private OrientationEventListener mOrientationListener; // Screen orientation listener Private boolean mScreenProtrait = true; Private boolean mCurrentOrient = false; |
Then, customize the screen change callback Interface
?
1 2 |
Abstract protected void OrientationChanged (int orientation ); // Screen orientation change event |
Finally, the custom listener class
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
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> 45) & (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 (); } |
Call in onCreate:
?
1 |
StartOrientationChangeListener (); |
Advantages of this method: You can monitor the status of screen rotation changes at any time and dynamically change the status at any time;
Note: For Camera, you can set initialization to a landscape or landscape screen, and then provide a rotation listener. This allows you to get the screen rotation status and perform corresponding operations, there will be no choppy and short-lived black screen switching caused by re-running the current onCreate Activity.
I hope this article will help you design your Android program.