There are a few examples and tutorials on the web, but be careful. Sensor.TYPE_ORIENTATION
became deprecated. You need to calculate rotations by listening to these two sensors Sensor.TYPE_ACCELEROMETER
and Sensor.TYPE_MAGNETIC_FIELD
.
The tricky part after registering to receive notifications from these sensors, is to figure out how to handle the data received from them. The key part is the following:
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = event.values;
if (mGravity != null && mGeomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
azimuth = orientation[0]; // orientation contains: azimuth, pitch and roll
pitch = orientation[1];
roll = orientation[2];
}
}
}
This is how you should be calculating the azimuth, pitch, roll values of your device in the onSensorChanged(SensorEvent event)
callback. Keep in mind that "All three angles above are in radians and positive in the counter-clockwise direction". You can simply convert them to degrees with Math.toDegrees()
As Louis CAD pointed out in the comments, it is a good idea to move the initialization of the I, R and orientation arrays out of the onSensorChanged callback, since it is called frequently. Creating and then leaving them behind for the GC is bad for your apps performance. I left it there for the sake of simplicity.
Based on how your device is rotated you might need to remap the coordinates to get the result you want. You can read more about remapCoordinateSystem
and also about getRotationMatrix
and getOrientation
in the android documentation
Example code:
http://www.codingforandroid.com/2011/01/using-orientation-sensors-simple.html
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…