Skip to content

Commit 08e34f8

Browse files
committed
Add feature Builder.showCounter(Boolean)
1 parent 0d97846 commit 08e34f8

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class DemoActivity : AppCompatActivity() {
4242
.badgeBorderSize(R.dimen.badge_border_size)
4343
.maximumCounter(99)
4444
.badgeMargin(10f)
45+
.showCounter(true)
4546
.build()
4647
.get(number)
4748
.let { drawable -> imageViewBadge.setImageDrawable(drawable) }

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

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class DrawableBadge private constructor(val context: Context,
2121
val badgeMargin: Float,
2222
val bitmap: Bitmap,
2323
val isShowBorder: Boolean,
24-
val maximumCounter: Int) {
24+
val maximumCounter: Int,
25+
val isShowCounter: Boolean,){
2526

2627
class Builder(private val context: Context) {
2728

@@ -35,6 +36,7 @@ class DrawableBadge private constructor(val context: Context,
3536
private var bitmap: Bitmap? = null
3637
private var isShowBorder: Boolean? = null
3738
private var maximumCounter: Int? = null
39+
private var isShowCounter: Boolean? = null
3840

3941
private fun createBitmapFromDrawable(drawable: Drawable): Bitmap {
4042
val bitmap = Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
@@ -100,6 +102,8 @@ class DrawableBadge private constructor(val context: Context,
100102

101103
fun maximumCounter(maximumCounter: Int) = apply { this.maximumCounter = maximumCounter }
102104

105+
fun showCounter(isShowCounter: Boolean) = apply { this.isShowCounter = isShowCounter }
106+
103107
fun build(): DrawableBadge {
104108
if (bitmap == null) throw IllegalArgumentException("Badge drawable/bitmap can not be null.")
105109
if (badgeSize == null) badgeSize(R.dimen.default_badge_size)
@@ -110,6 +114,7 @@ class DrawableBadge private constructor(val context: Context,
110114
if (badgeGravity == null) badgeGravity(Gravity.TOP or Gravity.END)
111115
if (isShowBorder == null) showBorder(true)
112116
if (maximumCounter == null) maximumCounter(MAXIMUM_COUNT)
117+
if (isShowCounter == null) showCounter(true)
113118

114119
return DrawableBadge(
115120
context = context,
@@ -122,7 +127,8 @@ class DrawableBadge private constructor(val context: Context,
122127
badgeGravity = badgeGravity!!,
123128
badgeMargin = badgeMargin ?: 0.0f,
124129
isShowBorder = isShowBorder!!,
125-
maximumCounter = maximumCounter!!)
130+
maximumCounter = maximumCounter!!,
131+
isShowCounter = isShowCounter!!)
126132
}
127133
}
128134

@@ -162,27 +168,28 @@ class DrawableBadge private constructor(val context: Context,
162168
canvas.drawOval(badgeRect, paintBorder)
163169
}
164170

165-
val textSize: Float
166-
val text: String
167-
val max = if (maximumCounter > MAXIMUM_COUNT) MAXIMUM_COUNT else maximumCounter
168-
if (counter > max) {
169-
textSize = badgeRect.height() * 0.45f
170-
text = "$max+"
171-
}
172-
else {
173-
textSize = badgeRect.height() * 0.55f
174-
text = counter.toString()
175-
}
171+
if(isShowCounter) {
172+
val textSize: Float
173+
val text: String
174+
val max = if (maximumCounter > MAXIMUM_COUNT) MAXIMUM_COUNT else maximumCounter
175+
if (counter > max) {
176+
textSize = badgeRect.height() * 0.45f
177+
text = "$max+"
178+
} else {
179+
textSize = badgeRect.height() * 0.55f
180+
text = counter.toString()
181+
}
176182

177-
val textPaint = TextPaint().apply {
178-
this.isAntiAlias = true
179-
this.color = textColor
180-
this.textSize = textSize
181-
}
183+
val textPaint = TextPaint().apply {
184+
this.isAntiAlias = true
185+
this.color = textColor
186+
this.textSize = textSize
187+
}
182188

183-
val x = badgeRect.centerX() - (textPaint.measureText(text) / 2f)
184-
val y = badgeRect.centerY() - (textPaint.ascent() + textPaint.descent()) * 0.5f
185-
canvas.drawText(text, x, y, textPaint)
189+
val x = badgeRect.centerX() - (textPaint.measureText(text) / 2f)
190+
val y = badgeRect.centerY() - (textPaint.ascent() + textPaint.descent()) * 0.5f
191+
canvas.drawText(text, x, y, textPaint)
192+
}
186193

187194
return BitmapDrawable(resources, output)
188195
}

0 commit comments

Comments
 (0)