Android-screen rotation Summary

Source: Internet
Author: User

Android-screen rotation Summary

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:

Android: screenOrientation = "landscape" horizontal screen setting; android: screenOrientation = "portrait" vertical 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:

SetRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE); // set the landscape

SetRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_PORTRAIT); // set the portrait Screen

SetRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_UNSPECIFIED); // default setting

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:

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; 
android:configChanges="keyboardHidden|orientation|screenSize"

Then, override the onConfigurationChanged method in the Activity. This method will be listened to when the screen rotation changes:
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 the screen rotation changes at any time and perform corresponding operations. Disadvantages: 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. Combined with OrientationEventListener, you can customize the rotation listening settings. If you want to be more perfect, you have full control over the monitoring screen rotation changes. For example, you do not want to re-create onCreate when switching the screen, especially on the Camera interface, if you do not want to encounter problems such as screen freezing and black screen rotation during preview rotation, try: first, create the 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) && (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:
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 the initialization to a horizontal or vertical screen, and then provide a rotation listener for external users. 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.

Related Article

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.