Android:将View的内容映射成Bitmap转图片导出

    前段时间在网上看到这么个例子是将view映射到一个bitmap中,稍加改进可以用于一些截图工具或者截图软件(QQ截图之类),例子写的不够完善,不过很有些学习的意义内容大致如下:

    在Android中自有获取view中的cache内容,然后将内容转换成bitmap,方法名是:getDrawingCache(),返回结果为Bitmap,但是刚开始使用的时候,得到的结果都是null,所以在一个论坛里查到了正确的使用方法.代码如下:


 
 
  1. contentLayout.setDrawingCacheEnabled(true);     
  2.        contentLayout.measure(     
  3.                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),     
  4.                 MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));     
  5.        contentLayout.layout(00, contentLayout.getMeasuredWidth(),     
  6.                 contentLayout.getMeasuredHeight());     
  7.    
  8.       contentLayout.buildDrawingCache();     
  9.        //会报Canvas: trying to use a recycled bitmap android.graphics.Bitmap
  10.        //Bitmap bitmap= contentLayout.getDrawingCache();
  11. //解决办法:

    //Copy the bitmap before you pass it to the static variable.

            Bitmap bitmap = contentLayout.getDrawingCache(true);

    //to

            Bitmap bitmap = Bitmap.createBitmap(contentLayout.getDrawingCache(true));


在使用的时候调用

Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());

就可以得到图片的bitmap了。

为了测试这个功能,作者使用了两种方式来创建LinerLayout中的内容,一种是在xml文件中就将view的内容添加了,只需在代码中添加对应ImageView中的图片就行了;另一种是动态添加LinerLayout中的View。

 

setview的代码:

 
 
  1. public void onCreate(Bundle savedInstanceState) {     
  2.     super.onCreate(savedInstanceState);     
  3.     setContentView(R.layout.set_view);     
  4.     contentLayout = (LinearLayout) findViewById(R.id.content);     
  5.     imgSource1 = (ImageView) findViewById(R.id.imgSource1);     
  6.     imgSource2 = (ImageView) findViewById(R.id.imgSource2);     
  7.     imgCache = (ImageView) findViewById(R.id.imgCache);     
  8.     
  9.    imgSource1.setImageResource(R.drawable.source1);     
  10.     imgSource2.setImageResource(R.drawable.source2);     
  11.         
  12.     contentLayout.setDrawingCacheEnabled(true);     
  13.     contentLayout.measure(     
  14.             MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),     
  15.             MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));     
  16.     contentLayout.layout(00, contentLayout.getMeasuredWidth(),     
  17.             contentLayout.getMeasuredHeight());     
  18.     
  19.     contentLayout.buildDrawingCache();     
  20.          
  21.     Bitmap bitmap= contentLayout.getDrawingCache();     
  22.     if(bitmap!=null){     
  23.         imgCache.setImageBitmap(bitmap);     
  24.     }else{     
  25.         Log.i("CACHE_BITMAP""DrawingCache=null");     
  26.     }     
  27. }    

第二种方法代码:

 

 
 
  1. private void addRelativeLayout() {     
  2.         // TODO Auto-generated method stub     
  3.         RelativeLayout.LayoutParams layoutpare = new RelativeLayout.LayoutParams(     
  4.                 LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);     
  5.    
  6.         RelativeLayout relativeLayout = new RelativeLayout(this);     
  7.         relativeLayout.setLayoutParams(layoutpare);     
  8.     
  9.         ImageView imgView1 = new ImageView(this);     
  10.         ImageView imgView2 = new ImageView(this);     
  11.         imgView1.setImageResource(R.drawable.source1);     
  12.         imgView2.setImageResource(R.drawable.source2);     
  13.         RelativeLayout.LayoutParams img1 = new RelativeLayout.LayoutParams(38,     
  14.                 38);     
  15.         img1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);     
  16.         RelativeLayout.LayoutParams img2 = new RelativeLayout.LayoutParams(38,     
  17.                 38);     
  18.         img2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);     
  19.     
  20.         relativeLayout.addView(imgView1, img1);     
  21.         relativeLayout.addView(imgView2, img2);     
  22.         addViewContent.addView(relativeLayout);     
  23.     }     
  24.     
  25.     /**    
  26.      * 添加图片源    
  27.      */    
  28.    private void addImgSource() {     
  29.         ImageView imgView1 = new ImageView(this);     
  30.         ImageView imgView2 = new ImageView(this);     
  31.         imgView1.setImageResource(R.drawable.source1);     
  32.         imgView2.setImageResource(R.drawable.source2);     
  33.         addViewContent.addView(imgView1, new LayoutParams(     
  34.                 LinearLayout.LayoutParams.WRAP_CONTENT,     
  35.                 LinearLayout.LayoutParams.WRAP_CONTENT));     
  36.         addViewContent.addView(imgView2, new LayoutParams(     
  37.                 LinearLayout.LayoutParams.WRAP_CONTENT,     
  38.                 LinearLayout.LayoutParams.WRAP_CONTENT));     
  39.     }    

 

 

文章参考自:http://www.iteye.com/topic/1097916

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值