Android:从方位角、滚动和获取设备方向沥青

Android: get device orientation from Azimuth, roll amp; pitch(Android:从方位角、滚动和获取设备方向沥青)

本文介绍了Android:从方位角、滚动和获取设备方向沥青的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

SensorEventListener sensorEventListener = new SensorEventListener() {浮动[] 重力;浮动[] m地磁;@覆盖公共无效 onSensorChanged(SensorEvent 事件){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) {浮动 R[] = 新浮动 [9];浮动 I[] = 新浮动 [9];布尔成功 = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);如果(成功){浮动方向数据[] = 新浮动[3];SensorManager.getOrientation(R,orientationData);方位角=方向数据[0];音高=方向数据[1];滚动=方向数据[2];//现在如何使用前 3 个值来计算方向}}}@覆盖公共无效onAccuracyChanged(传感器传感器,int精度){//TODO 自动生成的方法存根}};

现在问题是,如何使用azimuthpitch & 3个值roll 将当前设备方向检测为以下之一:

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

解决方案

我找到了&这是在读取 pitch & 后将在侦听器内部调用的计算函数滚动:

public static final int ORIENTATION_PORTRAIT = 0;公共静态最终 int ORIENTATION_LANDSCAPE_REVERSE = 1;公共静态最终 int ORIENTATION_LANDSCAPE = 2;公共静态最终 int ORIENTATION_PORTRAIT_REVERSE = 3;公共 int 方向 = ORIENTATION_PORTRAIT;私人 int 计算方向(int roll,int pitch){如果(((方向== ORIENTATION_PORTRAIT || 方向== ORIENTATION_PORTRAIT_REVERSE)&&(滚动 > -30 && 滚动 < 30))) {如果(平均间距 > 0)返回 ORIENTATION_PORTRAIT_REVERSE;别的返回 ORIENTATION_PORTRAIT;} 别的 {//在所有方向之间划分if (Math.abs(pitch) >= 30) {如果(间距 > 0)返回 ORIENTATION_PORTRAIT_REVERSE;别的返回 ORIENTATION_PORTRAIT;} 别的 {if (averageRoll > 0) {返回 ORIENTATION_LANDSCAPE_REVERSE;} 别的 {返回 ORIENTATION_LANDSCAPE;}}}}

-- 更新--

&这是我完整的

I need to get the updated device orientation, but I have to fix my activity to Portrait (which I did in the layout xml file), which prevents me from using this:

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

because it always gives me the portrait rotation,

So, I'm trying to rely on the sensors. I found that Sensor.TYPE_ORIENTATION is deprecated, so I'm using a combination of Sensor.TYPE_ACCELEROMETER & Sensor.TYPE_MAGNETIC_FIELD and here is the event listener:

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

    }
};

Now Question is, how to use the 3 values azimuth, pitch & roll to detect current device orientation as one of these:

  • Landscape (rotation 0)
  • Portrait (rotation 90)
  • Reverse Landscape (rotation 180)
  • Reverse Portrait (rotation 270)

解决方案

I've found it & here is the calculating function that will be called inside the listener after reading the pitch & roll:

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

-- Update --

& here is my full utility class implementation

-- Update --

Adding this image to help visualizing the azimuth, pitch & roll:

这篇关于Android:从方位角、滚动和获取设备方向沥青的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android:从方位角、滚动和获取设备方向沥青

基础教程推荐