View view=LayoutInflater.from(context).inflate(R.layout.linerlayout, null);
//打开图像缓存
view.setDrawingCacheEnabled(true);
//必须调用measure和layout方法才能成功保存可视组件的截图到png图像文件
//测量view的大小
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
//发送位置和尺寸到view及其所有的子view
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
try {
//获取可视组件的截图
Bitmap bitmap=view.getDrawingCache();
//将截图保存到SD卡根目录的test.png图像文件中
FileOutputStream fos=new FileOutputStream("/sdcard/test.png");
//将bitmap对象中的图像数据压缩成png格式的图像数据
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
将布局存为图像
最新推荐文章于 2023-10-16 14:58:43 发布