Android Utililty Methods Drawable to Bitmap Convert

List of utility methods to do Drawable to Bitmap Convert

Description

The list of methods to do Drawable to Bitmap Convert are organized into topic(s).

Method

BitmapdrawableToBitmap(Drawable drawable)
drawable To Bitmap
BitmapDrawable bd = (BitmapDrawable) drawable;
Bitmap bitmap = bd.getBitmap();
return bitmap;
BitmapdrawableToBitmap(Drawable drawable)
drawable To Bitmap
if (drawable == null) {
    return null;
if (drawable instanceof BitmapDrawable) {
    return ((BitmapDrawable) drawable).getBitmap();
int width = drawable.getIntrinsicWidth();
width = width > 0 ? width : 1;
...
BitmapdrawableToBitmap(Drawable drawable)
drawable To Bitmap
if (drawable instanceof BitmapDrawable) {
    return ((BitmapDrawable) drawable).getBitmap();
Canvas canvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(drawable.getMinimumWidth(),
        drawable.getMinimumHeight(), Config.ARGB_8888);
canvas.setBitmap(bitmap);
drawable.draw(canvas);
...
BitmapdrawableToBitmap(Drawable drawable)
drawable To Bitmap
if (drawable instanceof BitmapDrawable) {
    return ((BitmapDrawable) drawable).getBitmap();
final Bitmap bitmap = Bitmap.createBitmap(100, 100,
        Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
...
BitmapconvertToBitmap(final Drawable drawable)
convert To Bitmap
return ((BitmapDrawable) drawable).getBitmap();
BitmapdrawableToBitmap(Drawable drawable)
drawable To Bitmap
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
        : Bitmap.Config.RGB_565;
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
drawable.draw(canvas);
...
BitmapdrawableToBitmap(Drawable drawable)
drawable To Bitmap
Bitmap bitmap = Bitmap
        .createBitmap(
                drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(),
                drawable.getOpacity() != PixelFormat.OPAQUE ? Config.ARGB_8888
                        : Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
...
BitmapgetMutableBitmap(Drawable drawable)
get Mutable Bitmap
if (drawable.getIntrinsicHeight() > 0
        && drawable.getIntrinsicWidth() > 0) {
    Bitmap bitmap = Bitmap.createBitmap(
            drawable.getIntrinsicWidth(),
            drawable.getIntrinsicHeight(), Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
...