您好,登錄后才能下訂單哦!
本文實例講述了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); } }
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android視圖View技巧總結》、《Android編程之activity操作技巧總結》、《Android文件操作技巧匯總》、《Android資源操作技巧匯總》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。