java 在 Android 2.1 中使用 getRotationMatrix 和 getOrientation

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2963705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:42:46  来源:igfitidea点击:

Using getRotationMatrix and getOrientation in Android 2.1

javaandroideventsmatrixsensor

提问by aaronmar

I've been having issues with this for far too long. This code should output dx,dy,dz for the accelerometer, and a running total of the dx. It should also output azimuth, pitch, and roll.

我一直有这个问题太久了。此代码应为加速度计输出 dx,dy,dz,以及 dx 的运行总计。它还应该输出方位角、俯仰角和滚转角。

I've used the information given here, but to no avail.

我已经使用了此处提供的信息,但无济于事。

This code does not correctly output pitch, azimuth, or roll. It outputs 0.0, -0.0, -0.0 for the last three textviews, respectively.

此代码未正确输出俯仰、方位角或滚转。它分别为最后三个文本视图输出 0.0、-0.0、-0.0。

switch (event.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
        accelerometerValues = event.values.clone();
    case Sensor.TYPE_MAGNETIC_FIELD:
        geomagneticMatrix = event.values.clone();
        sensorReady = true;
        break;
    default:
        break;
}   

if (geomagneticMatrix != null && accelerometerValues != null && sensorReady) {
    sensorReady = false;

    float[] R = new float[16];
    float[] I = new float[16];

    SensorManager.getRotationMatrix(R, I, accelerometerValues, geomagneticMatrix);

    float[] actual_orientation = new float[3];
    SensorManager.getOrientation(R, actual_orientation);

    tvXCoordinate.setText(accelerometerValues[0] + "");
    tvYCoordinate.setText(accelerometerValues[1] + "");
    tvZCoordinate.setText(accelerometerValues[2] + "");

    floatXTotal += accelerometerValues[0];
    tvXTotal.setText(floatXTotal + "");

    tvAzimuth.setText(actual_orientation[0] + "");
    tvPitch.setText(actual_orientation[1] + "");
    tvRoll.setText(actual_orientation[2] + "");
}

采纳答案by aganders3

I might be missing something (and you may have solved this already), but to me it looks like your switch statement is incorrect:

我可能遗漏了一些东西(你可能已经解决了这个问题),但在我看来你的 switch 语句是不正确的:

switch (event.sensor.getType()) {
        case Sensor.TYPE_ACCELEROMETER:
            accelerometerValues = event.values.clone();
        case Sensor.TYPE_MAGNETIC_FIELD:
            geomagneticMatrix = event.values.clone();
            sensorReady = true;
            break;
        default:
            break;
    }

If your sensor event is TYPE_ACCELEROMETERthe values from the event will be cloned to both accelerometerValuesand geomagneticMatrixand sensorReadywill be set to true. I think you may need to change the order of this block, or possibly add a break;after your first case.

如果传感器事件TYPE_ACCELEROMETER从事件的值将被克隆到两个accelerometerValuesgeomagneticMatrixsensorReady将设置为true。我认为您可能需要更改此块的顺序,或者可能break;在您的第一个案例之后添加一个。

回答by schufty

The reason you're getting 0.0, -0.0, -0.0 from getOrientation() is that getRotationMatrix() doesn't always get a valid result. You need to check getRotationMatrix()'s return value, which will be false if the result is invalid, or true if it succeeded.

你从 getOrientation() 得到 0.0, -0.0, -0.0 的原因是 getRotationMatrix() 并不总是得到一个有效的结果。您需要检查 getRotationMatrix() 的返回值,如果结果无效则返回 false,如果成功则返回 true。

Added: Actually, that didn't come out right. You're getting an invalid result for the reason aganders pointed out. Checking the return value would simply be an indication that you were in fact getting an invalid result.

补充:实际上,这并不正确。由于 aganders 指出的原因,您得到的结果无效。检查返回值只是表明您实际上得到了无效结果。