android——自定义截图(加水印、logo等)
/** * 获取指定Activity的截屏,保存到png文件 */ public static Bitmap takeScreenShot(Activity activity) { // View是你需要截图的View View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap b1 = view.getDrawingCache(); // 获取状态栏高度 // Rect frame = new Rect(); // activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); // int statusBarHeight = frame.top; int statusBarHeight = getStatusHeight(activity); // 获取屏幕长和高 int width = activity.getWindowManager().getDefaultDisplay().getWidth(); int height = activity.getWindowManager().getDefaultDisplay() .getHeight(); //二维码图片 Bitmap erweima = BitmapFactory.decodeResource(activity.getResources(), R.drawable.icon_erweima); erweima = Bitmap.createScaledBitmap(erweima, width, width / 5, true); // 去掉标题栏 // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455); Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); Bitmap bitmap = Bitmap.createBitmap(width, height - statusBarHeight + width / 5, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); Paint bgPaint = new Paint(); bgPaint.setColor(Color.parseColor("#1e4873")); Rect rect = new Rect(0, 0, width, height - statusBarHeight + width / 5); canvas.drawRect(rect, bgPaint); int h = 0; canvas.drawBitmap(b, 0, h, paint); h += height - statusBarHeight; canvas.drawBitmap(erweima, 0, h, paint); // savePic(bitmap, "/sdcard/screen_test.png"); return bitmap; }
获取状态栏高度:
不积跬步,无以至千里。