Last active
November 26, 2017 05:13
-
-
Save tolmachevroman/7520d5f56e5db69047899e8f9aa524a7 to your computer and use it in GitHub Desktop.
Medium Post 2. Utils
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
@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