Skip to content

Instantly share code, notes, and snippets.

@chronick
Last active August 29, 2015 14:08
Show Gist options
  • Save chronick/4afeab0cfa1012b16be4 to your computer and use it in GitHub Desktop.
Save chronick/4afeab0cfa1012b16be4 to your computer and use it in GitHub Desktop.
Drawable to Bitmap for Android
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment