问题
I am trying to use this non-deprecated version of code to get orientation. However I can't get it to work as smooth and perfect as if I just use the deprecated Sensor.TYPE_ORIENTATION. Am I missing something?
//OLD
@Override
public void onSensorChanged(SensorEvent arg0) {
m_lastYaw = arg0.values[0];
invalidate();
}
//NEW
@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();
}
}
回答1:
I used the code that I posed here and it worked well only when the phone (Samsung Galaxy S3) layed flat. But it's a start: It doesn't work better than Sensor.TYPE_ORIENTATION
nor worse.
来源:https://stackoverflow.com/questions/11744201/android-sensor-type-orientation-works-perfect-and-i-cant-get-the-same-results-w