@@ -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}
0 commit comments