Skip to content

Commit 3817993

Browse files
committed
added support for VectorDrawable
1 parent 3c3ea52 commit 3817993

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

library/src/main/java/com/minibugdev/drawablebadge/DrawableBadge.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.content.Context
44
import android.graphics.*
55
import android.graphics.drawable.BitmapDrawable
66
import android.graphics.drawable.Drawable
7+
import android.graphics.drawable.VectorDrawable
78
import android.support.annotation.ColorInt
89
import android.support.annotation.ColorRes
910
import android.support.annotation.DimenRes
@@ -35,6 +36,14 @@ class DrawableBadge private constructor(val context: Context,
3536
private var isShowBorder: Boolean? = null
3637
private var maximumCounter: Int? = null
3738

39+
private fun createBitmapFromVectorDrawable(vectorDrawable: VectorDrawable): Bitmap {
40+
val bitmap = Bitmap.createBitmap(vectorDrawable.intrinsicWidth, vectorDrawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
41+
val canvas = Canvas(bitmap)
42+
vectorDrawable.setBounds(0, 0, canvas.width, canvas.height)
43+
vectorDrawable.draw(canvas)
44+
return bitmap
45+
}
46+
3847
fun drawableResId(@DrawableRes drawableRes: Int) = apply {
3948
val res = context.resources
4049
bitmap = BitmapFactory.decodeResource(res, drawableRes)
@@ -44,13 +53,19 @@ class DrawableBadge private constructor(val context: Context,
4453
if (d is BitmapDrawable) {
4554
bitmap = d.bitmap
4655
}
56+
if (d is VectorDrawable) {
57+
bitmap = createBitmapFromVectorDrawable(d)
58+
}
4759
}
4860
}
4961

5062
fun drawable(drawable: Drawable) = apply {
5163
if (drawable is BitmapDrawable) {
5264
this.bitmap = drawable.bitmap
5365
}
66+
if (drawable is VectorDrawable) {
67+
this.bitmap = createBitmapFromVectorDrawable(drawable)
68+
}
5469
}
5570

5671
fun bitmap(bitmap: Bitmap) = apply { this.bitmap = bitmap }

0 commit comments

Comments
 (0)