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 frequently used conversion method: public static Bitmap convertViewToBitmap (View view, int bitmapWidth, int bitmapHeight) {Bitmap bitmap = Bitmap. createBitmap (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;} This Methods can work normally. However, sometimes Bitmap generation may fail (Bitmap is black ). The main reason is that the value of drawingCache is greater than the value specified by the system. Let's take a look at a piece of code in the buildDrawingCache () method: 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 introduced the second method: 2) Perfect solution public static Bitmap convertViewToBitmap (View view) {view. measure (MeasureSpec. makeMeasureSpec (0, MeasureSpec. UNSPECIFIED), MeasureSpec. makeMeasureSpec (0, MeasureSpec. UNSPECIFIED); view. layout (0, 0, view. getMeasuredWidth (), view. getMeasuredHeight (); view. buildDrawingCache (); www.2cto.com Bitmap bitmap = view. getDrawingCache (); return bitmap;} view use "getMeasuredWidth ()" and The "getMeasuredHeight ()" method calculates the length and width. In this case, Bitmap can be obtained correctly.