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

This is a fully working solution:

private Bitmap getBitmap(int drawableRes) {
    Drawable drawable = getResources().getDrawable(drawableRes);
    Canvas canvas = new Canvas();
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    canvas.setBitmap(bitmap);
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    drawable.draw(canvas);

    return bitmap;
}

And here is an example:

Bitmap drawableBitmap = getBitmap(R.drawable.circle_shape);

circle_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <size
        android:width="15dp"
        android:height="15dp" />
    <solid
        android:color="#94f5b6" />
    <stroke
        android:width="2dp"
        android:color="#487b5a"/>
</shape>

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)