本文实例讲述了Android开发获取重力加速度和磁场强度的方法。分享给大家供大家参考,具体如下:
Android获取重力加速度和磁场强度主要依靠:
Sensor.getRotationMatrix (float[] R, float[] I, float[] gravity, float[] geomagnetic)
输入数据:
gravity为重力传感器测得的重力加速度float[3];
geomagnetic为罗盘传感器测得的地磁数据float[3];
输出数据:
R为通过这个方法计算得到的从手机的设备到坐标系到真实世界坐标系的转换矩阵
I为从将地磁数据转换到重力数据所在坐标的旋转矩阵
[0 0 g] = R * gravity (g = 重力加速度)
[0 m 0] = I * R * geomagnetic (m = 磁场强度)
下面是我做的结果图
核心部分代码如下:
public class MainActivity extends AppCompatActivity implements SensorEventListener { private static final String TAG = "CompassActivity"; private SensorManager sensorManager; //记录rotationMatrix矩阵值 private float[] r = new float[9]; private float[] gravity = null; private float[] geomagnetic = null; private float[] I=new float[9]; private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { if(gravity!=null&&geomagnetic!=null) { if(SensorManager.getRotationMatrix(r, I, gravity, geomagnetic)) { float gri = gravity[0] * r[6] + gravity[1] * r[7] + gravity[2] * r[8]; float h= (I[3]*r[0]+I[4]*r[3]+I[5]*r[6])*geomagnetic[0]+ (I[3]*r[1]+I[4]*r[4]+I[5]*r[7])*geomagnetic[1]+ (I[3]*r[2]+I[4]*r[5]+I[5]*r[8])*geomagnetic[2]; TextView textView = (TextView) findViewById(R.id.textView); textView.setText("重力加速度" + gri + "n"+"磁场强度" + h + "n"); } } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void onSensorChanged(SensorEvent sensorEvent) { switch (sensorEvent.sensor.getType()){ case Sensor.TYPE_ACCELEROMETER: gravity=sensorEvent.values; handler.sendEmptyMessage(0); break; case Sensor.TYPE_MAGNETIC_FIELD: geomagnetic=sensorEvent.values; handler.sendEmptyMessage(0); break; default:break; } } @Override public void onAccuracyChanged(Sensor sensor, int i) { } private void showMessage(String message){ View view=findViewById(R.id.fab); final Snackbar snackbar=Snackbar.make(view, message, Snackbar.LENGTH_INDEFINITE); snackbar.setAction("关闭", new View.OnClickListener() { @Override public void onClick(View view) { snackbar.dismiss(); } }); snackbar.show(); } @Override protected void onPause() { super.onPause(); sensorManager.unregisterListener(this); } @Override protected void onResume() { super.onResume(); Sensor sensor1=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); sensorManager.registerListener(this,sensor1,SensorManager.SENSOR_DELAY_UI); Sensor sensor2=sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); sensorManager.registerListener(this,sensor2,SensorManager.SENSOR_DELAY_UI); } }