android - OrientationEventListener vs Display.getRotation -
what relationship between degrees reported orientationeventlistener.onorientationchanged , display rotation reported windowmanager().getdefaultdisplay().getrotation.
both of these return orientation of device in degrees. relationship between values , how should 1 interpret them?
so did little bit of digging around , wrote helper class below can nested class within activity. orientationeventlistener listens on changes in accelerometer data. if hold phone in natural orientation (portrait / vertical), orientation 0. gradually increases when rotate phone in clockwise direction. when left side on top (landscape), orientation 90. when phone upside down 180. when right side up, 270.
while getrotation() the rotation of screen "natural" orientation. per docs, the angle rotation of drawn graphics on screen, opposite direction of physical rotation of device
. hence when phone held in portrait mode / natural orientation, rotation , orientation both 0. if left side @ top, orientation 90, rotation 270 since drawn graphics opposite in direction of device rotation. similarly, if right side @ top, orientation 270 , rotation 90. interesting case: phone held upside down -- orientation 180 rotation still 0. if graphics not redrawn when phone rotated, display.getrotation() remains unchanged. can verified code below. printing rotation , orientation when there change , orientation has been quantized 4 values of 0, 90, 180, 270.
static final class customorientationlistener extends orientationeventlistener { private static final string tag = customorientationlistener.class.getsimplename(); private int savedorientation = integer.min_value; private int savedrotation = integer.min_value; appcompatactivity activity; public customorientationlistener(appcompatactivity a) { super(a); activity = a; } /** * called when orientation of device has changed. * orientation parameter in degrees, ranging 0 359. * orientation 0 degrees when device oriented in natural position, * 90 degrees when left side @ top, 180 degrees when upside down, * , 270 degrees when right side top. */ @override public void onorientationchanged(int orientation) { orientation = normalizeorientation(orientation); if (savedorientation != orientation) { log.i(tag, "orientation: " + orientation); savedorientation = orientation; } int rotation = getrotationindegrees(activity.getwindowmanager().getdefaultdisplay().getrotation()); if (rotation != savedrotation) { log.i(tag, "rotation: " + rotation); savedrotation = rotation; } } /** * getrotation() returns rotation of screen "natural" orientation. returned value may * surface.rotation_0 (no rotation), surface.rotation_90, surface.rotation_180, or surface.rotation_270. * example, if device has naturally tall screen, , user has turned on side go * landscape orientation, value returned here may either surface.rotation_90 or surface.rotation_270 * depending on direction turned. angle rotation of drawn graphics on screen, * opposite direction of physical rotation of device. example, if device rotated * 90 degrees counter-clockwise, compensate rendering rotated 90 degrees clockwise , * returned value here surface.rotation_90. */ private static int getrotationindegrees(int rotation) { int degrees = 0; switch (rotation) { case surface.rotation_0: degrees = 0; break; case surface.rotation_90: degrees = 90; break; case surface.rotation_180: degrees = 180; break; case surface.rotation_270: degrees = 270; break; } return degrees; } private static int normalizeorientation(int degrees) { if (degrees > 315 || degrees <= 45) { return 0; } if (degrees > 45 && degrees <= 135) { return 90; } if (degrees > 135 && degrees <= 225) { return 180; } if (degrees > 225 && degrees <= 315) { return 270; } throw new runtimeexception("the physics know them no more. watch out anomalies."); } }
Comments
Post a Comment