Solution for converting view to bitmap and getdrawingcache = NULL in Android

Source: Internet
Author: User

1. Preface

In Android, views are often converted to bitmap. For example, screenshots are taken and images are generated for the entire screen view; coverflow needs to convert a page-by-page view to bitmap to achieve complex graphic effects (shadows, reflection effects, etc ); for example, some dynamic real-time views need to generate a static bitmap temporarily to facilitate observation and record of data.

2. Implementation Method

1) The following is a conversion method that I often use.

 
Public StaticBitmap convertviewtobitmap (view,IntBitmapwidth,IntBitmapheight) {Bitmap bitmap=Bitmap. createbitmap (bitmapwidth, bitmapheight, bitmap. config. argb_8888); view. Draw (NewCanvas (Bitmap ));ReturnBitmap ;}

Alternatively, use the following method:

Public StaticBitmap convertviewtobitmap (view) {view. builddrawingcache (); Bitmap bitmap=View. getdrawingcache ();ReturnBitmap ;}

This method works normally. However, sometimes bitmap generation may fail (bitmap is black ). The main reason is thatThe value of drawingcache is greater than the value specified by the system. Let's take a look.A section in the builddrawingcache () methodCode:

If(Width <= 0 | height <= 0 | (width * height * (opaque &&! Translucentwindow? 2: 4)>Viewconfiguration. Get (mcontext). getscaledmaximumdrawingcachesize () {destroydrawingcache ();Return;}

 In the code above, width and height are the width and height of the view to be cached, so (width * height * (opaque &&! Translucentwindow? 2: 4) The current cache size is calculated. Viewconfiguration. Get (mcontext). getscaledmaximumdrawingcachesize () obtains the maximum drawingcache value provided by the system. When the maximum drawingcache value provided by the required drawingcache> system is used, the bitmap is generated and the obtained bitmap is null.

Therefore, you only need to modify the required cache value to solve the problem. So we introduce the second method:

 

2) Perfect solution

Public StaticBitmap convertviewtobitmap (view) {view. Measure (measurespec. makemeasurespec (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 calculates the length and width using the "getmeasuredwidth ()" and "getmeasuredheight ()" method. In this case, bitmap can be obtained correctly.

 

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.