我知道有关于此的主题,而且我已经阅读了好几天。
我认为我的问题更具体。我正在尝试使设备绕y轴旋转。
如果我是正确的,那就叫做方位角
当我运行应用程序时,在调试记录值[0],值[1]和值[2]后,它在logcat中返回0.0
码:
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
SensorEventListener SEL = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
mSensorManager.getRotationMatrix(rotationMatrix, I, gravity, geomagnetic);
mSensorManager.getOrientation(rotationMatrix, values);
accy = values[0];
System.out.println(accy);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
int rate = SensorManager.SENSOR_DELAY_GAME;
mSensorManager.registerListener(SEL, accelerometer, rate);
mSensorManager.registerListener(SEL, magnetometer, rate);
Logcat:
09-05 17:34:02.622 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.642 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.642 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.662 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.662 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.682 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.682 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.702 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.702 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.722 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.722 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.742 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.742 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.762 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.762 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.782 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.782 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.802 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
09-05 17:34:02.802 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0
最佳答案
从上面的代码中,gravity
中的参数geomagnetic
和mSensorManager.getRotationMatrix(rotationMatrix, I, gravity, geomagnetic);
可能是zero array
,因此您的accy
始终为zero