Android:將View的內容映射成Bitmap轉圖片匯出

來源:互聯網
上載者:User

 

前段時間在網上看到這麼個例子是將view映射到一個bitmap中,稍加改進可以用於一些工具或者軟體(QQ之類),例子寫的不夠完善,不過很有些學習的意義內容大致如下:

在Android中自有擷取view中的cache內容,然後將內容轉換成bitmap,方法名是:getDrawingCache(),返回結果為Bitmap,但是剛開始使用的時候,得到的結果都是null,所以在一個論壇裡查到了正確的使用方法.代碼如下:

contentLayout.setDrawingCacheEnabled(true);    

        contentLayout.measure(    

               MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),    

                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));    

       contentLayout.layout(0, 0, contentLayout.getMeasuredWidth(),    

                contentLayout.getMeasuredHeight());    

  

     contentLayout.buildDrawingCache();    

          

      Bitmap bitmap= contentLayout.getDrawingCache();   

 

在使用的時候調用

Bitmap bitmap = view.getDrawingCache();

就可以得到圖片的bitmap了。

為了測試這個功能,作者使用了兩種方式來建立LinerLayout中的內容,一種是在xml檔案中就將view的內容添加了,只需在代碼中添加對應ImageView中的圖片就行了;另一種是動態添加LinerLayout中的View。

 

setview的代碼:

public void onCreate(Bundle savedInstanceState) {    

    super.onCreate(savedInstanceState);    

    setContentView(R.layout.set_view);    

    contentLayout = (LinearLayout) findViewById(R.id.content);    

    imgSource1 = (ImageView) findViewById(R.id.imgSource1);    

    imgSource2 = (ImageView) findViewById(R.id.imgSource2);    

    imgCache = (ImageView) findViewById(R.id.imgCache);    

   

   imgSource1.setImageResource(R.drawable.source1);    

    imgSource2.setImageResource(R.drawable.source2);    

       

    contentLayout.setDrawingCacheEnabled(true);    

    contentLayout.measure(    

            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),    

            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));    

    contentLayout.layout(0, 0, contentLayout.getMeasuredWidth(),    

            contentLayout.getMeasuredHeight());    

   

    contentLayout.buildDrawingCache();    

        

    Bitmap bitmap= contentLayout.getDrawingCache();    

    if(bitmap!=null){    

        imgCache.setImageBitmap(bitmap);    

    }else{    

        Log.i("CACHE_BITMAP", "DrawingCache=null");    

    }    

}   

第二種方法代碼:

 

private void addRelativeLayout() {    

        // TODO Auto-generated method stub    

        RelativeLayout.LayoutParams layoutpare = new RelativeLayout.LayoutParams(    

                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);    

  

        RelativeLayout relativeLayout = new RelativeLayout(this);    

        relativeLayout.setLayoutParams(layoutpare);    

   

        ImageView imgView1 = new ImageView(this);    

        ImageView imgView2 = new ImageView(this);    

        imgView1.setImageResource(R.drawable.source1);    

        imgView2.setImageResource(R.drawable.source2);    

        RelativeLayout.LayoutParams img1 = new RelativeLayout.LayoutParams(38,    

                38);    

        img1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);    

        RelativeLayout.LayoutParams img2 = new RelativeLayout.LayoutParams(38,    

                38);    

        img2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);    

   

        relativeLayout.addView(imgView1, img1);    

        relativeLayout.addView(imgView2, img2);    

        addViewContent.addView(relativeLayout);    

    }    

   

    /**   

     * 添加圖片源   

     */   

   private void addImgSource() {    

        ImageView imgView1 = new ImageView(this);    

        ImageView imgView2 = new ImageView(this);    

        imgView1.setImageResource(R.drawable.source1);    

        imgView2.setImageResource(R.drawable.source2);    

        addViewContent.addView(imgView1, new LayoutParams(    

                LinearLayout.LayoutParams.WRAP_CONTENT,    

                LinearLayout.LayoutParams.WRAP_CONTENT));    

        addViewContent.addView(imgView2, new LayoutParams(    

                LinearLayout.LayoutParams.WRAP_CONTENT,    

                LinearLayout.LayoutParams.WRAP_CONTENT));    

    }     

本文出自 “HDDevTeam” 部落格

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.