values[2] 表示Y轴的角度:翻转角 即由静止状态开始,左右翻转
可见统一获取方向的方法是必须的,因为处理这些数据的算法可能针对第一种获取方式,那么当用在第二种方式时,移植性就不好了。
看下面的方法
public static float[] getOrientation (float[] R, float[] values)
Since: API Level 3
Computes the device's orientation based on the rotation matrix.
When it returns, the array values is filled with the result:
values[0]: azimuth, rotation around the Z axis.
values[1]: pitch, rotation around the X axis.
values[2]: roll, rotation around the Y axis.
The reference coordinate-system used is different from the world coordinate-system defined for the rotation matrix:
X is defined as the vector product Y.Z (It is tangential to the ground at the device's current location and roughly points West).
Y is tangential to the ground at the device's current location and points towards the magnetic North Pole.
Z points towards the center of the Earth and is perpendicular to the ground.
All three angles above are in radians and positive in the counter-clockwise direction.
通常我们并不需要获取这个函数的返回值,这个方法会根据参数R[]的数据填充values[]而后者就是我们想要的。
那么R表示什么呢?又将怎么获取呢?
R[] 是一个旋转矩阵,用来保存磁场和加速度的数据,大家可以理解未加工的方向数据吧
R通过下面的静态方法获取,这个方法也是用来填充R[]
public static boolean getRotationMatrix (float[] R, float[] I, float[] gravity, float[] geomagnetic)
解释以下参数:
第一个就是我们需要填充的R数组,大小是9
第二个是是一个转换矩阵,将磁场数据转换进实际的重力坐标中 一般默认情况下可以设置为null
第三个是一个大小为3的数组,表示从加速度感应器获取来的数据 在onSensorChanged中
第四个是一个大小为3的数组,表示从磁场感应器获取来的数据 在onSensorChanged中
好了基本逻辑就是这样的,下面给大家演示一个简单的测试方向的例子,可以时刻监听用户的方向
/*
* @author octobershiner
* 2011 07 28
* SE.HIT
* 一个演示通过磁场和加速度两个感应器获取方向数据的例子
* */
package uni.sensor;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
public class OrientationActivity extends Activity{
private SensorManager sm;
//需要两个Sensor
private Sensor aSensor;
private Sensor mSensor;
float[] accelerometerValues = new float[3];
float[] magneticFieldValues = new float[3];
private static final String TAG = "sensor";
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
aSensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensor = sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
sm.registerListener(myListener, aSensor, SensorManager.SENSOR_DELAY_NORMAL);
sm.registerListener(myListener, mSensor,SensorManager.SENSOR_DELAY_NORMAL);
//更新显示数据的方法
calculateOrientation();
}
//再次强调:注意activity暂停的时候释放
public void onPause(){
sm.unregisterListener(myListener);
super.onPause();
}
final SensorEventListener myListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
magneticFieldValues = sensorEvent.values;
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
accelerometerValues = sensorEvent.values;
calculateOrientation();
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};
private void calculateOrientation() {
float[] values = new float[3];
float[] R = new float[9];
SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticFieldValues);
SensorManager.getOrientation(R, values);
// 要经过一次数据格式的转换,转换为度
values[0] = (float) Math.toDegrees(values[0]);
Log.i(TAG, values[0]+"");
//values[1] = (float) Math.toDegrees(values[1]);
//values[2] = (float) Math.toDegrees(values[2]);
if(values[0] >= -5 && values[0] < 5){
Log.i(TAG, "正北");
}
else if(values[0] >= 5 && values[0] < 85){
Log.i(TAG, "东北");
}
else if(values[0] >= 85 && values[0] <=95){
Log.i(TAG, "正东");
}
else if(values[0] >= 95 && values[0] <175){
Log.i(TAG, "东南");
}
else if((values[0] >= 175 && values[0] <= 180) || (values[0]) >= -180 && values[0] < -175){
Log.i(TAG, "正南");
}
else if(values[0] >= -175 && values[0] <-95){
Log.i(TAG, "西南");
}
else if(values[0] >= -95 && values[0] < -85){
Log.i(TAG, "正西");
}
else if(values[0] >= -85 && values[0] <-5){
Log.i(TAG, "西北");
}
}
}










