1

我目前正在尝试替换我的“Sensor.TYPE_ORIENTATION”,因为它已被弃用。因此,Android 文档缺少大部分关于如何操作的信息。在此处调试和挖掘SO时,我想出了如何计算azimuthOrientationSensor 曾经提供的值。我这样做:

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 中相同的输出

4

2 回答 2

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

你能发布方向传感器和你的度数方向[]变量的一些输出吗?

于 2012-04-18T08:08:51.227 回答
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);
}
于 2012-04-18T14:25:58.173 回答