我目前正在尝试更换我的'Sensor.TYPE_ORIENTATION',因为它已被弃用。
所以Android Documentation缺少大部分信息,关于如何做到这一点。在SO上进行调试和挖掘时,我想出了如何计算曾经由OrientationSensor提供的azimuth
。
我是这样做的:
float accelerometerVals[] = new float[3];
float magneticVals[] = new float[3];
float orientationSensorVals[] = new float[3];
float rotationMatrix[] = new float[16];
float orientation[] = new float[3];
@Override
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
System.arraycopy(event.values, 0, accelerometerVals, 0, 3);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
System.arraycopy(event.values, 0, magneticVals, 0, 3);
break;
case Sensor.TYPE_ORIENTATION:
System.arraycopy(event.values, 0, orientationSensorVals, 0, 3);
break;
default:
break;
}
if (null != magneticVals && null != accelerometerVals){
SensorManager.getRotationMatrix(rotationMatrix, null, accelerometerVals, magneticVals)
SensorManager.getOrientation(rotationMatrix, orientation);
float azimuth = Math.toDegrees(orientation[0])
//this calculation gives me the Azimuth in the same format that OrientationSensor
azimuth += (azimuth >= 0) ? 0 : 360
float FALSE_PITCH = Math.toDegrees(orientation[1])
float FALSE_ROLL = Math.toDegrees(orientation[2])
}
注意变量FALSE_PITCH&& FALSE_ROLL。我不知道如何将这个值“规范化”(值从+10到-10不等),以获得与之前在OrientationSensor中相同的输出
答案 0 :(得分:6)
我遇到了同样的问题,但我很惊讶你的滚动是不对的,因为这对我来说。这是我使用的:
//Let values[] be your orientation[] variable once in degrees.
// Azimuth scaling
if (values[0]<0) values[0] += 360;
// Pitch scaling
if (values[1]<-90) values[1] += (-2*(90+values[1]));
else if (values[1]>90) values[1] += (2*(90-values[1]));
// Roll scaling
// NOT NEEDED
您是否能够发布方向传感器和度数方向[]变量的某些输出?
答案 1 :(得分:1)
也许它会对你有所帮助!!
SensorManager mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
boolean orientok = mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_UI);
//test if orientation sensor exist on the device
if (!orientok){
mSensorManager.unregisterListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION));
((TextView)findViewById(R.id.orient)).setText("no orientation sensor");
}
public void onSensorChanged(SensorEvent event) {
switch(event.sensor.getType()){
case Sensor.TYPE_ORIENTATION:
onOrientChanged(event);
break;
...// add other sensor type if you want and create for each one his function like the example below
}
public void onOrientChanged(SensorEvent event) {
float azimuth,pitch,roll;
azimuth = event.values[0];
pitch = event.values[1];
roll = event.values[2];
((TextView)findViewById(R.id.azimuth)).setText("Axe x "+azimuth);
((TextView)findViewById(R.id.pitch)).setText("Axe y "+pitch);
((TextView)findViewById(R.id.roll)).setText("Axe z "+roll);
}