Android的Sensor.TYPE_ORIENTATION完美的作品,我不能与加速度计和磁力相同的结果 [英] Android Sensor.TYPE_ORIENTATION works perfect and I can't get the same results with accelerometer and magnetometer

查看:273
本文介绍了Android的Sensor.TYPE_ORIENTATION完美的作品,我不能与加速度计和磁力相同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想利用这个pcated非德$ P $的code版本,以获得取向。不过,我不能让它的工作顺利和完美的,因为如果我只是用pcated Sensor.TYPE_ORIENTATION去$ P $。我失去了一些东西?

// OLD

  @覆盖
公共无效onSensorChanged(SensorEvent为arg0){
    m_lastYaw = arg0.values​​ [0];
    无效();
}
 

// NEW

  @覆盖
公共无效onSensorChanged(SensorEvent事件){
    如果(event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE){
        返回;
    }

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

    如果(SensorManager.getRotationMatrix(m_rotationMatrix,空,m_lastAccels,m_lastMagFields)){
        SensorManager.getOrientation(m_rotationMatrix,m_orientation);

        m_lastYaw =(浮点)Math.toDegrees(m_orientation [0]);
        无效();
    }
}
 

解决方案

我用,我在这里提出的code 和它的工作很好,只有当手机(三星Galaxy S3)奠定持平。但它是一个开始:它不工作比 Sensor.TYPE_ORIENTATION 更好,也不更坏

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

解决方案

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.

这篇关于Android的Sensor.TYPE_ORIENTATION完美的作品,我不能与加速度计和磁力相同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆