Skip to content

Commit 4af87a6

Browse files
authored
Merge pull request #10 from minibugdev/drawable-selector
Support selector drawable
2 parents 37865d1 + 0860b87 commit 4af87a6

File tree

13 files changed

+19
-2
lines changed

13 files changed

+19
-2
lines changed

demo/src/main/java/com/minibugdev/drawablebadge/demo/DemoActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class DemoActivity : AppCompatActivity() {
2424

2525
private fun drawBadge(number: Int, position: BadgePosition) {
2626
DrawableBadge.Builder(applicationContext)
27-
.drawableResId(R.mipmap.ic_launcher)
27+
.drawableResId(R.drawable.selector_badge)
2828
.badgeColor(R.color.badgeColor)
2929
.badgeSize(R.dimen.badge_size)
3030
.badgePosition(position)
3.34 KB
Loading
Loading
2.15 KB
Loading
Loading
4.73 KB
Loading
Loading
Loading
Loading
Loading
Loading
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item android:drawable="@drawable/ic_launcher_round" android:state_pressed="true" />
4+
<item android:drawable="@drawable/ic_launcher_round" android:state_focused="true" />
5+
<item android:drawable="@drawable/ic_launcher" />
6+
</selector>

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import android.support.annotation.ColorRes
99
import android.support.annotation.DimenRes
1010
import android.support.annotation.DrawableRes
1111
import android.support.v4.content.ContextCompat
12+
import android.support.v4.content.res.ResourcesCompat
1213
import android.text.TextPaint
1314

1415
class DrawableBadge private constructor(val context: Context,
@@ -34,7 +35,17 @@ class DrawableBadge private constructor(val context: Context,
3435
private var isShowBorder: Boolean? = null
3536
private var maximumCounter: Int? = null
3637

37-
fun drawableResId(@DrawableRes drawableRes: Int) = apply { this.bitmap = BitmapFactory.decodeResource(context.resources, drawableRes) }
38+
fun drawableResId(@DrawableRes drawableRes: Int) = apply {
39+
val res = context.resources
40+
bitmap = BitmapFactory.decodeResource(res, drawableRes)
41+
42+
if (bitmap == null) {
43+
val d = ResourcesCompat.getDrawable(res, drawableRes, null)?.current
44+
if (d is BitmapDrawable) {
45+
bitmap = d.bitmap
46+
}
47+
}
48+
}
3849

3950
fun drawable(drawable: Drawable) = apply {
4051
if (drawable is BitmapDrawable) {

0 commit comments

Comments
 (0)