A summary of how Android implements screen rotation

Source: Internet
Author: User
Tags advantage

A summary of how Android implements screen rotation

This article mainly introduces the Android screen rotation method, the example summarizes the screen rotation related skills, with a certain reference value, the need for friends can refer to the

The example of this article summarizes the Android implementation of screen rotation method. Share to everyone for your reference. Specifically as follows:

Before introducing, we need to understand the mechanism by default for Android screen rotation:

By default, when the user's cell phone's gravity sensor is turned on, rotating the screen direction will cause the current activity to occur ondestroy-> OnCreate, which will reconstruct the current activity and interface layout, if the camera interface, The display is a cotton or black screen for some time. If you want to support screen rotation well in the overall UI design, it is recommended that you create Layout-land and layout-port two folders in res and put the horizontal and vertical layout files in the corresponding layout folders.

With that in place, we summarize the Android screen rotation method 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 code:

?

1 2 android:screenorientation= "Landscape" horizontal screen setting; android:screenorientation= "Portrait" vertical screen settings;

The advantage of this approach is that even if the screen spins, the activity does not oncreate again.

Disadvantage: The screen has only one direction.

2. Code Dynamic settings

If you need to change the screen settings dynamically, simply call the Setrequestedorientation () function in your code:

?

1 2 3 4 5 6 Setrequestedorientation (Activityinfo.screen_orientation_landscape); Horizontal screen setting setrequestedorientation (activityinfo.screen_orientation_portrait); Vertical screen setting setrequestedorientation (activityinfo.screen_orientation_unspecified); Default settings

The advantages of this method: can be set at will, to meet the requirements of our artificial change of the screen, at the same time to meet the screen UI different design needs;

Disadvantage: If you change the settings, then the activity will be destroyed, rebuilt, that is, re oncreate;

3. Rewrite onconfigurationchanged

If you don't want the activity to be constantly oncreate when you rotate the screen (which often results in a screen switch), you can use this method:

First, add Configchanges to the Androidmainfest.xml:

?

1 2 3 <activity android:name= ". Test "android:configchanges=" Orientation|keyboard > </activity>

Note that Keyboardhidden indicates that keyboard accessibility is hidden, and if your development API level is equal to or higher than 13, you need to set up screensize because ScreenSize will change when the screen rotates;

?

1 Android:configchanges= "Keyboardhidden|orientation|screensize"

Then, rewrite the Onconfigurationchanged method in the activity, which will be monitored when the screen rotates:

?

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 is done here} else {//Nothing need To be doing here}}

The advantage of this method: we can monitor the change of the screen rotation at any time, and corresponding operation is made;

Disadvantage: It can only rotate 90 degrees at a time, if it rotates 180 degrees, the onconfigurationchanged function will not be invoked.

4. With Orientationeventlistener, custom rotation listening settings

If you want to be more perfect, more complete control of monitoring screen rotation changes, such as the turn screen do not want to oncreate, especially in the camera interface, do not want to rotate preview screen, such as cotton, black screen, then, you can 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, customize the Listener class

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The private final void Startorientationchangelistener () {Morientatio Nlistener = new Orientationeventlistener (this) {@Override the 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 (); }

Called in OnCreate ():

?

1 Startorientationchangelistener ();

The advantage of this method: You can at any time accurately monitor the screen rotation changes in the state, you can dynamically change the state of the screen at any time;

Note: For camera, you can set the initialization to horizontal or vertical screen, and then provide rotation monitoring, so that you can get the screen rotation, so that you do the appropriate operation, and will not appear oncreate the current activity caused by the cotton and the short black screen switch.

I hope this article will help you with your Android program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.