标签:
在app编码中经常需要获取手机的屏幕分辨率(宽*高),原来我直接上网拷贝代码,但在使用过程中却发现诸多不便。不便一:下面代码中的getWidth和getHeight在adt上提示deprecated已经废弃了,实在扎眼
WindowManager wm = getWindowManager(); Display display = wm.getDefaultDisplay(); int screenWidth = display.getWidth(); int screenHeight = display.getHeight();
public static Point getSize(Activity act) { Display display = act.getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); //size.x就是宽度,size.y就是高度 return size; }
public static Point getSize(Context ctx) { WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); Point size = new Point(); display.getSize(size); return size; }
public static Point getSizeNew(Context ctx) { WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics dm = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(dm); Point size = new Point(); size.x = dm.widthPixels*dm.density; size.y = dm.heightPixels*dm.density; return size; }
public static Point getSizeNew(Context ctx) { WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics dm = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(dm); Point size = new Point(); size.x = dm.widthPixels; size.y = dm.heightPixels; return size; }
标签:
原文地址:http://blog.csdn.net/qq_32059827/article/details/52123873