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

后端 未结 8 2025
隐瞒了意图╮
隐瞒了意图╮ 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: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;
          }
    

提交回复
热议问题