android 获取屏幕尺寸

在内部类中使用:


//获取屏幕的宽度旧方法,已废弃
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
int screenWidth = wm.getDefaultDisplay().getWidth();
System.out.println("screenWidth:" + screenWidth);

//获取屏幕的宽高新方法1
Point size = new Point();
wm.getDefaultDisplay().getSize(size);
int screenWidth1 = size.x;
int screenHeight = size.y;
System.out.println("screenWidth1:" + screenWidth1);

//获取屏幕的宽高新方法2
DisplayMetrics displaymetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(displaymetrics);
int screenWidth2 = displaymetrics.widthPixels;
int screenHeight2 = displaymetrics.heightPixels;
System.out.println("screenWidth2:" + screenWidth2);

在activity里使用:


//方法1
DisplayMetrics dm = new
DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;

//方法2
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
int screenWidth = size.x;
int screenHeight = size.y;

理解Android Context 上下文 你必须知道的一切

你可能感兴趣的:(android 获取屏幕尺寸)