布局文件转bitmap


转 自: http://blog.csdn.net/ygc87/article/details/6801453

java代码中的处理,方法一:

[java]  view plain copy print ?
  1. //加载xml布局文件  
  2.         LayoutInflater factory = LayoutInflater.from(context);  
  3.         View view = factory.inflate(R.layout.main, null);  
  4.         //获得布局文件中的TextView  
  5.         TextView city = (TextView) view.findViewById(R.id.city);  
  6.         //设置city的文本信息  
  7.         city.setText("xml中的textview");    
  8.         //启用绘图缓存  
  9.         view.setDrawingCacheEnabled(true);        
  10.         //调用下面这个方法非常重要,如果没有调用这个方法,得到的bitmap为null  
  11.         view.measure(MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY),  
  12.                 MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY));  
  13.         //这个方法也非常重要,设置布局的尺寸和位置  
  14.         view.layout(00, view.getMeasuredWidth(), view.getMeasuredHeight());  
  15.         //获得绘图缓存中的Bitmap  
  16.         view.buildDrawingCache();  
  17.         Bitmap bitmap = view.getDrawingCache();  

 

java代码中的处理,方法二:

 

[java]  view plain copy print ?
  1. //加载xml布局文件  
  2.         LayoutInflater factory = LayoutInflater.from(context);  
  3.         View view = factory.inflate(R.layout.main, null);  
  4.         //获得布局文件中的TextView  
  5.         TextView city = (TextView) view.findViewById(R.id.city);  
  6.         //设置city的文本信息  
  7.         city.setText("xml中的textview");    
  8.         //调用下面这个方法非常重要,如果没有调用这个方法,得到的bitmap为null  
  9.         view.measure(MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY),  
  10.                 MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY));  
  11.         //这个方法也非常重要,设置布局的尺寸和位置  
  12.         view.layout(00, view.getMeasuredWidth(), view.getMeasuredHeight());  
  13.         //生成bitmap  
  14.         Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),  
  15.                 Bitmap.Config.RGB_565);  
  16.         //利用bitmap生成画布  
  17.         Canvas canvas = new Canvas(bitmap);  
  18.         //把view中的内容绘制在画布上  
  19.         view.draw(canvas);  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值