如何获取当前屏幕状态?左横屏 右横屏 竖屏

以前总是根据context.getResources().getConfiguration().orientation== Configuration.ORIENTATION_PORTRAIT 或者等于 Configuration.ORIENTATION_LANDSCAPE 来获取,但是这样只能获取是横屏或者竖屏,无法确切的知道是左横屏还是右横屏。下面的办法可以确切的知道到底屏幕的状态是什

public class Snippet { /** * * 获取当前屏幕旋转角度 * * @param activity * @return 0表示是竖屏; 90表示是左横屏; 180表示是反向竖屏; 270表示是右横屏 */ public static int getDisplayRotation(Activity activity) { if (activity == null) return 0; int rotation = activity.getWindowManager().getDefaultDisplay() .getRotation(); switch (rotation) { case Surface.ROTATION_0: return 0; case Surface.ROTATION_90: return 90; case Surface.ROTATION_180: return 180; case Surface.ROTATION_270: return 270; } return 0; } }

以前总是根据context.getResources().getConfiguration().orientation== Configuration.ORIENTATION_PORTRAIT 或者等于 Configuration.ORIENTATION_LANDSCAPE 来获取,但是这样只能获取是横屏或者竖屏,无法确切的知道是左横屏还是右横屏。下面的办法可以确切的知道到底屏幕的状态是什 public class Snippet { /** * * 获取当前屏幕旋转角度 * * @param activity * @return 0表示是竖屏; 90表示是左横屏; 180表示是反向竖屏; 270表示是右横屏 */ public static int getDisplayRotation(Activity activity) { if (activity == null) return 0; int rotation = activity.getWindowManager().getDefaultDisplay() .getRotation(); switch (rotation) { case Surface.ROTATION_0: return 0; case Surface.ROTATION_90: return 90; case Surface.ROTATION_180: return 180; case Surface.ROTATION_270: return 270; } return 0; } }
经验分享 程序员 微信小程序 职场和发展