57

我想找出设备的详细方向,最好是SCREEN_ORIENTATION_LANDSCAPE, SCREEN_ORIENTATION_PORTRAIT, SCREEN_ORIENTATION_REVERSE_LANDSCAPE, SCREEN_ORIENTATION_REVERSE_PORTRAITfromActivityInfo或等价物之一。

StackOverflow 上的一些答案包括

getWindowManager().getDefaultDisplay().getRotation()

但这并不能真正告诉我设备是处于纵向还是横向模式,只能告诉我它是如何参照其自然位置转动的——这反过来又可以是横向或纵向。

getResources().getConfiguration().orientation

返回以下三个之一:ORIENTATION_LANDSCAPE, ORIENTATION_PORTRAIT, ORIENTATION_SQUARE,然后它并不能真正告诉我手机的转动方向(无论是倒置还是转向哪一侧)。

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

4

8 回答 8

90

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

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

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

于 2012-04-30T11:45:49.467 回答
21

简单的方法是使用

getResources().getConfiguration().orientation

1 代表肖像,2 代表风景。

于 2014-05-01T20:35:13.767 回答
16
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;
      }
于 2013-03-25T11:32:01.010 回答
7

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

于 2012-04-30T09:10:42.340 回答
7

我认为您的问题是您可以检测横向和纵向,但不能检测反向横向和反向纵向,因为旧版本不支持它们。要检测您可以做什么,您可以同时使用定向和旋转。我给你一个想法,它可能对你有用。

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

            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;
                }
            }
于 2012-04-30T09:47:48.667 回答
2

我最终使用了上面 Zoltán 的答案,效果很好,除非我在平板电脑(三星 P6210 Galaxy Tab 7.0 Plus)上尝试过。在纵向模式下,它返回 SCREEN_ORIENTATION_REVERSE_PORTRAIT。因此,在 else 语句中(如果自然方向是横向),我将案例换成了 ROTATION_90 和 ROTATION_270,一切似乎都正常。(我没有足够的声誉将其发布为对 Zoltán 答案的评论。)

于 2014-01-17T17:45:29.740 回答
1

您可以通过一种非常简单的方式做到这一点:获取屏幕widhtheight. 当设备处于横向时,屏幕宽度总是会更高。

Display display = getWindowManager().getDefaultDisplay();
    int width = display.getWidth(); 
    int height = display.getHeight();
    Toast.makeText(getApplicationContext(), "" + width + "," + height,
            Toast.LENGTH_SHORT).show();
    if (width > height) {
        Toast.makeText(getApplicationContext(), "LandScape",
                Toast.LENGTH_SHORT).show();
    }
于 2014-09-13T10:29:10.703 回答
-1

这能解决你的问题吗?

public static int getscrOrientation(Activity act)
{
    Display getOrient = act.getWindowManager()
            .getDefaultDisplay();

    int orientation = getOrient.getOrientation();

    // Sometimes you may get undefined orientation Value is 0
    // simple logic solves the problem compare the screen
    // X,Y Co-ordinates and determine the Orientation in such cases
    if (orientation == Configuration.ORIENTATION_UNDEFINED) {

        Configuration config = act.getResources().getConfiguration();
        orientation = config.orientation;

        if (orientation == Configuration.ORIENTATION_UNDEFINED) {
            // if height and widht of screen are equal then
            // it is square orientation
            if (getOrient.getWidth() == getOrient.getHeight()) {
                orientation = Configuration.ORIENTATION_SQUARE;
            } else { // if widht is less than height than it is portrait
                if (getOrient.getWidth() < getOrient.getHeight()) {
                    orientation = Configuration.ORIENTATION_PORTRAIT;
                } else { // if it is not any of the above it will defineitly
                            // be landscape
                    orientation = Configuration.ORIENTATION_LANDSCAPE;
                }
            }
        }
    }
    return orientation; // return value 1 is portrait and 2 is Landscape
                        // Mode
}
于 2012-04-30T09:13:26.143 回答