Android对View进行截图并且保存

tech2024-01-10  78

View截图生成bitmp:

//方法1 public static Bitmap capture(View view, float width, float height, boolean scroll, Bitmap.Config config) { if (!view.isDrawingCacheEnabled()) { view.setDrawingCacheEnabled(true); } if(width==0){ width= ConstantsConfig.width; } if(height==0){ height= ConstantsConfig.height; } Bitmap bitmap = Bitmap.createBitmap((int) width, (int) height, config); bitmap.eraseColor(Color.WHITE); Canvas canvas = new Canvas(bitmap); int left = view.getLeft(); int top = view.getTop(); if (scroll) { left = view.getScrollX(); top = view.getScrollY(); } int status = canvas.save(); canvas.translate(-left, -top); float scale = width / view.getWidth(); canvas.scale(scale, scale, left, top); view.draw(canvas); canvas.restoreToCount(status); Paint alphaPaint = new Paint(); alphaPaint.setColor(Color.TRANSPARENT); canvas.drawRect(0f, 0f, 1f, height, alphaPaint); canvas.drawRect(width - 1f, 0f, width, height, alphaPaint); canvas.drawRect(0f, 0f, width, 1f, alphaPaint); canvas.drawRect(0f, height - 1f, width, height, alphaPaint); canvas.setBitmap(null); return bitmap; } //方法2 public static Bitmap createBitmap3(View v, int width, int height, Bitmap.Config config) { Bitmap bmp = Bitmap.createBitmap(v.getWidth(), v.getHeight(), config); Canvas c = new Canvas(bmp); c.drawColor(Color.WHITE); v.draw(c); return bmp; } //方法3 public static Bitmap createBitmap(View view) { view.buildDrawingCache(); Bitmap bitmap = view.getDrawingCache(); return bitmap; } //方法4 public static Bitmap createBitmap2(View v) { Bitmap bmp = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888); Canvas c = new Canvas(bmp); c.drawColor(Color.WHITE); v.draw(c); return bmp; }

保存图片到本地方法:

public static void saveBitmapPng(final Bitmap bmp, final String filepath) { new Thread() { @Override public void run() { saveBitmapPngInner(bmp,filepath); } }.start(); } private static void saveBitmapPngInner(Bitmap bmp, String filepath) { if(bmp==null){ return; } FileOutputStream out = null; try { out = new FileOutputStream(filepath); bmp.compress(Bitmap.CompressFormat.PNG, 80, out); // bmp is your Bitmap instance // PNG is a lossless format, the compression factor (100) is ignored } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } }

保存view截图后的bitmap到本地:

public String getScreenShot(View view) { int w = view.getWidth(); int h = view.getHeight(); String path = "/storage/emulated/0/pic_"+System.currentTimeMillis() + ".png"; if (w == 0 || h == 0) { return path; } else { Bitmap bitmap = null; if (bitmap == null) { bitmap = ScreenShotUtil.capture(view, Math.round(w / 3.0f), Math.round(h / 3.0f), true, Bitmap.Config.RGB_565); // bitmap = ScreenShotUtil.createBitmap3(view, Math.round(w / 4.0f), Math.round(h / 4.0f), Bitmap.Config.RGB_565); // bitmap = ScreenShotUtil.createBitmap2(view); // bitmap = ScreenShotUtil.createBitmap(view); ImageSaveUtil.saveBitmapPng(bitmap, path); } return path; } }
最新回复(0)