获取屏幕分辨率
程序员文章站
2022-07-03 21:57:31
...
第一种 不含状态栏
public int getWindowWidth(){
WindowManager wm = (WindowManager) (this.getSystemService(Context.WINDOW_SERVICE));
DisplayMetrics dm = new DisplayMetrics();
assert wm != null;
wm.getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
}
第二种,实际分辨率
public int getWindowWidth2(){
DisplayMetrics metrics =new DisplayMetrics();
getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
System.out.println("===屏幕宽width = "+width);
System.out.println("===屏幕宽height = "+height);
return width;
}