Android: Map the contents of the view into a bitmap-image export

Source: Internet
Author: User

Some time ago on the internet to see this example is to map the view to a bitmap, a little improvement can be used in some tools or software (QQ, etc.), the example is not well-written, but some of the meaning of learning is as follows:

In Android, you get the cache content in the view, and then convert the content to bitmap, the method name is: Getdrawingcache (), and the result is bitmap, but when you first use it, the result is null. So in a forum to find the right way to use. The code is as follows:

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 ();

When used, call the

Bitmap Bitmap = View.getdrawingcache ();

You can get the bitmap of the picture.

To test this functionality, the author uses two ways to create the contents of the Linerlayout, one is to add the contents of the view in the XML file, just add the image in the corresponding ImageView in the code. The other is to dynamically add a view in the Linerlayout.

Setview's Code:

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");

}

}

The second method code:

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);

}

/**

* Add a picture source

*/

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));

}

Android: Map the contents of the view into a bitmap-image export

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.