How do I get the CURRENT orientation (ActivityInfo.SCREEN_ORIENTATION_*) of an Android device?

后端 未结 8 1933
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 03:05

I would like to find out the detailed orientation of a device, preferably one of SCREEN_ORIENTATION_LANDSCAPE, SCREEN_ORIENTATION_PORTRAIT, S

相关标签:
8条回答
  • 2020-11-27 03:17

    I ended up using Zoltán's answer above, which works great, except when I tried it on a tablet (a Samsung P6210 Galaxy Tab 7.0 Plus). In portrait mode, it returned SCREEN_ORIENTATION_REVERSE_PORTRAIT. So in the else statement (if natural orientation is landscape) I swapped the cases for ROTATION_90 and ROTATION_270, and everything seems to work fine. (I don't have enough reputation to post this as a comment to Zoltán's answer.)

    0 讨论(0)
  • 2020-11-27 03:17

    You could do it in a very simple way: get the screen widhtand height. screen width will be always higher when device is in landscape orientation.

    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();
        }
    
    0 讨论(0)
  • 2020-11-27 03:18

    Simple approach would be to use

    getResources().getConfiguration().orientation
    

    1 is for Potrait and 2 for Landscape.

    0 讨论(0)
  • 2020-11-27 03:18

    I think your problem is that you can detect landscape and portrait but not reverse landscape and reverse protrait as they are not supported in older versions. To detect what you can do is that you can use both oreintation and rotation. I am giving you an idea it may be useful for you.

    try this i think it may solve your problem.

                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;
                    }
                }
    
    0 讨论(0)
  • 2020-11-27 03:20

    getResources().getConfiguration().orientation is the standard way of knowing current orientation being used. However, if it doesn't fulfill your needs then perhaps you may use Sensors to calculate it in terms of angle. Read this and this

    0 讨论(0)
  • 2020-11-27 03:27
    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;
          }
    
    0 讨论(0)
提交回复
热议问题