【问题标题】:Android Sensor.TYPE_ORIENTATION works perfect and I can't get the same results with accelerometer and magnetometerAndroid Sensor.TYPE_ORIENTATION 完美运行,加速度计和磁力计无法获得相同的结果
【发布时间】:2012-07-31 15:56:59
【问题描述】:

我正在尝试使用此非弃用版本的代码来获取方向。但是,我无法让它像使用已弃用的 Sensor.TYPE_ORIENTATION 一样顺畅和完美地工作。我错过了什么吗?

//旧

@Override
public void onSensorChanged(SensorEvent arg0) {
    m_lastYaw = arg0.values[0];
    invalidate();
}

//新的

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE){
        return;
    }

    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        System.arraycopy(event.values, 0, m_lastAccels, 0, 3);
    }
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
        System.arraycopy(event.values, 0, m_lastMagFields, 0, 3);
    }

    if (SensorManager.getRotationMatrix(m_rotationMatrix, null, m_lastAccels, m_lastMagFields)) {
        SensorManager.getOrientation(m_rotationMatrix, m_orientation); 

        m_lastYaw = (float)Math.toDegrees(m_orientation[0]);
        invalidate();
    }
}

【问题讨论】:

  • 您不应该忽略 SensorManager.SENSOR_STATUS_UNRELIABLE 值,因为在某些低端设备上,这是您将获得的唯一值(这些值实际上在我的测试中可用)

标签: android android-sensors


【解决方案1】:

我使用了我提交的代码here,并且只有在手机(三星 Galaxy S3)平放时才能正常工作。但这是一个开始:它不会比Sensor.TYPE_ORIENTATION 更好,也不会更糟。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    相关资源
    最近更新 更多