我正在尝试编写一个可以从北方找到角度的应用程序(方位角)手机需要保持(纵向模式和横向模式)我粘贴在下面的代码似乎可以工作,但有时会出错值。有没有更好的方法呢?这也适用于所有手机吗?我正在使用Sensor.TYPE_ROTATION_VECTOR获取以下代码。
int axisX = 0;
int axisY=0;
switch (getWindowManager().getDefaultDisplay().getRotation())
{
case Surface.ROTATION_0:
axisX = SensorManager.AXIS_Z ;//SensorManager.AXIS_X;
axisY = SensorManager.AXIS_MINUS_X;
// rotation.setText("Rotaion: 0");
break;
case Surface.ROTATION_90:
axisX = SensorManager.AXIS_Z ;//SensorManager.AXIS_X;
axisY = SensorManager.AXIS_MINUS_X;
// rotation.setText("Rotaion: 90");
break;
case Surface.ROTATION_180:
axisX = SensorManager.AXIS_MINUS_X;
axisY = SensorManager.AXIS_MINUS_Y;
// rotation.setText("Rotaion: 180");
break;
case Surface.ROTATION_270:
axisX = SensorManager.AXIS_Z;//SensorManager.AXIS_MINUS_Y;
axisY = SensorManager.AXIS_MINUS_X;;//SensorManager.AXIS_X;
// rotation.setText("Rotaion: 270");
break;
default:
break;
}
//
float[] rotationMatrix = new float[9];
SensorManager.getRotationMatrixFromVector(rotationMatrix,event.values);
float[] adjustedRotationMatrix = new float[9];
SensorManager.remapCoordinateSystem(rotationMatrix, axisX,axisY, adjustedRotationMatrix);
float[] orientation = new float[3];
SensorManager.getOrientation(adjustedRotationMatrix, orientation);
float azimuth =(float) Math.toDegrees( orientation[0]) ;// * FROM_RADS_TO_DEGS;