实现对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();
多说一句:希望对您有所帮助!:)
本文介绍了如何在Android中实现对指定View的截图,并将其保存为PNG格式的图片文件。核心步骤包括创建Bitmap对象,使用Canvas进行绘制,以及将Bitmap压缩并写入文件。

被折叠的 条评论
为什么被折叠?



