Android获取屏幕的宽和高
程序员文章站
2022-07-14 17:33:57
...
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();
}