Detailed explanation of the Android screen direction, detailed explanation of the android Screen

Source: Internet
Author: User

Detailed explanation of the Android screen direction, detailed explanation of the android Screen

The screen direction is for Activity, so you canAndroidManifest. xmlFile<Activity>MarkedScreenOrientationSet attributes, for example:


<Activity android: name = ". SketchpadActivity" android: screenOrientation = "landscape"/> <! -- Make the Activity always display as a horizontal screen -->

The screenOrientations attribute has a total of 7 optional values (constants are defined in the android. content. pm. ActivityInfo class ):

The preceding configuration values are reflected in Activity. in the return value of the getRequestedOrientation () method, the corresponding setRequestedOrientation () method can dynamically change the value of this attribute through API, the following example shows how to switch between landscape and landscape screens:


/** Use the API to dynamically change the display direction of the current screen */public void apiChangeOrientation () {// get the int orient = getRequestedOrientation (); Logger. get (). I ("orientation:" + MyUtils. getOrientationName (orient )); // If landscape or portrait is not clear, the actual display direction is confirmed by the wide/high ratio method. // This ensures that the final value of orient is a clear landscape or portrait portraitif (orient! = ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE & orient! = ActivityInfo. SCREEN_ORIENTATION_PORTRAIT) {// width> the height is a horizontal screen, and it is an int [] size = MyUtils. getDisplaySize (this); orient = size [0] <size [1]? ActivityInfo. SCREEN_ORIENTATION_PORTRAIT: ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE; Logger. get (). I ("w/h:" + MyUtils. getOrientationName (orient);} if (orient = ActivityInfo. SCREEN_ORIENTATION_PORTRAIT) {setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE);} else {setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_PORTRAIT );}}

Use the setRequestedOrientation (xxx) method to set. the configuration in the xml file is equivalent. Therefore, after the preceding routine explicitly specifies the direction, the Activity no longer automatically performs horizontal and vertical screen Switching Based on the physical sensor. To restore the screen, call setRequestedOrientation (UNSPECIFIED) you can.

 

In addition, you can use the Configuration object to obtain the current display direction of the Activity:


// Confirm the current display direction through the Configuration object Configuration conf = getResources (). getConfiguration (); String orientName = "undefined"; switch (orient) {case Configuration. ORIENTATION_LANDSCAPE: orientName = "landscape"; case Configuration. ORIENTATION_PORTRAIT: orientName = "portrait"; case Configuration. ORIENTATION_SQUARE: orientName = "square"; default: orientName = "undefined";} Logger. get (). I ("conf. orient: "+ orientName );

Note that the definitions of constants related to the two (ActivityInfo and Configuration) are inconsistent. constants in ActivityInfo are used to determine the display direction, the constants in the Configuration object are clear actual display directions, which may be four: UNDEFINED, LANDSCAPE, and PORTRAIT) and SQUARE (SQUARE ).

 

Finally, let's take a look at how to capture events with changed display directions in the program, starting with the Activity. onConfigurationChanged method.

The Android system reloads different resource files (such as layout resources) based on device configuration changes (such as screen direction changes ), it terminates and restarts the Activity to reload resources. If we declare to them that we want to handle (some) configuration changes, we must reload the relevant resources ourselves-that is, the target Activity will not be in (some) configuration changes, the process of termination and restart is now complete.

We pay attention to the changes in the display direction. We need to specify the configChanges attribute to be equal to orientation when declaring the Activity. The example is as follows:


<activityandroid:name=".MyActivity"android:configChanges="orientation"android:label="@string/app_name"/>

In fact, the configChanges attributes include: fontScale (the user first changes the font size), locale (the user's language environment settings change), and keyboard (the keyboard type changes.

 

Then override the Activity. onConfigurationChanged method, for example:


// Callback public void onConfigurationChanged (Configuration conf) {// super. onConfigurationChanged (conf); // TODO: YOU Process ...}

In this way, the above method will be called back when the screen direction changes. It should be emphasized that onConfigurationChanged is called back only when the specified configuration item in configChanges changes. Therefore, the above method is called back only when the screen direction (orientation) changes.


Android: a simple example of dynamically changing the screen direction (LANDSCAPE and PORTRAIT )! AndroidHow can a mobile phone dynamically move the screen direction of the mobile phone? Maybe the mobile phone also has this function. When the direction of the mobile phone changes, the screen also changes. AndroidIt is easy to implement.

Here we mainly useGetRequestedOrientation(), AndSetRequestedorientation() Two methods. But to use these two methods, you must firstAndroidManiefst. xmlSet the screen property. Otherwise, the program will not work properly.

The main code is as follows:

// If it is a vertical column, change it to a horizontal column if (getRequestedOrientation () = ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE) {setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_PORTRAIT);} // if the row is horizontal, change it to vertical else if (getRequestedOrientation () = ActivityInfo. SCREEN_ORIENTATION_PORTRAIT) {setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE );}


InAndroidManifest. xmlSet the default direction in the file, or the program will not work properly. The Code is as follows:

     <activity android:name=".Activity01"                  android:label="@string/app_name"                  android:screenOrientation="portrait">



How does the Android program obtain the screen direction and size?

By default, when the screen is switched, The onCreate () method of the activity will be called again, so you can use the following code to read the screen direction: public void onCreate () {if (this. getResources (). getConfiguration (). orientation = Configuration. ORIENTATION_LANDSCAPE) {Log. I ("info", "landscape");} else if (this. getResources (). getConfiguration (). orientation = Configuration. ORIENTATION_PORTRAIT) {Log. I ("info", "portrait") ;}} if you are in androidmanifest. add android: configChanges = "orientation | keyboardHidden | navigation in xml. When the screen is flipped, the Activity will not repeatedly call onCreate (), onPause (), and onResume (). instead, call onConfigurationChanged (Configuration newConfig) int screenWidth, screenHeight; WindowManager windowManager = getWindowManager (); Display display = windowManager. getdefadisplay display (); screenWidth = display. getWidth (); screenHeight = display. getHeight (); another method is available: DisplayMetrics dm = new DisplayMetrics (); getWindowManager (). getdefadisplay display (). getMetrics (dm );

How does the Android program obtain the screen direction and size?

Public void onCreate () {if (this. getResources (). getConfiguration (). orientation = Configuration. ORIENTATION_LANDSCAPE) {Log. I ("info", "landscape");} else if (this. getResources (). getConfiguration (). orientation = Configuration. ORIENTATION_PORTRAIT) {Log. I ("info", "portrait") ;}} if you are in androidmanifest. add android: configChanges = "orientation | keyboardHidden | navigation in xml. When the screen is flipped, the Activity will not repeatedly call onCreate (), onPause (), and onResume (). instead, call onConfigurationChanged (Configuration newConfig) int screenWidth, screenHeight; WindowManager windowManager = getWindowManager (); Display display = windowManager. getdefadisplay display (); screenWidth = display. getWidth (); screenHeight = display. getHeight (); also has another method:

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.