import androID.app.Activity; import androID.content.Context; import androID.graphics.Bitmap; import androID.graphics.Rect; import androID.util.displayMetrics; import androID.vIEw.VIEw; import androID.vIEw.WindowManager; /** * 获得屏幕相关的辅助类 */ public class ScreenUtils { private ScreenUtils() { /* cannot be instantiated */ throw new UnsupportedOperationException("cannot be instantiated"); } /** * 获得屏幕高度 * * @param context * @return */ 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 */ 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 */ 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 */ 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 */ 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工具类篇 屏幕辅助类 ScreenUtils全部内容,希望文章能够帮你解决Android工具类篇 屏幕辅助类 ScreenUtils所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)