我正在设计一个每秒显示陀螺仪和加速度计读数的应用程序。这是我使用的代码 我有一个开始按钮,在OnCreate方法中将时间设置为当前。
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
time = System.currentTimeMillis();
mSensorManager.registerListener(MainActivity.this, mAccelerometerSensor, SensorManager.SENSOR_DELAY_GAME);
mSensorManager.registerListener(MainActivity.this, mGyroscopeSensor, SensorManager.SENSOR_DELAY_GAME);
}
然后在onSensorChanged:
public void onSensorChanged(SensorEvent event) {
long currentTime = System.currentTimeMillis();
if (currentTime - time > 1000) {
time = currentTime;
switch (event.sensor.getType()) {
case Sensor.TYPE_GYROSCOPE: {
mXGyrValueText.setText(String.format("%1.3f",
event.values[SensorManager.DATA_X]));
mYGyrValueText.setText(String.format("%1.3f",
event.values[SensorManager.DATA_Y]));
mZGyrValueText.setText(String.format("%1.3f",
event.values[SensorManager.DATA_Z]));
break;
}
case Sensor.TYPE_LINEAR_ACCELERATION: {
mXAccValueText.setText(String.format("%1.3f",
event.values[SensorManager.DATA_X]));
mYAccValueText.setText(String.format("%1.3f",
event.values[SensorManager.DATA_Y]));
mZAccValueText.setText(String.format("%1.3f",
event.values[SensorManager.DATA_Z]));
break;
}
}
}
这适用于陀螺仪,但加速度计读数在随机时间段更新。问题是什么?也许,有一个更好的解决方案?