Data may be used to calculate the orientation as outlined below:
1)
Retrieve sensor data from accelerometer and magnetic field sensor
as required by getRotationMatrix():
This actually involves a number of steps in the Activity class:
1.1)
Obtain a sensor manager.
NOTE: Context.getSystemService needs a Context, typically called
within an Activity’s context.
mSensMan = (SensorManager) getSystemService(SENSOR_SERVICE);
1.2)
Register a sensor event listener for each of the above sensor
types.
mSensMan.registerListener(this,
mSensMan.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_UI);
mSensMan.registerListener(this,
mSensMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
1.3)
In the listener’s onSensorChanged method copy the data from the
SensorEvent.values.
NOTE: The data must be copied off the event.values as the system is
reusing that array in all SensorEvents, simply assigning won’t work.public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
System.arraycopy(event.values, 0, mGravs, 0, 3);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
System.arraycopy(event.values, 0, mGeoMags, 0, 3);
break;
default:
return;
}
}
2)
Pass the copied sensor data as arrays to
SensorManager.getRotationMatrix() to receive the rotation matrix.
SensorManager.getRotationMatrix(mRotationM, null, mGravs, mGeoMags)
Optionally transform the returned rotation matrix through
SensorManager.remapCoordinateSystem() or multiplying by a
transformation matrix.
3)
Pass the rotation matrix to SensorManager.getOrientation() to
receive the orientation as yaw, pitch and roll expressed in radians.
SensorManager.getOrientation(mRotationM, mOrientation);
原文: http://www.mail-archive.com/android-beginners@googlegroups.com/msg23415.html
其實你可能會說:”有沒有中文的說明呀!!!”
沒錯, 有的!
http://blog.csdn.net/octobershiner/article/details/6641942
而且有寫一個小範例
有空實作一下吧
沒有留言:
張貼留言