Android converts a view to bitmap + to handle the black edge of the view with rounded corners

Recently, the company has been working on a poster production function, which is to write a layout and plug in various data locally, and then save the whole layout as an image to the phone, so you need to turn a view into a bitmap.
Convert a View into a Bitmap

public static Bitmap createBitmapFromView(View view) {
      //If the ImageView is getting directly
        if (view instanceof ImageView) {
            Drawable drawable = ((ImageView) view).getDrawable();
            if (drawable instanceof BitmapDrawable) {
                return ((BitmapDrawable) drawable).getBitmap();
            }
        }
        view.clearFocus();
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        if (bitmap != null) {
            Canvas canvas = new Canvas(bitmap);
            view.draw(canvas);
            canvas.setBitmap(null);
        }
        return bitmap;
}

The above method can be used to turn a view into a bitmap, which can meet the needs of most people
However, in my actual development, the layout had rounded corners, and all the pictures saved by this method were filled with black. After trying many methods, I finally solved my problem by drawing a background color with canvas. The code is as follows.
Handle the black edge of the rounded View

  public static Bitmap createBitmapFromView(View view) {
        if (view instanceof ImageView) {
            Drawable drawable = ((ImageView) view).getDrawable();
            if (drawable instanceof BitmapDrawable) {
                return ((BitmapDrawable) drawable).getBitmap();
            }
        }
        view.clearFocus();
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        if (bitmap != null) {
            Canvas canvas = new Canvas(bitmap);
            canvas.drawColor(Color.WHITE);//Here you can change to other colors
            view.draw(canvas);
        }
        return bitmap;
    }


Read More: