Android获取常用辅助方法(获取屏幕高度、宽度、密度、通知栏高度、截图)
程序员文章站
2024-02-22 19:44:52
我们需要获取android手机或pad的屏幕的物理尺寸,以便于界面的设计或是其他功能的实现。下面就分享一下android中常用的一些辅助方法:
获取屏幕高度:...
我们需要获取android手机或pad的屏幕的物理尺寸,以便于界面的设计或是其他功能的实现。下面就分享一下android中常用的一些辅助方法:
获取屏幕高度:
/** * 获得屏幕高度 * @param context * @return * by hankkin at:2015-10-07 21:15:59 */ public static int getscreenwidth(context context) { windowmanager wm = (windowmanager) context .getsystemservice(context.window_service); displaymetrics outmetrics = new displaymetrics(); wm.getdefaultdisplay().getmetrics(outmetrics); return outmetrics.widthpixels; }
获取屏幕宽度:
/** * 获得屏幕宽度 * @param context * @return * by hankkin at:2015-10-07 21:16:13 */ public static int getscreenheight(context context) { windowmanager wm = (windowmanager) context .getsystemservice(context.window_service); displaymetrics outmetrics = new displaymetrics(); wm.getdefaultdisplay().getmetrics(outmetrics); return outmetrics.heightpixels; }
获取屏幕密度:
/** * 获取屏幕密度 * @param context * @return * by hankkin at:2015-10-07 21:16:29 */ public static float getscreendensity(context context) { return context.getresources().getdisplaymetrics().density; }
dip转px:
/** * dip转px像素 * @param context * @param px * @return * by hankkin at:2015-10-07 21:16:43 */ public static int dip2px(context context, float px) { final float scale = getscreendensity(context); return (int) (px * scale + 0.5); }
获取状态栏高度:
/** * 获得状态栏的高度 * @param context * @return * by hankkin at:2015-10-07 21:16:43 */ public static int getstatusheight(context context) { int statusheight = -1; try { class<?> clazz = class.forname("com.android.internal.r$dimen"); object object = clazz.newinstance(); int height = integer.parseint(clazz.getfield("status_bar_height") .get(object).tostring()); statusheight = context.getresources().getdimensionpixelsize(height); } catch (exception e) { e.printstacktrace(); } return statusheight; }
获取屏幕当前截图:
/** * 获取当前屏幕截图,包含状态栏 * @param activity * @return * by hankkin at:2015-10-07 21:16:43 */ public static bitmap snapshotwithstatusbar(activity activity) { view view = activity.getwindow().getdecorview(); view.setdrawingcacheenabled(true); view.builddrawingcache(); bitmap bmp = view.getdrawingcache(); int width = getscreenwidth(activity); int height = getscreenheight(activity); bitmap bp = null; bp = bitmap.createbitmap(bmp, 0, 0, width, height); view.destroydrawingcache(); return bp; } /** * 获取当前屏幕截图,不包含状态栏 * @param activity * @return * by hankkin at:2015-10-07 21:16:43 */ public static bitmap snapshotwithoutstatusbar(activity activity) { view view = activity.getwindow().getdecorview(); view.setdrawingcacheenabled(true); view.builddrawingcache(); bitmap bmp = view.getdrawingcache(); rect frame = new rect(); activity.getwindow().getdecorview().getwindowvisibledisplayframe(frame); int statusbarheight = frame.top; int width = getscreenwidth(activity); int height = getscreenheight(activity); bitmap bp = null; bp = bitmap.createbitmap(bmp, 0, statusbarheight, width, height - statusbarheight); view.destroydrawingcache(); return bp; }
以上所述是本文给大家介绍的android获取常用辅助方法(获取屏幕高度、宽度、密度、通知栏高度、截图),希望对大家也是帮助,更多信息登录网站了解更多信息。
上一篇: mybatis查询语句揭秘之封装数据