Android implements the method of saving view into bitmap _android

Source: Internet
Author: User

The example in this article describes how the Android implementation saves view to bitmap. Share to everyone for your reference, specific as follows:

1,

Public Bitmap Convertviewtobitmap (view view) {
    Bitmap Bitmap = Bitmap.createbitmap (View.getwidth (), View.getheight (),
        Bitmap.Config.ARGB_8888);
    Using bitmap to generate canvas
    Canvas Canvas = new Canvas (bitmap);
    Draw the contents of the view on the canvas
    view.draw (canvas);
  return bitmap;
}

2,

/** * Save View as a bitmap/private bitmap Saveviewbitmap (view view) {//Get current
  View Bitmap view.setdrawingcacheenabled (true);
  View.builddrawingcache (TRUE);
  Bitmap Bitmap = View.getdrawingcache (true);
  Bitmap bmp = Duplicatebitmap (BITMAP);
  if (bitmap!= null &&!bitmap.isrecycled ()) {bitmap.recycle (); bitmap = null;}
  Clear the cache view.setdrawingcacheenabled (false);
return BMP;
  public static Bitmap Duplicatebitmap (Bitmap bmpsrc) {if (null = = BMPSRC) {return null;}
  int bmpsrcwidth = Bmpsrc.getwidth ();
  int bmpsrcheight = Bmpsrc.getheight (); Bitmap bmpdest = Bitmap.createbitmap (Bmpsrcwidth, Bmpsrcheight, config.argb_8888);
  if (null!= bmpdest) {Canvas Canvas = new Canvas (bmpdest); final Rect Rect = new Rect (0, 0, bmpsrcwidth, bmpsrcheight); Canvas.drawbitmap (BMPSRC, rect, rect, null);
return bmpdest; }

For more information on Android-related content readers can view the site topics: "Android File Operation tips Summary", "Android programming development of the SD card operation Summary", "Android Development introduction and Advanced Course", "Android Resources Operating Skills summary", " Android View tips Summary and a summary of the use of Android controls

I hope this article will help you with the Android program.

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.