在 SensorFusionActivity 中添加 mDisplay 变量,如下所示:
private Display mDisplay = null;
然后,在 onCreate 方法中添加以下代码:
mDisplay = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
最后,以这种方式修改 calculateAccMagOrientation:
// calculates orientation angles from accelerometer and magnetometer output
public void calculateAccMagOrientation() {
if(SensorManager.getRotationMatrix(rotationMatrix, null, accel, magnet)) {
int rotation = mDisplay.getRotation();
if(rotation == Surface.ROTATION_0)
SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_X, rotationMatrix);
else if(rotation == Surface.ROTATION_180)
SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, rotationMatrix);
else if(rotation == Surface.ROTATION_270)
SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_MINUS_X, SensorManager.AXIS_MINUS_Y, rotationMatrix);
SensorManager.getOrientation(rotationMatrix, accMagOrientation);
}
}