Skip to content

Commit

Permalink
Merge pull request #10 from minibugdev/drawable-selector
Browse files Browse the repository at this point in the history
Support selector drawable
  • Loading branch information
minibugdev authored Oct 19, 2017
2 parents 37865d1 + 0860b87 commit 4af87a6
Show file tree
Hide file tree
Showing 13 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class DemoActivity : AppCompatActivity() {

private fun drawBadge(number: Int, position: BadgePosition) {
DrawableBadge.Builder(applicationContext)
.drawableResId(R.mipmap.ic_launcher)
.drawableResId(R.drawable.selector_badge)
.badgeColor(R.color.badgeColor)
.badgeSize(R.dimen.badge_size)
.badgePosition(position)
Expand Down
Binary file added demo/src/main/res/drawable-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/src/main/res/drawable-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/src/main/res/drawable-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/src/main/res/drawable-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions demo/src/main/res/drawable/selector_badge.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_launcher_round" android:state_pressed="true" />
<item android:drawable="@drawable/ic_launcher_round" android:state_focused="true" />
<item android:drawable="@drawable/ic_launcher" />
</selector>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.support.annotation.ColorRes
import android.support.annotation.DimenRes
import android.support.annotation.DrawableRes
import android.support.v4.content.ContextCompat
import android.support.v4.content.res.ResourcesCompat
import android.text.TextPaint

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

fun drawableResId(@DrawableRes drawableRes: Int) = apply { this.bitmap = BitmapFactory.decodeResource(context.resources, drawableRes) }
fun drawableResId(@DrawableRes drawableRes: Int) = apply {
val res = context.resources
bitmap = BitmapFactory.decodeResource(res, drawableRes)

if (bitmap == null) {
val d = ResourcesCompat.getDrawable(res, drawableRes, null)?.current
if (d is BitmapDrawable) {
bitmap = d.bitmap
}
}
}

fun drawable(drawable: Drawable) = apply {
if (drawable is BitmapDrawable) {
Expand Down

0 comments on commit 4af87a6

Please sign in to comment.