通过SensorManager获取手机方位,从而实现指南针功能。
在很多旧的文档介绍中,都是通过SensorManager .getDefaultSensor(Sensor.TYPE_ORIENTATION); 但是,这个方法其实是已经被android抛弃的方法,现在我们来讲一下新的方法。
一 首先我们需要认识到在手机里是没有方位传感器这个实体的硬件,它只是通过一些逻辑运算而计算出来的。而它是通过磁场传感器和加速度传感器计算得来的。
二 实现方法,注册磁场传感器和加速度传感器监听,在监听中获取加速度传感器的Values值和磁场传感器的Values值,根据这俩个值计算方位传感器的value
三 通过SensorManager类的俩个重要方法:getRotationMatrix()和getOrientation()方法获取方位传感器的value。
在getRotationMatrix()方法中, float[] R是输出值,I/gravity/geomagnetic是输入值。
通过float[] I为null,float[] gravity在onSensorChanged()中从加速度传感器中获取,float[] geomagenetic在onSensorChanged()中从磁场传感器中获取。
在getOrientation()方法中,float[] R是输入值,float[] values是输出值,该值就是方位传感器的value值。该值介绍请看下文四。
四 方位传感器的value值是个float[3]数组,
其中values[0]表示手机顶部和正北方的夹角,0表示手机顶部朝向正北,90表示手机顶部朝向正东,180表示手机顶部朝向正南,-90表示手机顶部朝向正西。
values[1]表示手机顶部或尾部翘起的角度。水平放置该值是0。
values[2]表示手机左侧或右侧翘起的角度。水平放置该值是0;
其实我们在指南针的实现中,只用到了values[0]的值。
最后我们看一下完整的代码:
public class MainActivity extends Activity {
private static final String TAG ="LinkGame: MainActivity";
private SensorManager mSensorManager =null;
private Sensor mAcceleSensor = null;
private Sensor mMagneticSensor = null;
private float targetDegree =0.0f;
private MyCompassView mImageView;
private boolean mStopUpdateCompass;
private float[] mMageneticValues = new float[3];
private float[] mAcceleValues = new float[3];
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG," onCreate() ysj");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initCompass();
}
public void initCompass(){
Log.d(TAG," initCompass()");
mImageView = (MyCompassView)findViewById(R.id.compass_image);
mSensorManager = (SensorManager) this.getSystemService(this.SENSOR_SERVICE);
mAcceleSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mMagneticSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d(TAG," onResume() ysj targetDegree ="+targetDegree);
mStopUpdateCompass = false;
mSensorManager.registerListener(mOrientationSensorEventListener,mAcceleSensor , SensorManager.SENSOR_DELAY_NORMAL); //注册加速度传感器监听
mSensorManager.registerListener(mOrientationSensorEventListener,mMagneticSensor , SensorManager.SENSOR_DELAY_NORMAL);//注册磁场传感器监听
new Handler().postDelayed(mRunnable, 100);
}
private Runnable mRunnable = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
if(mImageView != null && mStopUpdateCompass == false){
Log.d(TAG," mImageView().updateCompass: targetDegree ="+targetDegree);
mImageView.updateCompass(targetDegree);
new Handler().postDelayed(mRunnable, 500);
}
}
};
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.d(TAG," onPause() ysj");
mStopUpdateCompass = true;
mSensorManager.unregisterListener(mOrientationSensorEventListener);
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.d(TAG," onStop() ysj");
mStopUpdateCompass = true;
mSensorManager.unregisterListener(mOrientationSensorEventListener);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d(TAG," onDestroy() ysj");
}
private SensorEventListener mOrientationSensorEventListener = new SensorEventListener(){
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
Log.d(TAG," onAccuracyChanged()");
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
int sensorType = event.sensor.getType();
Log.d(TAG," onSensorChanged() sensorType = "+sensorType);
//通过加速度传感器的mAcceleValues和磁场传感器的mMageneticValues,来计算方位传感器的value
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
mAcceleValues = event.values;
}
if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD){
mMageneticValues = event.values;
}
calculateOrientation();
}
};
public void calculateOrientation(){
float[] values = new float[3];
float[] R = new float[9];
SensorManager.getRotationMatrix(R, null, mAcceleValues, mMageneticValues);
SensorManager.getOrientation(R, values);
values[0] = (float)Math.toDegrees(values[0]);
Log.d(TAG," calculateOrientation() values[0]="+values[0] );
targetDegree = (-values[0]+360.0f) % 360;
}
}