如何获得Android设备的当前方向(ActivityInfo.SCREEN_ORIENTATION_ *)? [英] How do I get the CURRENT orientation (ActivityInfo.SCREEN_ORIENTATION_*) of an Android device?

查看:3350
本文介绍了如何获得Android设备的当前方向(ActivityInfo.SCREEN_ORIENTATION_ *)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找出一个设备,$ P $的具体方位pferably SCREEN_ORIENTATION_LANDSCAPE之一 SCREEN_ORIENTATION_PORTRAIT SCREEN_ORIENTATION_REVERSE_LANDSCAPE SCREEN_ORIENTATION_REVERSE_PORTRAIT ActivityInfo 或同等学历。

这里的一些答案的StackOverflow包括了

  getWindowManager()。getDefaultDisplay()。getRotation()
 

但这并没有真正告诉我,该设备是在纵向或横向模式下,只有它是如何变成参照它的自然位置 - 这反过来又可以横向或纵向摆在首位

  getResources()。getConfiguration()。方向
 

返回以下三种: ORIENTATION_LANDSCAPE ORIENTATION_PORTRAIT ORIENTATION_SQUARE ,然后并没有真正告诉我哪条路手机处于关机(不管是上下或两侧的其它的转向)。

我知道我可以使用后者结合 DisplayMetrics 查找设备的自然倾向,但真的没有更好的办法?

解决方案

我最终使用了以下解决方案:

 私人诠释getScreenOrientation(){
    INT旋转= getWindowManager()getDefaultDisplay()getRotation()。
    DisplayMetrics DM =新DisplayMetrics();
    。getWindowManager()getDefaultDisplay()getMetrics(DM)。
    INT宽度= dm.widthPixels;
    INT高= dm.heightPixels;
    INT方向;
    //如果设备的自然方向为纵向:
    如果((旋转== Surface.ROTATION_0
            ||旋转== Surface.ROTATION_180)及&安培;高度>宽度||
        (旋转== Surface.ROTATION_90
            ||旋转== Surface.ROTATION_270)及&安培;宽度GT;高度) {
        开关(旋转){
            案例Surface.ROTATION_0:
                定向= ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                打破;
            案例Surface.ROTATION_90:
                定向= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                打破;
            案例Surface.ROTATION_180:
                定向=
                    ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
                打破;
            案例Surface.ROTATION_270:
                定向=
                    ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
                打破;
            默认:
                Log.e(TAG,未知屏幕方向。默认到+
                        肖像。);
                定向= ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                打破;
        }
    }
    //如果设备的自然方向为横向,或者如果该装置
    //为方形:
    其他 {
        开关(旋转){
            案例Surface.ROTATION_0:
                定向= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                打破;
            案例Surface.ROTATION_90:
                定向= ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                打破;
            案例Surface.ROTATION_180:
                定向=
                    ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
                打破;
            案例Surface.ROTATION_270:
                定向=
                    ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
                打破;
            默认:
                Log.e(TAG,未知屏幕方向。默认到+
                        景观。);
                定向= ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                打破;
        }
    }

    返回的方向;
}
 

注意:有些用户(Geltrude和holtaf在下面的评论)指出,这一解决方案将不会在所有设备上从自然倾向是不规范的旋转方向努力。

I would like to find out the detailed orientation of a device, preferably one of SCREEN_ORIENTATION_LANDSCAPE, SCREEN_ORIENTATION_PORTRAIT, SCREEN_ORIENTATION_REVERSE_LANDSCAPE, SCREEN_ORIENTATION_REVERSE_PORTRAIT from ActivityInfo or equivalent.

Some of the answers here on StackOverflow included

getWindowManager().getDefaultDisplay().getRotation()

but this doesn't really tell me whether the device is in portrait or landscape mode, only how it's turned with reference to its natural position - which in turn can be landscape or portrait in the first place.

getResources().getConfiguration().orientation

returns one of the following three: ORIENTATION_LANDSCAPE, ORIENTATION_PORTRAIT, ORIENTATION_SQUARE, which then doesn't really tell me which way the phone is turned (whether it's upside down or which of the sides it's turned to).

I know I could use the latter in combination with DisplayMetrics to find out the device's natural orientation, but is there really no better way?

解决方案

I ended up using the following solution:

private int getScreenOrientation() {
    int rotation = getWindowManager().getDefaultDisplay().getRotation();
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels;
    int height = dm.heightPixels;
    int orientation;
    // if the device's natural orientation is portrait:
    if ((rotation == Surface.ROTATION_0
            || rotation == Surface.ROTATION_180) && height > width ||
        (rotation == Surface.ROTATION_90
            || rotation == Surface.ROTATION_270) && width > height) {
        switch(rotation) {
            case Surface.ROTATION_0:
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;
            case Surface.ROTATION_90:
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                break;
            case Surface.ROTATION_180:
                orientation =
                    ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
                break;
            case Surface.ROTATION_270:
                orientation =
                    ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
                break;
            default:
                Log.e(TAG, "Unknown screen orientation. Defaulting to " +
                        "portrait.");
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;              
        }
    }
    // if the device's natural orientation is landscape or if the device
    // is square:
    else {
        switch(rotation) {
            case Surface.ROTATION_0:
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                break;
            case Surface.ROTATION_90:
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;
            case Surface.ROTATION_180:
                orientation =
                    ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
                break;
            case Surface.ROTATION_270:
                orientation =
                    ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
                break;
            default:
                Log.e(TAG, "Unknown screen orientation. Defaulting to " +
                        "landscape.");
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                break;              
        }
    }

    return orientation;
}

NOTE: Some users (Geltrude and holtaf in the comments below) pointed out that this solution will not work on all devices as the direction of rotation from the natural orientation is not standardized.

这篇关于如何获得Android设备的当前方向(ActivityInfo.SCREEN_ORIENTATION_ *)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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