获取3个旋转值(方位、俯仰和滚动)的方法有两种。
第一种是注册一个类型为TYPE_ORIENTATION的监听器。这是最简单的方法,我可以从每次旋转中获得正确范围的值,就像文档中所说的那样: 方位:[0, 359] 俯仰:[-180, 180] 滚动:[-90, 90]
另一种方法则是最精确但也是最难理解的一种方式,Android建议使用,因此我想使用它,但我却得到了不同的值。
方位:[-180, 180]。-180/180表示南,0表示北,90表示东,-90表示西。
俯仰:[-90, 90]。90表示90,-90表示-90,0表示0,但是-180/180(屏幕朝下)表示0。
滚动:[-180, 180]。
我应该获得相同的值,但是带有小数点,对吧?
以下是我的代码:
aValues = new float[3];
mValues = new float[3];
sensorListener = new SensorEventListener (){
public void onSensorChanged (SensorEvent event){
switch (event.sensor.getType ()){
case Sensor.TYPE_ACCELEROMETER:
aValues = event.values.clone ();
break;
case Sensor.TYPE_MAGNETIC_FIELD:
mValues = event.values.clone ();
break;
}
float[] R = new float[16];
float[] orientationValues = new float[3];
SensorManager.getRotationMatrix (R, null, aValues, mValues);
SensorManager.getOrientation (R, orientationValues);
orientationValues[0] = (float)Math.toDegrees (orientationValues[0]);
orientationValues[1] = (float)Math.toDegrees (orientationValues[1]);
orientationValues[2] = (float)Math.toDegrees (orientationValues[2]);
azimuthText.setText ("azimuth: " + orientationValues[0]);
pitchText.setText ("pitch: " + orientationValues[1]);
rollText.setText ("roll: " + orientationValues[2]);
}
public void onAccuracyChanged (Sensor sensor, int accuracy){}
};
请帮忙。这真的很令人沮丧。
我需要处理那些值吗?还是我做错了什么?
谢谢。