Detect Android orientation: landscape-Left v. landscape-Right
How can I detect which one of 4 sides of the 开发者_JAVA百科phone is up.
I can detect portrait/landscape mode, but how do I tell landscape-turned-on-left-side from landscape-turned-on-right-side?
Basically I want to make a nice transition animation when user turns phone. You know, like in iPhone's Safari: a swift 400ms rotation from the previous layout to the new.
Use an OrientationEventListener:
mOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL)
{
@Override
public void onOrientationChanged(int orientation)
{
mDeviceOrientation = orientation;
}
};
if(mOrientationEventListener.canDetectOrientation())
{
mOrientationEventListener.enable();
}
mDeviceOrientation should then be an integer telling you the angle your device is rotated to, if you do some clever rounding you should be able to see which of the four orientations it is in:
// Divide by 90 into an int to round, then multiply out to one of 5 positions, either 0,90,180,270,360.
int orientation = 90*Math.round(mDeviceOrientation / 90);
// Convert 360 to 0
if(orientation == 360)
{
orientation = 0;
}
Enjoy!
Just came across :) 2.2+ put xml code to ur res/values(false) and res/values-xlarge(true)
<resources>
<bool name="isTablet">false</bool>
private void getScreenRotationOnPhone() {
final Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
switch (display.getRotation()) {
case Surface.ROTATION_0:
System.out.println("SCREEN_ORIENTATION_PORTRAIT");
break;
case Surface.ROTATION_90:
System.out.println("SCREEN_ORIENTATION_LANDSCAPE");
break;
case Surface.ROTATION_180:
System.out.println("SCREEN_ORIENTATION_REVERSE_PORTRAIT");
break;
case Surface.ROTATION_270:
System.out.println("SCREEN_ORIENTATION_REVERSE_LANDSCAPE");
break;
}
}
private void getScreenRotationOnTablet() {
final Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
switch (display.getRotation()) {
case Surface.ROTATION_0:
System.out.println("SCREEN_ORIENTATION_LANDSCAPE");
break;
case Surface.ROTATION_90:
System.out.println("SCREEN_ORIENTATION_REVERSE_PORTRAIT");
break;
case Surface.ROTATION_180:
System.out.println("SCREEN_ORIENTATION_REVERSE_LANDSCAPE");
break;
case Surface.ROTATION_270:
System.out.println("SCREEN_ORIENTATION_PORTRAIT");
break;
}
}
private boolean isTabletDevice(){
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (tabletSize) {
return true;
} else {
return false;
}
}
It seems like you should be able to tell from the screenOrientation, gotten via getRequestedOrientation
I was able to get landscape left vs right events using this code:
object : OrientationEventListener(this, SENSOR_DELAY_NORMAL) {
override fun onOrientationChanged(orientation: Int) {
afterGlobalLayout {
logInfoToast(if (screenRotation == LandscapeLeft)
"LandscapeLeft" else "LandscapeRight")
}
}
}.enable()
afterGlobalLayout
uses viewTreeObserver.addOnGlobalLayoutListener
and screenRotation
is calculated by Piotr Ślesarew code.
精彩评论