且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-09 19:51:41

我已找到&这是在读取pitch& roll:

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

-更新-

&这是我完整的实用程序类的实现

& here is my full utility class implementation

-更新-

添加此图像有助于可视化azimuthpitch& roll:

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