Created
February 12, 2019 01:05
-
-
Save alexshr/1ec36b3441d013f9bf91207d30bb0c67 to your computer and use it in GitHub Desktop.
drawable to bitmap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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