Skip to content

Instantly share code, notes, and snippets.

@alphamu
Last active January 13, 2020 15:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alphamu/6265084 to your computer and use it in GitHub Desktop.
Save alphamu/6265084 to your computer and use it in GitHub Desktop.
Code on how to get a bitmap from a Drawable and how to resize that bitmap and return a Drawable. This could prove to be useful if you want to manually resize a drawable or if you want to simply get a bitmap of a drawable. Note: If using Compound Drawables you can simply call `setBounds` on the drawable and set it using the `setCompoundDrawable` …
private Drawable resize(Resources r, Drawable image, int newSize) {
Bitmap b = ((BitmapDrawable)image).getBitmap();
Bitmap bitmapResized = Bitmap.createScaledBitmap(b, newSize, newSize, false);
BitmapDrawable drawableBmp = new BitmapDrawable(r, bitmapResized);
return drawableBmp;
}
@MrAlshahawy
Copy link

This will lead to ClassCastException on Android 8, 9 & 10.

please read comments of answer : stackoverflow.com/a/3036187/3947321

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment