Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LayerDrawable to bitmap

I'm using a LayerDrawable to merge multiple Drawable. Now, I'd like to export my LayerDrawable to a file.

I've tried this way:

Bitmap b = ((BitmapDrawable)myLayerDrawable).getBitmap();
--> ClassCastException...

What can I do?

like image 841
Francois Avatar asked Nov 04 '10 21:11

Francois


2 Answers

Thank both users have answered before me (@Kyle P and @Anjum Shrimali). Inspired by their answers ... This worked for my case fine:

final int width = myLayerDrawable.getIntrinsicWidth();
final int height = myLayerDrawable.getIntrinsicHeight();

final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
myLayerDrawable.setBounds(0, 0, width, height);
myLayerDrawable.draw(new Canvas(bitmap));
like image 181
Mir-Ismaili Avatar answered Nov 06 '22 12:11

Mir-Ismaili


Have you tried drawing the Drawable to a Bitmap Canvas? I think the call order would go something like:

Bitmap b = Bitmap.createBitmap(int width, int height, Bitmap.Config config);
myLayerDrawable.draw(new Canvas(b));

Then you can write the Bitmap object to an output stream.

like image 15
Kyle P Avatar answered Nov 06 '22 13:11

Kyle P