【问题标题】:Android: get device orientation from Azimuth, roll & pitchAndroid:从方位角、滚动和俯仰获取设备方向
【发布时间】:2014-09-22 11:01:41
【问题描述】:

我需要获取更新的设备方向,但我必须将我的活动修复为肖像(我在布局 xml 文件中所做的),这使我无法使用它:

int rotation = getWindowManager().getDefaultDisplay().getRotation();

因为它总是给我纵向旋转,

所以,我正在尝试依靠传感器。我发现Sensor.TYPE_ORIENTATION 已被弃用,所以我使用Sensor.TYPE_ACCELEROMETERSensor.TYPE_MAGNETIC_FIELD 的组合,这里是事件监听器:

SensorEventListener sensorEventListener = new SensorEventListener() {
    float[] mGravity;
    float[] mGeomagnetic;

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
            mGravity = event.values;
        if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
            mGeomagnetic = event.values;
        if (mGravity != null && mGeomagnetic != null) {
            float R[] = new float[9];
            float I[] = new float[9];
            boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
            if (success) {
                float orientationData[] = new float[3];
                SensorManager.getOrientation(R, orientationData);
                azimuth = orientationData[0];
                pitch = orientationData[1];
                roll = orientationData[2];
                // now how to use previous 3 values to calculate orientation
            }
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }
};

现在问题是,如何使用 3 个值 azimuthpitchroll 来检测当前设备方向作为其中之一:

  • 横向(旋转 0)
  • 纵向(旋转 90)
  • 反向横向(旋转 180)
  • 反转纵向(旋转 270)

【问题讨论】:

  • @HoanNguyen 看起来不错,但在这种情况下,getRotationMatrix 的参数是什么?
  • 设备方向与方位角、俯仰角和滚动角无关。如果您只想查找设备方向,则无需查找旋转矩阵,因此无需调用 getRotationMatrix。否则你仍然需要做我发布的计算。
  • 但是您之前发布的答案缺少一些“对于平放的情况,您必须使用指南针查看设备从起始位置旋转了多少。”
  • OK,在平放的情况下,你必须传入重力和磁场的参数,并使用方位角来查看设备从原始位置旋转了多少。但是,这不会告诉您设备的方向,因为方向取决于用户握持设备的方式。它只告诉您设备从用户持有设备的原始位置旋转了多少。

标签: android rotation android-sensors


【解决方案1】:

我找到了,这里是读取pitchroll后将在监听器内部调用的计算函数:

public static final int ORIENTATION_PORTRAIT = 0;
public static final int ORIENTATION_LANDSCAPE_REVERSE = 1;
public static final int ORIENTATION_LANDSCAPE = 2;
public static final int ORIENTATION_PORTRAIT_REVERSE = 3;
public int orientation = ORIENTATION_PORTRAIT;

private int calculateOrientation(int roll, int pitch) {
    if (((orientation == ORIENTATION_PORTRAIT || orientation == ORIENTATION_PORTRAIT_REVERSE)
            && (roll > -30 && roll < 30))) {
        if (averagePitch > 0)
            return ORIENTATION_PORTRAIT_REVERSE;
        else
            return ORIENTATION_PORTRAIT;
    } else {
        // divides between all orientations
        if (Math.abs(pitch) >= 30) {
            if (pitch > 0)
                return ORIENTATION_PORTRAIT_REVERSE;
            else
                return ORIENTATION_PORTRAIT;
        } else {
            if (averageRoll > 0) {
                return ORIENTATION_LANDSCAPE_REVERSE;
            } else {
                return ORIENTATION_LANDSCAPE;
            }
        }
    }
}

-- 更新--

& 这是我完整的utility class 实现

-- 更新--

添加此图像以帮助可视化 azimuthpitchroll

【讨论】:

  • 您的代码中的averagePitchaverageRoll 是什么?
  • @agamov 对此感到抱歉:),我已经用我的整个实施的链接更新了我的答案。
  • 请您在这里简单解释一下calculateOrientation方法的逻辑,谢谢?
  • 好吧,这是很久以前的事了,有很多反复试验的方法:)。但据我记得,当处于纵向时(我的应用程序默认方向),用户可以roll 设备稍微向右或向左,但如果他们滚动太多(超过 30 度),那么我们需要重新计算新的情况。请参阅此图片以更好地了解滚动和俯仰 mathworks.com/matlabcentral/mlc-downloads/downloads/submissions/…
猜你喜欢
  • 2019-01-15
  • 1970-01-01
  • 2018-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
相关资源
最近更新 更多