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