Skip to content

Instantly share code, notes, and snippets.

@tolmachevroman
Last active November 26, 2017 05:13
Show Gist options
  • Save tolmachevroman/7520d5f56e5db69047899e8f9aa524a7 to your computer and use it in GitHub Desktop.
Save tolmachevroman/7520d5f56e5db69047899e8f9aa524a7 to your computer and use it in GitHub Desktop.
Medium Post 2. Utils
@Singleton
class Utils @Inject constructor(private val context: Context) {
fun getBitmap(drawableId: Int): Bitmap {
val drawable = ContextCompat.getDrawable(context, drawableId)
return when (drawable) {
is BitmapDrawable -> BitmapFactory.decodeResource(context.resources, drawableId)
is VectorDrawable -> getBitmap(drawable)
else -> throw IllegalArgumentException("unsupported drawable type")
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private fun getBitmap(vectorDrawable: VectorDrawable): Bitmap {
val bitmap = Bitmap.createBitmap(vectorDrawable.intrinsicWidth,
vectorDrawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
vectorDrawable.setBounds(0, 0, canvas.width, canvas.height)
vectorDrawable.draw(canvas)
return bitmap
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment