实现对View的截图操作,核心代码如下:
Bitmap bitmap = null; FileOutputStream fileOutputStream = null; try { bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(); canvas.setBitmap(bitmap); view.draw(canvas); fileOutputStream = new FileOutputStream(filePath); bitmap.compress(CompressFormat.PNG, 100, fileOutputStream); } catch (Exception e) { e.printStackTrace(); } finally { if (bitmap != null && !bitmap.isRecycled()) { bitmap.recycle(); bitmap = null; } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
例如:对整个窗口进行截图,可以使用下面代码获得窗口的View:
View windowView = (View) getWindow().getDecorView();
多说一句:希望对您有所帮助!:)