本文主要是介绍SensorManager的方向传感器Orientation -- 指南针的简易实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
通过SensorManager获取手机方位,从而实现指南针功能。
在很多旧的文档介绍中,都是通过SensorManager .getDefaultSensor(Sensor.TYPE_ORIENTATION); 但是,这个方法其实是已经被android抛弃的方法,现在我们来讲一下新的方法。
一 首先我们需要认识到在手机里是没有方位传感器这个实体的硬件,它只是通过一些逻辑运算而计算出来的。而它是通过磁场传感器和加速度传感器计算得来的。
二 实现方法,注册磁场传感器和加速度传感器监听,在监听中获取加速度传感器的Values值和磁场传感器的Values值,根据这俩个值计算方位传感器的value
三 通过SensorManager类的俩个重要方法:getRotationMatrix()和getOrientation()方法获取方位传感器的value。
boolean getRotationMatrix (float[] R, float[] I, float[] gravity, float[] geomagnetic)
float[] getOrientation (float[] R, float[] values)
在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];@Overrideprotected 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);}@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.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(){@Overridepublic void run() {// TODO Auto-generated method stubif(mImageView != null && mStopUpdateCompass == false){ Log.d(TAG," mImageView().updateCompass: targetDegree ="+targetDegree);mImageView.updateCompass(targetDegree);new Handler().postDelayed(mRunnable, 500);}}};@Overrideprotected void onPause() {// TODO Auto-generated method stubsuper.onPause();Log.d(TAG," onPause() ysj");mStopUpdateCompass = true;mSensorManager.unregisterListener(mOrientationSensorEventListener);}@Overrideprotected void onStop() {// TODO Auto-generated method stubsuper.onStop();Log.d(TAG," onStop() ysj");mStopUpdateCompass = true;mSensorManager.unregisterListener(mOrientationSensorEventListener);}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();Log.d(TAG," onDestroy() ysj");}private SensorEventListener mOrientationSensorEventListener = new SensorEventListener(){@Overridepublic void onAccuracyChanged(Sensor arg0, int arg1) {// TODO Auto-generated method stubLog.d(TAG," onAccuracyChanged()");}@Overridepublic void onSensorChanged(SensorEvent event) {// TODO Auto-generated method stubint sensorType = event.sensor.getType();Log.d(TAG," onSensorChanged() sensorType = "+sensorType);//通过加速度传感器的mAcceleValues和磁场传感器的mMageneticValues,来计算方位传感器的valueif(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; }}
这篇关于SensorManager的方向传感器Orientation -- 指南针的简易实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!