【发布时间】:2012-07-31 15:56:59
【问题描述】:
我正在尝试使用此非弃用版本的代码来获取方向。但是,我无法让它像使用已弃用的 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();
}
}
【问题讨论】:
-
您不应该忽略
SensorManager.SENSOR_STATUS_UNRELIABLE值,因为在某些低端设备上,这是您将获得的唯一值(这些值实际上在我的测试中可用)