Android Sensor.TYPE_ORIENTATION works perfect and I can't get the same results with accelerometer and magnetometer

爱⌒轻易说出口 提交于 2019-12-10 11:28:25

问题


I am trying to use this non-deprecated version of code to get orientation. However I can't get it to work as smooth and perfect as if I just use the deprecated Sensor.TYPE_ORIENTATION. Am I missing something?

//OLD

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

//NEW

@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();
    }
}

回答1:


I used the code that I posed here and it worked well only when the phone (Samsung Galaxy S3) layed flat. But it's a start: It doesn't work better than Sensor.TYPE_ORIENTATION nor worse.



来源:https://stackoverflow.com/questions/11744201/android-sensor-type-orientation-works-perfect-and-i-cant-get-the-same-results-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!