Detailed screen orientation in Android

Source: Internet
Author: User

The screen orientation is for activity, so you can use the <activity> tag in the Androidmanifest.xml file. The Screenorientation property is set, for example:

XML code
    1. <activity
    2. android:name=". Sketchpadactivity "
    3. android:screenorientation="Landscape"/><!--so that activity is always displayed as a horizontal screen--

There are 7 optional values in the Screenorientations property (constants are defined in the Android.content.pm.ActivityInfo class ):

  1. Landscape: Horizontal screen (landscape photo), the width of the display is greater than the height;
  2. Portrait: Vertical Screen (Portrait), the display of higher than the width of the degree;
  3. User: The current preferred direction for users;
  4. Behind: Inherits the active direction of the activity below the activity stack;
  5. Sensor: The physical sensor determines the direction of the display, depending on how the user holds the device, and when the device is rotated, the direction changes-between the horizontal screen and the vertical screen;
  6. Nosensor: Ignoring physical sensors-that is, the display direction is independent of the physical sensor, no matter how the user rotates the display direction of the device will not change (except for the "unspecified" setting);
  7. unspecified : Unspecified, this is the default value, by the Android system to choose the appropriate direction, the selection strategy depends on the configuration of the specific device, so different devices will have a different direction choice;

The above configuration values are reflected in the return value of the Activity.getrequestedorientation () method, and the corresponding Setrequestedorientation () method can dynamically change the value of the property in the form of an API. The following example toggles between two directions in a horizontal/vertical screen:

Java code
  1. /*
  2. * Dynamically changing the display direction of the current screen via API
  3. */
  4. Public void Apichangeorientation () {
  5. //Get current screen orientation
  6. int orient = Getrequestedorientation ();
  7. Logger.get (). I ("orientation:" + myutils.getorientationname (Orient));
  8. //If it is not clear landscape or portrait, then confirm the actual display direction through the method of wide and high ratio
  9. //This will guarantee that the Orient final value will be clear horizontal screen landscape or vertical screen portrait
  10. if (orient! = Activityinfo.screen_orientation_landscape
  11. && Orient! = activityinfo.screen_orientation_portrait) {
  12. //width > Height for horizontal screen, anyway for vertical screen
  13. int[] size = myutils.getdisplaysize (this);
  14. Orient = size[0] < size[1]? Activityinfo.screen_orientation_portrait
  15. : Activityinfo.screen_orientation_landscape;
  16. Logger.get (). I ("w/h:" + myutils.getorientationname (Orient));
  17. }
  18. if (orient = = activityinfo.screen_orientation_portrait) {
  19. Setrequestedorientation (Activityinfo.screen_orientation_landscape);
  20. }else{
  21. Setrequestedorientation (activityinfo.screen_orientation_portrait);
  22. }
  23. }

The Setrequestedorientation (XXX) method setting is equivalent to the configuration in the Androidmanifest.xml file, so the activity will no longer automatically switch between the vertical and horizontal screens based on the physical sensor after specifying the direction explicitly by the above routines. To recover, call Setrequestedorientation (UNSPECIFIED) again.

In addition, you can use the configuration object to get the current display direction of the activity:

Java code
  1. Confirm the current display direction with the configuration object
  2. Configuration conf = getresources (). GetConfiguration ();
  3. String orientname = "undefined";
  4. Switch (Orient) {
  5. Case Configuration.orientation_landscape:
  6. Orientname = "Landscape";
  7. Case CONFIGURATION.ORIENTATION_PORTRAIT:
  8. Orientname = "Portrait";
  9. Case Configuration.orientation_square:
  10. Orientname = "Square";
  11. Default
  12. Orientname = "undefined";
  13. }
  14. Logger.get (). I ("conf.orient:" + orientname);

It is important to note that the two (Activityinfo and configuration) constants defined for the direction are inconsistent, and the constants in the Activityinfo are the policies used for the direction of the decision display, and the constants in the configuration objects are explicit The actual display direction, a total of 4 possible: undefined (UNDEFINED), horizontal screen (LANDSCAPE) , vertical screen ( PORTRAIT) , and the square (square ) .

Finally, let's take a look at how to capture events that show directional changes in the program, starting with the Activity.onconfigurationchanged method.

The Android system reloads different resource files (such as layout layouts resources) based on changes in the configuration of the device (such as the change in the screen orientation) , and it implements the reload of the resource by terminating and restarting the activity . If we declare to them that we are going to handle (some) configuration changes ourselves, we must be responsible for reloading the associated resources-that is, the target activity will not go through the process of terminating and restarting when (some) configuration changes .

We focus on the changes in the display direction and need to specify that the Configchanges property equals orientation when the activity is declared, as shown in the following example:

Java code
    1. <activity
    2. Android:name=". MyActivity "
    3. android:configchanges="Orientation"
    4. Android:label="@string/app_name"/>

In fact, the configchanges attributes are: Fontscale (user first font size change), locale (user's locale change), keyboard (keyboard type change) and many other optional values .

Then overwrite the Activity. onconfigurationchanged methods, such as:

Java code
    1. Callback when device configuration is changed
    2. Public void Onconfigurationchanged (Configuration conf) {
    3. //super.onconfigurationchanged (conf);
    4. //todo:you Process ...
    5. }

This will cause the above method to be called back when the screen orientation changes. Again, the onconfigurationchanged is called back only if the configuration item specified in Configchanges changes, so the above method is only recalled when the screen orientation (orientation) changes.

Detailed screen orientation in Android

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.