例如,我启动了应用程序,然后将设备侧面旋转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获取这些值。
详见此页面。
你可以使用OrientationEventListener来处理平面
OrientationEventListener mOrientationListener = new OrientationEventListener(
getApplicationContext()) {
@Override
public void onOrientationChanged(int orientation) {
}
};
if (mOrientationListener.canDetectOrientation()) {
mOrientationListener.enable();
}
我认为你需要使用传感器管理器来计算每个点的x、y、z,并使用方程式找到两个点之间的角度。考虑你的起始点(0,0,0);