Created
October 23, 2019 23:57
-
-
Save jesvs/b5c68103a0122fb71c2a66413b3ae9fb to your computer and use it in GitHub Desktop.
Get a Bitmap from a Drawable resource id
This file contains 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
fun getBitmapFromDrawable(resId: Int): Bitmap? { | |
val drawable = AppCompatResources.getDrawable(requireContext(), resId) ?: return null | |
val bitmap = Bitmap.createBitmap( | |
drawable.intrinsicWidth, | |
drawable.intrinsicHeight, | |
Bitmap.Config.ARGB_8888 | |
) | |
val canvas = Canvas(bitmap) | |
drawable.setBounds(0, 0, canvas.width, canvas.height) | |
drawable.draw(canvas) | |
return bitmap | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment