设为首页 加入收藏

TOP

Android--将布局保存成图像
2014-11-24 08:12:43 来源: 作者: 【 】 浏览:0
Tags:Android-- 布局 保存 图像

//将布局转换为View类型对象
View view = getLayoutInflater().inflate(R.layout.main, 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格式的图像数据,并将这些数据保存在test.png文件中
bitmap.compress(CompressFormat.PNG, 100, fos);
//关闭文件输出流
fos.close();
}catch (Exception e) {

}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android开发教程:使用include调.. 下一篇Android--Alarm,定时闹钟

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Linux_百度百科 (2025-12-26 12:51:52)
·Shell 流程控制 | 菜 (2025-12-26 12:51:49)
·TCP/UDP协议_百度百科 (2025-12-26 12:20:11)
·什么是TCP和UDP协议 (2025-12-26 12:20:09)
·TCP和UDP详解 (非常 (2025-12-26 12:20:06)