package com.fredric.util; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.util.DisplayMetrics; import android.view.View; import android.view.WindowManager; /*- * 屏幕交互类封装 */ public class ScreenUtils { //获取屏幕宽度 public static int getScreenWidth(Context context){ WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); //DisplayMetrics提供关于现实的通用信息 DisplayMetrics display = new DisplayMetrics(); //将当前窗口信息放置入display wm.getDefaultDisplay().getMetrics(display); //返回绝对像素 return display.widthPixels; } //获取屏幕高度 public static int getScreenHeight(Context context){ WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics display = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(display); return display.heightPixels; } //截屏 public static Bitmap getSnapShot(Activity activity){ //decorView是window的最顶层view View view = activity.getWindow().getDecorView(); //view组件的内容通过cache机制保存至bitmap //setDrawingCacheEnabled 开启cache //getDrawingCache 获取view的cache图片 //buildDrawingCache 可不调用,若getDrawingCache调用时cache没有建立则调用 //destoryDrawingCache 销毁cache,setDrawingCacheEnabled(false)也可销毁cache //cache若要更新则必须先销毁旧cache view.setDrawingCacheEnabled(true); Bitmap bp = null; int width = getScreenWidth(activity); int height = getScreenHeight(activity); bp = Bitmap.createBitmap(view.getDrawingCache(), 0, 0, width, height); view.destroyDrawingCache(); return bp; } }
时间: 2024-10-08 22:00:03