Skip to content Skip to sidebar Skip to footer

Android I Want To Get Rotation Angle For Device

For example, I started application then device is turned 90 degree by side.I want to detect this event for each planes on device.

Solution 1:

You can get the screen orientation (for example in your onResume() Method) like this:

privatestaticfinalintORIENTATION_0=0;
privatestaticfinalintORIENTATION_90=3;
privatestaticfinalintORIENTATION_270=1;





Displaydisplay= ((WindowManager)
          getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
intscreenOrientation= display.getRotation();

switch (screenOrientation)
{
default:
case ORIENTATION_0: // Portrait// do smth.break;
case ORIENTATION_90: // Landscape right// do smth.break;
case ORIENTATION_270: // Landscape left// do smth.break;
}

Solution 2:

Use SensorManager and TYPE_GYROSCOPE/TYPE_ROTATION_VECTOR/TYPE_ORIENTATION/ to get these value.

Look this page for details.

Solution 3:

I think you have to use the sensor manager to calculate x,y,z on every point. and use equation to find angle between two points. consider your starting points (0,0,0);

Solution 4:

You can use OrientationEventListener for the planes

OrientationEventListenermOrientationListener=newOrientationEventListener(
            getApplicationContext()) {
        @OverridepublicvoidonOrientationChanged(int orientation) {

        }
    };

    if (mOrientationListener.canDetectOrientation()) {
        mOrientationListener.enable();
    }

Post a Comment for "Android I Want To Get Rotation Angle For Device"