Skip to content

Instantly share code, notes, and snippets.

@alexshr
Created February 12, 2019 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexshr/1ec36b3441d013f9bf91207d30bb0c67 to your computer and use it in GitHub Desktop.
Save alexshr/1ec36b3441d013f9bf91207d30bb0c67 to your computer and use it in GitHub Desktop.
drawable to bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
int width = drawable.getIntrinsicWidth();
width = width > 0 ? width : 1;
int height = drawable.getIntrinsicHeight();
height = height > 0 ? height : 1;
Bitmap bitmap = Bitmap.createBitmap(width, height, 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