Android always monitors the rotation angle of the phone to determine at what angle the vertical layout is loaded when the horizontal screen layout is loaded

Source: Internet
Author: User
Tags in degrees

First, the scene description:

Recent development encountered a problem, is that we do the vertical and horizontal screen switching function, the layout of the screen is the operating system to perceive, as a developer can not determine when the activity when loading the horizontal screen layout, when loading portrait screen layout. So in order to find the point of loading the horizontal screen layout and the vertical screen layout, i specifically monitored the screen rotation angle, see at what angle will load the horizontal screen layout, at what angle loads the vertical screen layout.


Second, the screen rotation degree changes

Changes in degrees, holding the phone clockwise rotation, the degree will become larger.

Third, in the activity to monitor the mobile phone rotation angle, on the code.

/** * Always monitor the screen orientation changes * @author wilson.xiong */class Myorientationdetector extends Orientationeventlistener {public Myorie Ntationdetector (Context context) {super (context);} @Overridepublic void onorientationchanged (int orientation) {//If the screen rotation is turned on, the settings screen rotates//0-57 degrees 125-236 degrees 306-360  degrees These intervals range from vertical screen//58-124 degrees to 237-305 degrees  for horizontal screen if (orientation = =-1 | | (Orientation >= 0) && (Orientation <= 57)) | | ((Orientation >=) && (Orientation <= 236)) | | (Orientation >= 306 && Orientation <= 360)) {mscreenorientation = 1;//vertical Screen} else if ((Orientation >= && Orientation <= 124) | | ((Orientation >= 237 && Orientation <= 305))) {mscreenorientation = 0;//horizontal Screen}//morientation = orientation;}}


How to use this class:

(1) Call the Enable () method in Onresume () to monitor the angle change

@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 ();}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android always monitors the rotation angle of the phone to determine at what angle the vertical layout is loaded when the horizontal screen layout is loaded

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.