Skip to content

Commit 37865d1

Browse files
authored
Merge pull request #9 from minibugdev/maximum-count
Add maximum counter config
2 parents 38a3fe0 + 077a420 commit 37865d1

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ val drawable = DrawableBadge.Builder(context)
1919
.showBorder(true)
2020
.badgeBorderColor(R.color.badgeBorderColor)
2121
.badgeBorderSize(R.dimen.badge_border_size)
22+
.maximumCounter(99)
2223
.build()
2324
.get(99)
2425
```
@@ -38,6 +39,7 @@ imageViewBadge.setImageDrawable(drawable)
3839
- `showBorder`: Set show/hide badge border, default `true`.
3940
- `badgeBorderColor`: Badge border color resource id , default `#FFFFFF`.
4041
- `badgeBorderSize`: Badge border size dimension id, default `0.5dp`.
42+
- `maximumCounter`: Maximum counter text will append with `+`, default and not more than `99`.
4143

4244
## Download
4345
``` groovy

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ class DemoActivity : AppCompatActivity() {
1515
}
1616

1717
private fun registerEvents() {
18-
buttonTopLeft.setOnClickListener { drawBadge(99, BadgePosition.TOP_LEFT) }
19-
buttonTopRight.setOnClickListener { drawBadge(99, BadgePosition.TOP_RIGHT) }
18+
buttonTopLeft.setOnClickListener { drawBadge(1, BadgePosition.TOP_LEFT) }
19+
buttonTopRight.setOnClickListener { drawBadge(50, BadgePosition.TOP_RIGHT) }
2020
buttonBottomLeft.setOnClickListener { drawBadge(99, BadgePosition.BOTTOM_LEFT) }
21-
buttonBottomRight.setOnClickListener { drawBadge(99, BadgePosition.BOTTOM_RIGHT) }
21+
buttonBottomRight.setOnClickListener { drawBadge(100, BadgePosition.BOTTOM_RIGHT) }
2222
buttonReset.setOnClickListener { drawBadge(0, BadgePosition.TOP_LEFT) }
2323
}
2424

@@ -32,6 +32,7 @@ class DemoActivity : AppCompatActivity() {
3232
.showBorder(true)
3333
.badgeBorderColor(R.color.borderColor)
3434
.badgeBorderSize(R.dimen.badge_border_size)
35+
.maximumCounter(99)
3536
.build()
3637
.get(number)
3738
.let { drawable -> imageViewBadge.setImageDrawable(drawable) }

library/src/androidTest/java/com/minibugdev/drawablebadge/DrawableBadgeBuilderTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class DrawableBadgeBuilderTest {
5050
assertEquals(EXPECTED_DEFAULT_BADGE_TEXT_COLOR_INT, actual.textColor)
5151
assertEquals(BadgePosition.TOP_RIGHT, actual.badgePosition)
5252
assertEquals(true, actual.isShowBorder)
53+
assertEquals(99, actual.maximumCounter)
5354
}
5455

5556
@Test
@@ -69,6 +70,7 @@ class DrawableBadgeBuilderTest {
6970
.textColor(FAKE_TEXT_COLOR_RES_ID)
7071
.badgePosition(BadgePosition.BOTTOM_LEFT)
7172
.showBorder(false)
73+
.maximumCounter(50)
7274
.build()
7375

7476
assertEquals(EXPECTED_CONFIG_BADGE_SIZE, actual.badgeSize)
@@ -78,6 +80,7 @@ class DrawableBadgeBuilderTest {
7880
assertEquals(EXPECTED_CONFIG_BADGE_TEXT_COLOR_INT, actual.textColor)
7981
assertEquals(BadgePosition.BOTTOM_LEFT, actual.badgePosition)
8082
assertEquals(false, actual.isShowBorder)
83+
assertEquals(50, actual.maximumCounter)
8184
}
8285

8386
companion object {

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class DrawableBadge private constructor(val context: Context,
1919
val badgeSize: Float,
2020
val badgePosition: BadgePosition,
2121
val bitmap: Bitmap,
22-
val isShowBorder: Boolean) {
22+
val isShowBorder: Boolean,
23+
val maximumCounter: Int) {
2324

2425
class Builder(private val context: Context) {
2526

@@ -31,6 +32,7 @@ class DrawableBadge private constructor(val context: Context,
3132
private var badgePosition: BadgePosition? = null
3233
private var bitmap: Bitmap? = null
3334
private var isShowBorder: Boolean? = null
35+
private var maximumCounter: Int? = null
3436

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

@@ -56,6 +58,8 @@ class DrawableBadge private constructor(val context: Context,
5658

5759
fun showBorder(isShowBorder: Boolean) = apply { this.isShowBorder = isShowBorder }
5860

61+
fun maximumCounter(maximumCounter: Int) = apply { this.maximumCounter = maximumCounter }
62+
5963
fun build(): DrawableBadge {
6064
if (bitmap == null) throw IllegalArgumentException("Badge drawable/bitmap can not be null.")
6165
if (badgeSize == null) badgeSize(R.dimen.default_badge_size)
@@ -65,6 +69,7 @@ class DrawableBadge private constructor(val context: Context,
6569
if (badgeBorderSize == null) badgeBorderSize(R.dimen.default_badge_border_size)
6670
if (badgePosition == null) badgePosition(BadgePosition.TOP_RIGHT)
6771
if (isShowBorder == null) showBorder(true)
72+
if (maximumCounter == null) maximumCounter(DrawableBadge.MAXIMUM_COUNT)
6873

6974
return DrawableBadge(
7075
context = context,
@@ -75,13 +80,14 @@ class DrawableBadge private constructor(val context: Context,
7580
badgeBorderSize = badgeBorderSize!!,
7681
badgeSize = badgeSize!!,
7782
badgePosition = badgePosition!!,
78-
isShowBorder = isShowBorder!!)
83+
isShowBorder = isShowBorder!!,
84+
maximumCounter = maximumCounter!!)
7985
}
8086
}
8187

82-
fun get(number: Int): Drawable {
88+
fun get(counter: Int): Drawable {
8389
val resources = context.resources
84-
if (number == 0) return BitmapDrawable(resources, bitmap)
90+
if (counter == 0) return BitmapDrawable(resources, bitmap)
8591

8692
val sourceBitmap = bitmap
8793
val width = sourceBitmap.width
@@ -123,18 +129,32 @@ class DrawableBadge private constructor(val context: Context,
123129
canvas.drawOval(badgeRect, paintBorder)
124130
}
125131

126-
val textSize = badgeRect.height() * 0.55f
132+
val textSize: Float
133+
val text: String
134+
val max = if (maximumCounter > MAXIMUM_COUNT) MAXIMUM_COUNT else maximumCounter
135+
if (counter > max) {
136+
textSize = badgeRect.height() * 0.45f
137+
text = "$max+"
138+
}
139+
else {
140+
textSize = badgeRect.height() * 0.55f
141+
text = counter.toString()
142+
}
143+
127144
val textPaint = TextPaint().apply {
128145
this.isAntiAlias = true
129146
this.color = textColor
130147
this.textSize = textSize
131148
}
132149

133-
val text = number.toString()
134150
val x = badgeRect.centerX() - (textPaint.measureText(text) / 2f)
135151
val y = badgeRect.centerY() - (textPaint.ascent() + textPaint.descent()) * 0.5f
136152
canvas.drawText(text, x, y, textPaint)
137153

138154
return BitmapDrawable(resources, output)
139155
}
156+
157+
companion object {
158+
const val MAXIMUM_COUNT = 99
159+
}
140160
}

screenshot.png

3.03 KB
Loading

0 commit comments

Comments
 (0)