Define Android orientation: landscape-left v. Landscape right

How to determine which of the four sides of the phone is installed.

I can define portrait / landscape mode, but how can I tell the landscape rotation to the left from the landscape rotation to the right ?

Basically, I want to make a nice transitional animation when the user rotates the phone. You know how in iPhone Safari: a quick turn of 400 ms from the previous layout to a new one.

+6
android
source share
3 answers

Use OrientationEventListener:

mOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) { @Override public void onOrientationChanged(int orientation) { mDeviceOrientation = orientation; } }; if(mOrientationEventListener.canDetectOrientation()) { mOrientationEventListener.enable(); } 

mDeviceOrientation should be an integer indicating the angle of rotation of your device, if you do some smart rounding, you should see which of the four orientations it is:

 // 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 it!

+10
source share

Just stumbled :) 2.2+ put the xml code in 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; } } 
+7
source share

It seems you should be able to tell screenOrientation received via getRequestedOrientation

+1
source share

All Articles