您好,登錄后才能下訂單哦!
本文實例講述了Android開發(fā)獲取重力加速度和磁場強(qiáng)度的方法。分享給大家供大家參考,具體如下:
Android獲取重力加速度和磁場強(qiáng)度主要依靠:
Sensor.getRotationMatrix (float[] R, float[] I, float[] gravity, float[] geomagnetic)
輸入數(shù)據(jù):
gravity為重力傳感器測得的重力加速度float[3];
geomagnetic為羅盤傳感器測得的地磁數(shù)據(jù)float[3];
輸出數(shù)據(jù):
R為通過這個方法計算得到的從手機(jī)的設(shè)備到坐標(biāo)系到真實世界坐標(biāo)系的轉(zhuǎn)換矩陣
I為從將地磁數(shù)據(jù)轉(zhuǎn)換到重力數(shù)據(jù)所在坐標(biāo)的旋轉(zhuǎn)矩陣
[0 0 g] = R * gravity (g = 重力加速度)
[0 m 0] = I * R * geomagnetic (m = 磁場強(qiáng)度)
下面是我做的結(jié)果圖
核心部分代碼如下:
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"+"磁場強(qiáng)度" + 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("關(guān)閉", 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); } }
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。