Android development practices: screen Rotation Processing

Source: Internet
Author: User
Tags api manual

Recently, Android Camera-related programs have been developed to maximize screen rotation. On the one hand, we must consider the layout changes after screen rotation, on the other hand, we need to figure out the relationship between the orientation and angle of the screen and the Preview angle of Camera. Originally, the screen rotation can be detected by reloading the onConfigurationChanged method of the Activity, but one problem is found. It can only detect switching between the horizontal direction and the vertical direction, and cannot detect jump of 180 degrees, for example: the horizontal direction suddenly turns to 180 degrees to the horizontal direction), so the OrientationEventListener method has to be used to solve the problem. Here, I would like to share my experiences and summarize the processing of screen rotation in Android development.


1. If no processing is performed


If no specific processing is performed, by default, when the gravity Sensor of your mobile phone is turned on, rotating the screen will lead to onDestroy-> onCreate, the current activity and interface layout will be re-constructed. If the layout of many horizontal/vertical screens is not well designed, it will be difficult to see after being converted to the vertical/landscape screen.


If you want to support screen rotation, we recommend that you create two folders, layout-land and layout-port, in res, to put the layout files of the horizontal and vertical screens into the corresponding layout folder.


2. How to set a fixed screen direction


In the activity attribute corresponding to AndroidManifest. xml, add:


Android: screenOrientation = "landscape" // horizontal screen android: screenOrientation = "portrait" // portrait Screen


By default, after the application is started, it is fixed to the specified screen direction. Even if the screen is rotated, the Activity will not be destroyed or redirected to any other response.


3. Force turn on the screen rotation effect


If the gravity sensor is not enabled on your mobile phone or android: screenOrientation is set in AndroidManifest. xml, the Activity does not respond to the screen rotation event by default. If you still want the Activity to respond to screen rotation in this case, add the following code:


// This. setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_FULL_SENSOR) in the onCreate function of the activity );


4. When the screen is rotated, you do not want the activity to be destroyed.


If you want to capture a screen rotation event and do not want the activity to be destroyed, use the following method:


1) In the activity attribute corresponding to AndroidManifest. xml, add:


android:configChanges="orientation|screenSize"


2) In the corresponding activity, the onConfigurationChanged function is reloaded.


@Overridepublic voidonConfigurationChanged(Configuration newConfig) {    super.onConfigurationChanged(newConfig);}


The function can detect the current screen status in two ways:


First:


Determine whether newConfig is equal to Configuration. ORIENTATION_LANDSCAPE, Configuration. ORIENTATION_PORTRAIT


Of course, this method can only determine whether the screen is a horizontal screen or a vertical screen, and cannot obtain the specific rotation angle.


Second:


Call this. getWindowManager (). getDefaultDisplay (). getRotation ();


The return values of this function are as follows:


Surface. ROTATION_0, Surface. ROTATION_90, Surface. ROTATION_180, Surface. ROTATION_270


Surface. ROTATION_0 indicates the direction of the phone's portrait screen up, followed by a series of clockwise increase of 90 degrees based on this.


3) bugs in this method


Recently, we found that this method has a Bug that it can only Rotate 90 degrees at a time. If you suddenly rotate 180 degrees, the onConfigurationChanged function will not be called.


5. determine every angle of screen rotation in real time


The above-mentioned screen rotation angles can only be judged at most 0, 90, 180,270. If you want to get the changes of each angle in real time, you can use OrientationEventListener.


Usage:


1) create a class to inherit OrientationEventListener


public class MyOrientationDetector extends OrientationEventListener{    public MyOrientationDetector( Context context ) {        super(context );    }    @Override    public void onOrientationChanged(int orientation) {        Log.i("MyOrientationDetector ","onOrientationChanged:"+orientation);    }}


2) Enable and disable listeners


You can create an object of the MyOrientationDetector class in the activity. Note that the listener is disabled by the enable () and disable () Methods of the parent class of the class.


Therefore, you can call the enable method of the MyOrientationDetector object in onResume () of the activity, and call the disable method of the MyOrientationDetector object in onPause () to complete the car function.


3) Monitor the specified screen Rotation Angle


The onOrientationChanged parameter orientation of the MyOrientationDetector class is from 0 ~ 359. If you only want to process four directions, add a judgment:

If (orientation = OrientationEventListener. ORIENTATION_UNKNOWN) {return; // when the mobile phone is flat, no valid angle is detected} // only whether there are four angles changed if (orientation> 350 | orientation <10) {// 0 degree orientation = 0;} else if (orientation> 80 & orientation <100) {// 90 degree orientation = 90 ;} else if (orientation> 170 & orientation <190) {// 180 degree orientation = 180;} else if (orientation> 260 & orientation <280) {// 270 degree orientation = 270;} else {return;} Log. I ("MyOrientationDetector", "onOrientationChanged:" + orientation );


As for how to process screen rotation, this article only outlines various aspects of screen Rotation Processing. For details, you can search for related keywords on the Internet or view the API manual, here I will not go into detail, any questions welcome to leave a message or letter to the lujun.hust@gmail.com exchange.


This article from the "three shadows" blog, please be sure to keep this source http://ticktick.blog.51cto.com/823160/1301209

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.