How to set the orientation of the android screen at any time on the Framework Layer

Source: Internet
Author: User
The device is flat and the screen is facing to the top of the screen. The following four constants are represented respectively:
Private Static final int rotation_0 = 0; // initial condition. In this case, whether the device is a horizontal screen or a portrait screen is related to the default display direction when the hardware device is installed.
Private Static final int rotation_90 = 1; // when the screen orientation is set to auto-rotate, the screen will rotate in this direction when the right side is turned up.
Private Static final int rotation_270 = 2; // when the screen orientation is set to auto-rotate, the screen will rotate in this direction when the left side is turned up.
Private Static final int rotation_180 = 3; // After the screen orientation is set to auto-rotate, the screen will rotate in this direction immediately at the bottom of the screen.

Let's look at two Arrays:

private static final int[][][] THRESHOLDS_WITH_180 = new int[][][] {{{60, 165}, {165, 195}, {195, 300}},{{0, 30}, {165, 195}, {195, 315}, {315, 360}},{{0, 45}, {45, 165}, {165, 195}, {330, 360}},{{0, 45}, {45, 135}, {225, 315}, {315, 360}},};private static final int[][] ROTATE_TO_WITH_180 = new int[][] {{ROTATION_90, ROTATION_180, ROTATION_270},{ROTATION_0, ROTATION_180, ROTATION_90, ROTATION_0},{ROTATION_0, ROTATION_270, ROTATION_180, ROTATION_0},{ROTATION_0, ROTATION_90, ROTATION_270, ROTATION_0},};

When the current screen rotation direction is rotation_0, int [] [] Threshold = thresholds_with_180 [0] is taken;
When the current screen rotation direction is rotation_90, take int [] [] Threshold = thresholds_with_180 [1];
When the current screen rotation direction is rotation_270, use int [] [] Threshold = thresholds_with_180 [2];
When the current screen rotation direction is rotation_180, take int [] [] Threshold = thresholds_with_180 [3];

Each element in threshold consists of two values to indicate a range.

Descriworientationlistener registers an accelerator-type sensoreventlistener. When a new sensorevent is generated, filterorientation is called to generate an int orientation value. This value will match in the range indicated by each element of threshold to see which range it will fall in. Suppose the current screen direction is rotation_0, then Threshold = {60,165}, {165,195}, {195,300}. Assume that at this time, the left side of the screen is raised by 90 degrees. The orientation value calculated by filterorientation falls within the range of the third element. In this case, go to rotate_to_with_180 to find the corresponding value and find it rotation_270, then, you can change the orientation of the current screen to 270 degrees. The value of threshold is thresholds_with_180 [2]. When the screen is normally placed again, the orientation value calculated by filterorientation falls within the range indicated by the first element. Go to rotate_to_with_180 and find the value corresponding to it. If it is rotation_0, the current screen rotation direction is changed to 0 degrees.
Another variable is very important, mallow180rotation. If this variable is set to false, thresholds_with_180 and rotate_to_with_180 arrays will not be used for the above changes, so thresholds and rotate_to will be used.
In fact, I haven't figured out the filterorientation Algorithm for a long time and the specific meaning of each number in the thresholds_with_180 and thresholds arrays. Finally, I only figured out the above process, and the four angles rotation_0, rotation_90, rotation_270, and rotation_180 represent the four directions respectively. But this is enough to deal with what we have to do.
For example, I want to make the Screen rotate at most 90 degrees and 180 degrees, so that it will not have the chance to rotate 270 degrees. Turn all rotation_270 in rotate_to_with_180 to 90 degrees. In this way, it should be rotated to 90 degrees when it is rotated to 270 degrees. If you do not want to rotate the screen, change all values to rotation_0.

Further explore this topic
Refer to the article I wrote earlier (http://blog.csdn.net/a345017062/article/details/6592527) to know that phonwwindowmanager is the only class that implements the javasworientationlistener interface, which manages the display of the entire device interface. When phonwwindowmanager uses javasworientationlistener to know that the screen direction is rotated, it will tell windowmanagerservice:
Mwindowmanager. setrotation (rotation, false, mfancyrotationanimation );
After windowmanagerservice receives this notification, it will do two important things:
1. Surface. setorientation (0, rotation, animflags );
2. mrotationwatchers. Get (I). onrotationchanged (rotation );
We know that each activity has a view tree, and each view tree is painted on a surface. In the above two steps, the surface is rotated first, and then the activity is told to re-draw the view tree, and the entire screen is rotated.

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.