LayerDrawable for bitmap

I use LayerDrawable to combine multiple Drawable. Now I would like to export the LayerDrawable file to a file.

I tried this way:

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

What can I do?

+6
android drawable bitmap
source share
3 answers

Have you tried drawing Drawable on a Bitmap canvas? I think the order of the call would look something like this:

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

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

+12
source share

Thanks for the help. But beginners like me need some kind of specific code. I tried and worked for me as follows.

 Bitmap b = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); layerDrawable.setBounds(0, 0, getWidth(), getHeight()); layerDrawable.draw(new Canvas(b)); 

Ultimately, b (bitmap) is the desired combined bitmap.

+4
source share

Thank you for both people answering me (@Kyle and @Anjum). Inspired by their answers ... This worked for my case:

 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)); 
+1
source share

All Articles