kaneda kaneda - 1 year ago 78
Android Question

How to get a Bitmap from a drawable defined in a xml?

How can I get the bitmap from a xml shape drawable.
What am I doing wrong?


<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

android:type="linear" />

<size android:height="7.0dip" />


My method to retrieve the bitmap from drawable:

private Bitmap getBitmap(int id) {
return BitmapFactory.decodeResource(getContext().getResources(), id);

getBitmap() is returning null when the id passed in is the shadow.xml drawable id.

Answer Source

a ShapeDrawable doesn't have a bitmap associated with it - its sole purpose is to be drawn on a canvas. Until its draw method is called, it has no image. If you can get a canvas element at the place where you need to draw the shadow, you can draw it as a shapeDrawable, otherwise you might need a separate, empty view in your layout with the shadow as a background.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download