我目前正在尝试替换我的“Sensor.TYPE_ORIENTATION”,因为它已被弃用。因此,Android 文档缺少大部分关于如何操作的信息。在此处调试和挖掘SO时,我想出了如何计算azimuth
OrientationSensor 曾经提供的值。我这样做:
float accelerometerVals[] = new float[3];
float magneticVals[] = new float[3];
float orientationSensorVals[] = new float[3];
float rotationMatrix[] = new float[16];
float orientation[] = new float[3];
@Override
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
System.arraycopy(event.values, 0, accelerometerVals, 0, 3);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
System.arraycopy(event.values, 0, magneticVals, 0, 3);
break;
case Sensor.TYPE_ORIENTATION:
System.arraycopy(event.values, 0, orientationSensorVals, 0, 3);
break;
default:
break;
}
if (null != magneticVals && null != accelerometerVals){
SensorManager.getRotationMatrix(rotationMatrix, null, accelerometerVals, magneticVals)
SensorManager.getOrientation(rotationMatrix, orientation);
float azimuth = Math.toDegrees(orientation[0])
//this calculation gives me the Azimuth in the same format that OrientationSensor
azimuth += (azimuth >= 0) ? 0 : 360
float FALSE_PITCH = Math.toDegrees(orientation[1])
float FALSE_ROLL = Math.toDegrees(orientation[2])
}
注意变量 FALSE_PITCH && FALSE_ROLL。我不知道如何“标准化”(值从 +10 到 -10 不同)这个值,以获得与我之前在 OrientationSensor 中相同的输出