我正在尝试为nVidia防护罩创建增强现实应用程序。
我在另一台Android设备上尝试了我的应用,并且可以正常运行。
不幸的是,在屏蔽层上,不会触发onSensorChanged事件。
这是我的代码:
_sensorEventListener = new SensorEventListener()
{
public void onSensorChanged(SensorEvent event) {
AndroidAttitude.this.processSensorEvent(event);
}
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
}
};
Thread sensorThread = new Thread(new Runnable()
{
public void run() {
Looper.prepare();
_sensorLooper = Looper.myLooper();
Handler handler = new Handler();
_sensorManager = (SensorManager)_context.getSystemService("sensor");
Sensor sensor = _sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
_sensorManager.registerListener(_sensorEventListener, sensor, 0, _sensorManager.SENSOR_DELAY_GAME);
Looper.loop();
}
});
sensorThread.start();
这是将值转换为旋转矩阵的地方:
private synchronized void processSensorEvent(SensorEvent event)
{
float[] rotationVector = { -event.values[1], event.values[0], event.values[2] };
float[] quaternion = new float[4];
float[] rotationMatrix = new float[16];
_sensorManager.getQuaternionFromVector(quaternion, rotationVector);
_sensorManager.getRotationMatrixFromVector(rotationMatrix, rotationVector);
}
知道为什么它行不通吗?
最佳答案
好的,如果您必须使用TYPE_ROTATION_VECTOR
,则将SENSOR_DELAY_GAME
更改为SENSOR_DELAY_NORMAL
Sensor sensor = _sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
_sensorManager.registerListener(_sensorEventListener, sensor, 0, _sensorManager.SENSOR_DELAY_NORMAL)