如何获取Android设备的CURRENT方向(ActivityInfo.SCREEN_ORIENTATION_*)?

Zol*_*tán 56 android screen-orientation

我想找出一个装置的详细取向,优选的一个SCREEN_ORIENTATION_LANDSCAPE,SCREEN_ORIENTATION_PORTRAIT,SCREEN_ORIENTATION_REVERSE_LANDSCAPE,SCREEN_ORIENTATION_REVERSE_PORTRAITActivityInfo或等同物.

StackOverflow中的一些答案包括在内

getWindowManager().getDefaultDisplay().getRotation()
Run Code Online (Sandbox Code Playgroud)

但这并没有真正告诉我该设备是处于纵向还是横向模式,只是它是如何根据其自然位置转动的 - 而这反过来可以是风景或肖像.

getResources().getConfiguration().orientation
Run Code Online (Sandbox Code Playgroud)

返回以下三种:ORIENTATION_LANDSCAPE,ORIENTATION_PORTRAIT,ORIENTATION_SQUARE,然后并没有真正告诉我哪种方式手机处于开机状态(无论是上下颠倒或两侧的它的转向).

我知道我可以结合使用后者DisplayMetrics来找出设备的自然方向,但是真的没有更好的方法吗?

Zol*_*tán 89

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

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;
}
Run Code Online (Sandbox Code Playgroud)

注意:一些用户(Geltrude和holtaf在下面的评论中)指出,这种解决方案不适用于所有设备,因为自然方向的旋转方向未标准化.

  • 当您需要弄清楚从相机意图中捕获的图像旋转方向时,这非常棒... Android很糟糕. (8认同)
  • 如果您的活动具有固定的屏幕方向,则会失败.getRotation仅在未锁定屏幕方向时报告值. (3认同)
  • 很公平,但如果你锁定了屏幕方向,那么你很可能知道它是否被锁定为横向或纵向 - 所以你不需要这个代码就可以解决它. (2认同)

Nil*_*esh 21

简单的方法就是使用

getResources().getConfiguration().orientation
Run Code Online (Sandbox Code Playgroud)

1表示Potrait,2表示Landscape.

  • 该问题要求提供更详细的信息,即包括反向纵向和反向横向,这些功能不提供. (5认同)

Uma*_*han 15

public static int getScreenOrientation(Activity activity) {
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        int orientation = activity.getResources().getConfiguration().orientation;
        if (orientation == Configuration.ORIENTATION_PORTRAIT) {
          if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270) {
            return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
          } else {
            return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
          }
        }
        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
          if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {
            return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
          } else {
            return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
          }
        }
        return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
      }
Run Code Online (Sandbox Code Playgroud)


waq*_*lam 7

getResources().getConfiguration().orientation是了解当前使用方向的标准方法.但是,如果它不能满足您的需求,那么也许您可以使用传感器以角度计算它.阅读本文此内容


Bha*_*rma 7

我认为您的问题是您可以检测横向和纵向,但不能反转横向和反转版本,因为旧版本不支持它们.要检测你可以做的是你可以使用oreintation和rotation.我告诉你一个可能对你有用的想法.

试试这个我认为它可以解决你的问题.

            int orientation = getResources().getConfiguration().orientation;
            int rotation = getWindowManager().getDefaultDisplay().getRotation();
            int actual_orientation = -1;
            if (orientation == Configuration.ORIENTATION_LANDSCAPE
            &&  (rotation == Surface.ROTATION_0 
            ||  rotation == Surface.ROTATION_90)){
                orientation = Configuration.ORIENTATION_LANDSCAPE;
            } else if (orientation == Configuration.ORIENTATION_PORTRAIT
                  &&  (rotation == Surface.ROTATION_0 
                   ||  rotation == Surface.ROTATION_90)) {
                orientation = Configuration.ORIENTATION_PORTRAIT;
            } else if (orientation == Configuration.ORIENTATION_LANDSCAPE
                  &&  (rotation == Surface.ROTATION_180 
                   ||  rotation == Surface.ROTATION_270)){
                orientation = //any constant for reverse landscape orientation;
            } else {
                if (orientation == Configuration.ORIENTATION_PORTRAIT
                        &&  (rotation == Surface.ROTATION_180 
                         ||  rotation == Surface.ROTATION_270)){
                      orientation = //any constant for reverse portrait orientation;
                }
            }
Run Code Online (Sandbox Code Playgroud)