How to Convert a Drawable to a Bitmap

How to convert a Drawable to a Bitmap?

This converts a BitmapDrawable to a Bitmap.

Drawable d = ImagesArrayList.get(0);  
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();

How to load an image from drawable and convert to a bitmap

The following method should work fine:

public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
drawable = (DrawableCompat.wrap(drawable)).mutate();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}

build.gradle (project-level)

dependencies {
classpath 'com.android.tools.build:gradle:2.2.0-alpha5'
}

build.gradle (app-level)

android {
compileSdkVersion 23
buildToolsVersion '23.0.3'
defaultConfig {
minSdkVersion 16
targetSdkVersion 23
vectorDrawables.useSupportLibrary = true
}
...
}
...

Or, you can use android-ktx in Kotlin too like this which works for any subclass of Drawable:

build.gradle

dependencies {
implementation 'androidx.core:core-ktx:1.0.0-alpha1'
}

Then use Drawable#toBitmap() like this:

val bitmap = AppCompatResources.getDrawable(requireContext(), drawableId).toBitmap() 

How to create Bitmap form Drawable object

You can convert your Drawable to Bitmap like this (for resource):

Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.drawable_source);

OR

If you've it stored in a variable, you can use this :

public static Bitmap drawableToBitmap (Drawable drawable) {
Bitmap bitmap = null;

if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}

if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}

Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}

More details

Getting Bitmap from vector drawable

Checked on API: 17, 21, 23

public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
drawable = (DrawableCompat.wrap(drawable)).mutate();
}

Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);

return bitmap;
}

UPDATE:

Project gradle:

dependencies {
classpath 'com.android.tools.build:gradle:2.2.0-alpha5'
}

Module gradle:

android {
compileSdkVersion 23
buildToolsVersion '23.0.3'
defaultConfig {
minSdkVersion 16
targetSdkVersion 23
vectorDrawables.useSupportLibrary = true
}
...
}
...

converting drawable resource image into bitmap

You probably mean Notification.Builder.setLargeIcon(Bitmap), right? :)

Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);
notBuilder.setLargeIcon(largeIcon);

This is a great method of converting resource images into Android Bitmaps.

How to convert a Drawable to a scaled Bitmap

With pskink's answer (Make Image view rounded (not the image)) to a similar question I came to the following solution, based on the saveLayer approach (with some minor modifications).

@Override
public void onDraw( Canvas canvas )
{
Paint paint1 = new Paint( Paint.ANTI_ALIAS_FLAG )
Paint paint2 = new Paint()
paint2.setXfermode( new PorterDuffXfermode( Mode.SRC_IN ) )

Drawable drawable = getDrawable()
RectF rectangle = new RectF()
rectangle.set( drawable.getBounds() )

getImageMatrix.mapRect( rectangle )
rectangle.offset( getPaddingLeft(), getPaddingTop() )

// Prevent radius being drawn out of canvas bounds
rectangle.intersect( new RectF( 0, 0, canvas.getWidth(), canvas.getHeight() ) )

int restore = canvas.saveLayer( rectangle, null, Canvas.ALL_SAVE_FLAG )
canvas.drawRoundRect( rectangle, radius.getValue(), radius.getValue(), paint1 )
canvas.saveLayer( rectangle, paint2, Canvas.ALL_SAVE_FLAG )
super.onDraw( canvas )
canvas.restoreToCount( restore )
}

The above code ignores object caching in class level and ignores NPE from getDrawable().

How to convert a Bitmap to Drawable in android?

Sounds like you want to use BitmapDrawable

From the documentation:

A Drawable that wraps a bitmap and can
be tiled, stretched, or aligned. You
can create a BitmapDrawable from a
file path, an input stream, through
XML inflation, or from a Bitmap
object.



Related Topics



Leave a reply



Submit