Detailed screen orientation in Android
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:
<activity android:name= ". Sketchpadactivity " android:screenorientation=" Landscape "/><!--let the activity always appear as a horizontal screen--
There are 7 optional values in the Screenorientations property (constants are defined in the Android.content.pm.ActivityInfo class ):
- Landscape: Horizontal screen (landscape photo), the width of the display is greater than the height;
- Portrait: Vertical Screen (Portrait), the display of higher than the width of the degree;
- User: The current preferred direction for users;
- Behind: Inherits the active direction of the activity below the activity stack;
- 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;
- 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);
- 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:
1 /*2 * Dynamically changing the display direction of the current screen via API3 */4 Public voidapichangeorientation () {5 //get the current screen orientation6 intOrient =getrequestedorientation ();7Logger.get (). I ("Orientation:" +Myutils.getorientationname (Orient));8 9 //confirm the actual display direction by means of a wide and high ratio if not explicitly landscape or portraitTen //This will ensure that the Orient final value will be a clear horizontal screen landscape or vertical screen portrait One if(Orient! =Activityinfo.screen_orientation_landscape A&& Orient! = activityinfo.screen_orientation_portrait) { - //wide > High for horizontal screen, anyway for vertical screen - int[] size = Myutils.getdisplaysize ( This); theOrient = 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); A}Else{ at setrequestedorientation (activityinfo.screen_orientation_portrait); - } -}
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:
The configuration object confirms the current display direction of the 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 ";d efault:orientname =" undefined ";} 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:
<activity android:name= ". MyActivity " android:configchanges=" orientation " 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:
Callback public void onconfigurationchanged (configuration conf) {//super.onconfigurationchanged (CONF) when device configuration changes;//todo: You Process ...}
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.
Reference: http://cnetwei.iteye.com/blog/781602
Detailed screen orientation in Android