我知道有关于此的主题,并且我已经阅读它们好几天了。
我认为我的问题更具体。我正在尝试让设备绕 y 轴旋转。
如果我没猜错的话,它被称为方位角
当我运行应用程序时,在调试日志记录 value[0]、values[1] 和 value[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);
日志猫:
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
最佳答案
从上面的代码中,mSensorManager.getRotationMatrix(rotationMatrix, I,重力,地磁);
中的参数gravity
和geotropic
可能是zero array
因此你的 accy
总是 zero
关于java - Android 传感器直立旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25694307/