Method for converting View or Drawable to Bitmap in Android App development

  • 2021-06-28 13:52:30
  • OfStack

View to Bitmap

Android often encounters situations where View is converted to Bitmap, such as taking a screen shot of the entire screen view and generating a picture;view with one page and one page needs to be converted to Bitmap in order to achieve complex graphic effects (shadows, shadow effects, etc.).Another example is dynamic, real-time Views, which temporarily generate static Bitmaps for easy observation and recording of data.

Implementation method:

1) Here is a common conversion method used by the author


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

In general, this method works well.Sometimes, however, there is a problem generating Bitmap (Bitmap is all black).The main reason is that the value of drawingCache is larger than the value given by the system.Let's look at a piece of code from 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 drawing of the desired cache, so (width * height * (opaque) & & !translucentWindow ?2: 4) The current required cache size is calculated.ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize() yields the maximum DrawingCache value provided by the system.When drawingCache is needed > When the maximum DrawingCache value provided by the system is generated, there will be a problem, at which point the Bitmap obtained is null.

So you can solve the problem by simply modifying the required cache value.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();
    Bitmap bitmap = view.getDrawingCache();

 *  return bitmap;
}

view uses the "getMeasuredWidth()" and "getMeasuredHeight()" method to calculate the length and width.At this point, Bitmap can be obtained correctly.

Convert Drawable to Bitmap

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


/** 
   * Bitmap translate into drawable 
   * @param bitmap 
   * @return 
   */  
   public static Drawable bitmap2Drawable(Bitmap bitmap){  
     return new BitmapDrawable(bitmap) ;  
   }  
     
   /** 
   * Drawable  turn  bitmap 
   * @param drawable 
   * @return 
   */  
   public static Bitmap drawable2Bitmap(Drawable drawable){  
     if(drawable instanceof BitmapDrawable){  
       return ((BitmapDrawable)drawable).getBitmap() ;  
     }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 ;  
     }  
  }  


Related articles: