The way to convert view or drawable to bitmap in Android app _android

Source: Internet
Author: User

View Convert to Bitmap

Android often encounters situations where a view is converted to bitmap, such as screenshots of the entire screen view and the creation of a picture; in Coverflow, you need to convert one page view to bitmap to achieve complex graphical effects (shadows, reflections, etc.) , such as some dynamic real-time view to facilitate the observation and recording of data, the need to temporarily generate static bitmap.

Implementation method:

1) The following is the author often used a conversion method

  public static Bitmap Convertviewtobitmap (view view, int bitmapwidth, int bitmapheight) {
    Bitmap Bitmap = bitmap.create Bitmap (Bitmapwidth, Bitmapheight, Bitmap.Config.ARGB_8888);
    View.draw (New Canvas (bitmap));
    
    return bitmap;
  }

Or use the following method:

public static Bitmap Convertviewtobitmap (view view) {
    view.builddrawingcache ();
Bitmap Bitmap = View.getdrawingcache ();
return bitmap; 
}

In general, this method works properly. Sometimes, however, there is a problem with generating bitmap (bitmap All Black). The main reason is that the value of Drawingcache is greater than the value given by the system. We can look at a piece of code in the Builddrawingcache () method:

if (width <= 0 | | height <= 0 | | (Width * Height * (opaque &&!translucentwindow 2:4) > Viewconfiguration.get (mcontext). Getscaledmaximumdraw Ingcachesize ()) {  
         destroydrawingcache ());  
         return;  
 } 

In the code above, width and height are the widths and heights of the view drawn by the cache, so (width * height * (opaque &&!translucentwindow 2:4) calculates the currently required CAC He size. Viewconfiguration.get (Mcontext). Getscaledmaximumdrawingcachesize () Gets the maximum Drawingcache value provided by the system. When the required Drawingcache > system provides the maximum Drawingcache value, the build bitmap problems occur and the bitmap obtained is null.

So you can solve the problem only if you need to modify the cache value. So we introduced the second method:

2 Perfect Solution

public static Bitmap Convertviewtobitmap (view view) {
  view.measure (0, measurespec.unspecified), Measurespec.makemeasurespec (0, measurespec.unspecified));
    View.layout (0, 0, view.getmeasuredwidth (), View.getmeasuredheight ());
    View.builddrawingcache ();
    Bitmap Bitmap = View.getdrawingcache ();

return bitmap;
}

View uses the Getmeasuredwidth () and Getmeasuredheight () methods to calculate the length of the width. At this point, bitmap can get it right.

Convert drawable to Bitmap

This is relatively less complex, look at the code directly:

/** * Bitmap converted to drawable * @param bitmap * @return * * public static drawable bitmap2drawable (bitmap b  
   ITMAP) {return new bitmapdrawable (bitmap); /** * drawable to Bitmap * @param drawable * @return * * public static bitmap Drawable2bit Map (drawable drawable) {if (drawable instanceof bitmapdrawable) {return (bitmapdrawable) drawable). Getbitma  
     P ();  
               }else if (drawable instanceof ninepatchdrawable) {Bitmap Bitmap = Bitmap. CreateBitmap ( Drawable.getintrinsicwidth (), Drawable.getintrinsicheight (), drawable.getopacity ()! = Pixelformat.opaque?  
       Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565);  
       Canvas Canvas = new Canvas (bitmap);  
       Drawable.setbounds (0, 0, drawable.getintrinsicwidth (), Drawable.getintrinsicheight ());  
       Drawable.draw (canvas);  
     return bitmap;  
 }else{      return null;  
 }  
  }

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.