android之获取屏幕的宽度和高度

时间:2022-03-18 19:21:08

获取屏幕的宽度和高度:

方法一:

        //获取屏幕的宽度
public static int getScreenWidth(Context context) {
WindowManager manager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
return display.getWidth();
}
//获取屏幕的高度
public static int getScreenHeight(Context context) {
WindowManager manager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
return display.getHeight();
}

方法二:

DisplayMetrics  dm = new DisplayMetrics();    

getWindowManager().getDefaultDisplay().getMetrics(dm);    

int screenWidth = dm.widthPixels;              

int screenHeight = dm.heightPixels;