android - 通过加速度计获取方向值

标签 android accelerometer

我目前正在研究如何通过加速度计值获取方向值(偏航、俯仰、滚动)。 下面是我目前使用的方式,但是orientation值好像不对,是不是有什么问题或者错误我忽略了? 非常感谢您的帮助!

if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        accelerometerValues = event.values;
    }

    if (sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
        magneticFieldValues = event.values;
    }

    SensorManager.getRotationMatrix(rotate, null, accelerometerValues,
            magneticFieldValues);
    SensorManager.getOrientation(rotate, rotationValues);

    // change radian to degree
    rotationValues[0] = (float) Math.toDegrees(rotationValues[0]);
    rotationValues[1] = (float) Math.toDegrees(rotationValues[1]);
    rotationValues[2] = (float) Math.toDegrees(rotationValues[2]);

最佳答案

getRotationMatrix() 和getOrientationMatrix 使用的坐标轴不同。所以需要旋转上面的rotate矩阵代码如下:

SensorManager.getRotationMatrix(rotate, null, accelerometerValues,
        magneticFieldValues);
SensorManager.remapCoordinateSystem(rotate, SensorManager.AXIS_X, SensorManager.AXIS_MINUS_Z, rotate); //Overwriting rotate matrix with the rotated values
SensorManager.getOrientation(rotate, rotationValues);

关于android - 通过加速度计获取方向值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14757625/

相关文章:

android - 如何存储加速度计值

android - 在android中计算用户移动速度

Android:切换加速度计旋转状态设置

c# - 为 SVM 准备加速度计数据

android - '错误 : Ambient Vertex Buffer overflow!!使用 304,总共 260' Android ViewPropertyAnimator

android - braintree 集成导致 Android 问题

iPhone "multi-threading"问题

java - 检查导航栏

c# - Microsoft Azure 移动服务 - Xamarin.Android 离线同步问题

android - 图片上传不适用于 WKWebView,但适用于 SFSafariViewController