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

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

提交回复
热议问题