/** * 獲取指定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; }
獲取狀態欄高度:
