Android获取屏幕的宽和高

Android获取屏幕的宽和高,以便于绘图或其他操作,经常用的的方法如下,

        DisplayMetrics dm = getResources().getDisplayMetrics();
        mScreenWidth = dm.widthPixels;
        mScreenHeight = dm.heightPixels;

 

也有人用下列方式获取屏幕的大小,方法不是太好,不过还是做个记录,

			Display display = getWindowManager().getDefaultDisplay();
			Point point = new Point();
			try {
				Method getRealSize = Display.class.getMethod("getRealSize", Point.class);
				getRealSize.setAccessible(true);
				getRealSize.invoke(display, point);
				iWidth = point.x;
				iHeight = point.y;
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}

 

你可能感兴趣的:(Android,Java)