Android--将布局保存成图像

来源:互联网 发布:iu脸型数据 编辑:程序博客网 时间:2024/06/05 07:31
  1. /将布局转换为View类型对象  
  2.         View view = getLayoutInflater().inflate(R.layout.main, null);  
  3.         //打开图像缓存  
  4.         view.setDrawingCacheEnabled(true);  
  5.         //必须调用measure和layout方法才能成功保存可视组件的截图到png图像文件  
  6.         //测量View大小  
  7.         view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),  
  8.                 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));  
  9.         //发送位置和尺寸到View及其所有的子View  
  10.         view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());  
  11.         try{  
  12.             //获得可视组件的截图  
  13.             Bitmap bitmap = view.getDrawingCache();  
  14.             //将截图保存在SD卡根目录的test.png图像文件中  
  15.             FileOutputStream fos = new FileOutputStream("/sdcard/test.png");  
  16.             //将Bitmap对象中的图像数据压缩成png格式的图像数据,并将这些数据保存在test.png文件中  
  17.             bitmap.compress(CompressFormat.PNG, 100, fos);  
  18.             //关闭文件输出流  
  19.             fos.close();  
  20.         }catch (Exception e) {  
  21.               
  22.         }  
0 0
原创粉丝点击