Skip to content

Commit 60d577a

Browse files
committed
Support more drawable type
1 parent e630c34 commit 60d577a

File tree

1 file changed

+15
-33
lines changed

1 file changed

+15
-33
lines changed

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

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ 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.InsetDrawable
8-
import android.graphics.drawable.VectorDrawable
97
import android.support.annotation.ColorInt
108
import android.support.annotation.ColorRes
119
import android.support.annotation.DimenRes
1210
import android.support.annotation.DrawableRes
1311
import android.support.v4.content.ContextCompat
1412
import android.support.v4.content.res.ResourcesCompat
13+
import android.support.v4.graphics.drawable.DrawableCompat
1514
import android.text.TextPaint
1615

1716
class DrawableBadge private constructor(val context: Context,
@@ -37,19 +36,11 @@ class DrawableBadge private constructor(val context: Context,
3736
private var isShowBorder: Boolean? = null
3837
private var maximumCounter: Int? = null
3938

40-
private fun createBitmapFromVectorDrawable(vectorDrawable: VectorDrawable): Bitmap {
41-
val bitmap = Bitmap.createBitmap(vectorDrawable.intrinsicWidth, vectorDrawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
39+
private fun createBitmapFromDrawable(drawable: Drawable): Bitmap {
40+
val bitmap = Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
4241
val canvas = Canvas(bitmap)
43-
vectorDrawable.setBounds(0, 0, canvas.width, canvas.height)
44-
vectorDrawable.draw(canvas)
45-
return bitmap
46-
}
47-
48-
private fun createBitmapFromInsetDrawable(insetDrawable: InsetDrawable): Bitmap {
49-
val bitmap = Bitmap.createBitmap(insetDrawable.intrinsicWidth, insetDrawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
50-
val canvas = Canvas(bitmap)
51-
insetDrawable.setBounds(0, 0, canvas.width, canvas.height)
52-
insetDrawable.draw(canvas)
42+
drawable.setBounds(0, 0, canvas.width, canvas.height)
43+
drawable.draw(canvas)
5344
return bitmap
5445
}
5546

@@ -58,28 +49,19 @@ class DrawableBadge private constructor(val context: Context,
5849
bitmap = BitmapFactory.decodeResource(res, drawableRes)
5950

6051
if (bitmap == null) {
61-
val d = ResourcesCompat.getDrawable(res, drawableRes, null)?.current
62-
if (d is BitmapDrawable) {
63-
bitmap = d.bitmap
64-
}
65-
if (d is VectorDrawable) {
66-
bitmap = createBitmapFromVectorDrawable(d)
67-
}
68-
if (d is InsetDrawable) {
69-
bitmap = createBitmapFromInsetDrawable(d)
70-
}
52+
ResourcesCompat.getDrawable(res, drawableRes, null)
53+
?.current
54+
?.let {
55+
drawable(it)
56+
}
7157
}
7258
}
7359

74-
fun drawable(drawable: Drawable) = apply {
75-
if (drawable is BitmapDrawable) {
76-
this.bitmap = drawable.bitmap
77-
}
78-
if (drawable is VectorDrawable) {
79-
this.bitmap = createBitmapFromVectorDrawable(drawable)
80-
}
81-
if (drawable is InsetDrawable) {
82-
this.bitmap = createBitmapFromInsetDrawable(drawable)
60+
fun drawable(drawable: Drawable): Builder = apply {
61+
val drawableCompat = DrawableCompat.wrap(drawable)
62+
bitmap = when (drawableCompat) {
63+
is BitmapDrawable -> drawableCompat.bitmap
64+
else -> createBitmapFromDrawable(drawableCompat)
8365
}
8466
}
8567

0 commit comments

Comments
 (0)