Android always monitors the Rotation Angle of the mobile phone. determines at which angle the vertical screen layout is loaded Based on the rotation angle.

Source: Internet
Author: User

Android always monitors the Rotation Angle of the mobile phone. determines at which angle the vertical screen layout is loaded Based on the rotation angle.

I. Scenario Description:

During recent development, we encountered a problem that the operating system was aware of the horizontal and vertical screen layout when performing horizontal and vertical screen switching, as an developer, you cannot determine when the Activity loads the landscape layout and when the landscape layout is loaded. Therefore, in order to find the demarcation points for loading the horizontal and vertical screen la S, I have monitored the angle of screen rotation in particular, to see what angle will the horizontal screen la s, at what angle is the vertical screen layout loaded.

 

Ii. Change of screen Rotation Degree

The degree changes. The mobile phone rotates clockwise, And the degree increases.

3. Listen to the rotation angle of the mobile phone in the Activity and add the code.

 

/*** Check whether the screen direction changes from time to time * @ author wilson. xiong */class MyOrientationDetector extends OrientationEventListener {public MyOrientationDetector (Context context) {super (context) ;}@ Overridepublic void onOrientationChanged (int orientation) {// If the screen rotation is turned on, set the screen to be rotated // 0-57 degrees 125-236 degrees 306-360 degrees. These ranges are vertical screens. // The range of 58-124 degrees 237-305 degrees is landscape screen if (orientation =-1 | (orientation> = 0) & (orientation <= 57) | (orientation> = 125) & (orientation <= 236 )) | (orientation> = 306 & orientation <= 360) {mScreenOrientation = 1; // landscape} else if (orientation> = 58 & orientation <= 124) | (orientation >=237 & orientation <= 305) {mScreenOrientation = 0; // landscape screen} // mOrientation = orientation ;}}

 

Usage of this class:

(1) Call the enable () method in onResume () to listen for angle changes

 

@Overridepublic void onResume() {super.onResume();mDetector.enable();if (!isFirst) {if (GTConfig.instance().hasDickLoaded) {GTSQuote.updateGTSQuoteList();}refreshData();} else {isFirst = false;}}

 

(2) Call the disable () method in the onPause () method to stop listening.

 

@Overridepublic void onPause() {super.onPause();mDetector.disable();}

 

 

 

 

 

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.