0
我是Android編程的新手,只是玩弄瞭解更多。我正在嘗試使用陀螺儀,但我注意到,當我將陀螺儀平放在桌面上時,數值仍在移動。如果我沒有錯,這是由於一些漂移問題。這些是我的以下代碼。有關Android陀螺儀需要的新手幫助
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager sm = null;
private Sensor mGyroscope=null;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
mGyroscope = sm.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView) findViewById(R.id.txt);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
protected void onResume() {
super.onResume();
sm.registerListener(this, mGyroscope, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
sm.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
txt.setText("Orientation X (Roll) :"+ Float.toString(event.values[2]) +"\n"+
"Orientation Y (Pitch) :"+ Float.toString(event.values[1]) +"\n"+
"Orientation Z (Yaw) :"+ Float.toString(event.values[0]));
}
}
我看了android api,但我並沒有真正明白它的含義。希望有人能幫助我。
附上參考:http://developer.android.com/reference/android/hardware/SensorEvent.html#values