第一种:通过当前设备直接获取
public static int getScreenWidth(Context context) {
return context.getResources().getDisplayMetrics().widthPixels;
}
public static int getScreenHeight(Context context) {
return context.getResources().getDisplayMetrics().heightPixels;
}
public static float getScreenDensity(Context context) {
return context.getResources().getDisplayMetrics().density;
}
public static int dip2px(Context context, float px) {
final float scale = getScreenDensity(context);
return (int) (px * scale + 0.5);
}
第二种;框架ScreenUtils工具类
getScreenWidth : 获取屏幕的宽度(单位:px)
getScreenHeight : 获取屏幕的高度(单位:px)
setLandscape : 设置屏幕为横屏
setPortrait : 设置屏幕为竖屏
isLandscape : 判断是否横屏
isPortrait : 判断是否竖屏
getScreenRotation : 获取屏幕旋转角度
captureWithStatusBar : 获取当前屏幕截图,包含状态栏
captureWithoutStatusBar : 获取当前屏幕截图,不包含状态栏
isScreenLock : 判断是否锁屏
注:个人更倾向于使用框架,因为这个框架很多涉及很广泛,也很好用