How to get a Bitmap from VectorDrawable
First of all, you cannot cast VectorDrawable
to BitmapDrawable
. They don't have a parent-child relationship. They both are direct subclasses of Drawable
class.
Now, to get a bitmap from drawable, you will need to create a Bitmap
from the drawable metadata.
Probably something like this in a separate method,
try {
Bitmap 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;
} catch (OutOfMemoryError e) {
// Handle the error
return null;
}
I hope this helps.
Use Drawable.toBitmap()
from AndroidX support library. VectorDrawable
is child of Drawable
.