简体   繁体   中英

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.

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

private static final int ORIENTATION_0 = 0;
private static final int ORIENTATION_90 = 3;
private static final int ORIENTATION_270 = 1;





Display display = ((WindowManager)
          getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int screenOrientation = 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;
}

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

Look this page for details.

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);

You can use OrientationEventListener for the planes

OrientationEventListener mOrientationListener = new OrientationEventListener(
            getApplicationContext()) {
        @Override
        public void onOrientationChanged(int orientation) {

        }
    };

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM