我正在尝试使用此非弃用版本的代码来获取方向。但是,我无法让它像只使用已弃用的 Sensor.TYPE_ORIENTATION 一样顺畅和完美地工作。我错过了什么吗?
//老的
@Override
public void onSensorChanged(SensorEvent arg0) {
m_lastYaw = arg0.values[0];
invalidate();
}
//新的
@Override
public void onSensorChanged(SensorEvent event) {
if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE){
return;
}
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
System.arraycopy(event.values, 0, m_lastAccels, 0, 3);
}
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
System.arraycopy(event.values, 0, m_lastMagFields, 0, 3);
}
if (SensorManager.getRotationMatrix(m_rotationMatrix, null, m_lastAccels, m_lastMagFields)) {
SensorManager.getOrientation(m_rotationMatrix, m_orientation);
m_lastYaw = (float)Math.toDegrees(m_orientation[0]);
invalidate();
}
}