繁体   English   中英

Android 我想获得设备的旋转角度

[英]Android I want to get rotation angle for device

例如,我启动了应用程序,然后将设备并排旋转 90 度。我想为设备上的每个平面检测此事件。

您可以像这样获取屏幕方向(例如在您的 onResume() 方法中):

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

使用 SensorManager 和 TYPE_GYROSCOPE/TYPE_ROTATION_VECTOR/TYPE_ORIENTATION/ 获取这些值。

查看此页面了解详细信息。

我认为您必须使用传感器管理器来计算每个点的 x、y、z。 并使用等式求出两点之间的角度。 考虑您的起点 (0,0,0);

您可以为飞机使用OrientationEventListener

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

        }
    };

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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