Skip to content

Commit fc58797

Browse files
authored
Merge pull request #28 from minibugdev/feature/show-number
Feature show counter number
2 parents 631c14c + 2f3ae63 commit fc58797

File tree

9 files changed

+49
-38
lines changed

9 files changed

+49
-38
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Drawable Badge
22
[![CircleCI](https://circleci.com/gh/minibugdev/DrawableBadge.svg?style=shield)](https://circleci.com/gh/minibugdev/DrawableBadge)
3-
[![Release](https://jitpack.io/v/minibugdev/DrawableBadge.svg)](https://jitpack.io/#minibugdev/DrawableBadge/2.0.0)
3+
[![Release](https://jitpack.io/v/minibugdev/DrawableBadge.svg)](https://jitpack.io/#minibugdev/DrawableBadge/2.0.1)
44
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/minibugdev/DrawableBadge/master/LICENSE)
55

66
Drawable Badge is a android library for adding badges to drawables.
@@ -21,6 +21,7 @@ val drawable = DrawableBadge.Builder(context)
2121
.badgeBorderColor(R.color.badgeBorderColor)
2222
.badgeBorderSize(R.dimen.badge_border_size)
2323
.maximumCounter(99)
24+
.showCounter(true)
2425
.build()
2526
.get(99)
2627
```
@@ -42,6 +43,7 @@ imageViewBadge.setImageDrawable(drawable)
4243
- `badgeBorderColor`: Badge border color resource id , default `#FFFFFF`.
4344
- `badgeBorderSize`: Badge border size supported `@DimenRes` or `@Px`, default `0.5dp`.
4445
- `maximumCounter`: Maximum counter text will append with `+`, default and not more than `99`.
46+
- `showCounter`: Set visible of badge counter number, default `true`.
4547

4648
#### Deprecated
4749
- `badgePosition`: Position of Badge which need to added.
@@ -54,7 +56,7 @@ repositories {
5456
```
5557

5658
``` groovy
57-
compile 'com.github.minibugdev:drawablebadge:2.0.0'
59+
compile 'com.github.minibugdev:drawablebadge:2.0.1'
5860
```
5961

6062

build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
buildscript {
2-
ext.kotlin_version = '1.3.61'
3-
ext.androidx_version = '1.1.0'
2+
ext.kotlin_version = '1.4.21'
3+
ext.androidx_version = '1.2.0'
44
ext.android_annotation_version = '1.1.0'
55

66
ext.test_runner_version = '1.2.0'
77
ext.junit_version = '4.12'
88
ext.mockk_version = '1.9.2'
9-
ext.robolectric_version = '4.3.1'
9+
ext.robolectric_version = '4.4'
1010

1111
repositories {
1212
jcenter()
1313
google()
1414
}
1515
dependencies {
16-
classpath 'com.android.tools.build:gradle:3.5.3'
16+
classpath 'com.android.tools.build:gradle:4.1.1'
1717
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1818
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
1919

demo/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ apply plugin: 'kotlin-android'
33
apply plugin: 'kotlin-android-extensions'
44

55
android {
6-
compileSdkVersion 29
7-
buildToolsVersion "29.0.2"
6+
compileSdkVersion 30
7+
buildToolsVersion "30.0.3"
88
defaultConfig {
99
applicationId "com.minibugdev"
1010
minSdkVersion 15
11-
targetSdkVersion 29
11+
targetSdkVersion 30
1212
versionCode 1
1313
versionName "1.0"
1414
}

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) }

gradle.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
# The setting is particularly useful for tweaking memory settings.
1212
android.enableJetifier=true
1313
android.useAndroidX=true
14-
android.enableUnitTestBinaryResources=true
1514
org.gradle.jvmargs=-Xmx1536m
1615

1716
# When configured, Gradle will run in incubating parallel mode.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Mon Jun 24 11:14:39 ICT 2019
1+
#Tue Dec 29 15:08:23 ICT 2020
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip

library/build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ apply plugin: 'com.github.dcendents.android-maven'
55
group = 'com.github.minibugdev'
66

77
android {
8-
compileSdkVersion 29
9-
buildToolsVersion "29.0.2"
8+
compileSdkVersion 30
9+
buildToolsVersion "30.0.3"
1010

1111
defaultConfig {
1212
minSdkVersion 14
13-
targetSdkVersion 29
14-
versionCode 5
15-
versionName "2.0.0"
13+
targetSdkVersion 30
14+
versionCode 6
15+
versionName "2.0.1"
1616
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1717
}
1818
buildTypes {

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
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class DrawableBadgeBuilderTest {
6868
.badgeGravity(Gravity.CENTER_VERTICAL or Gravity.END)
6969
.showBorder(false)
7070
.maximumCounter(50)
71+
.showCounter(false)
7172
.build()
7273

7374
val expectedBadgeColor = ContextCompat.getColor(context, android.R.color.holo_red_dark)
@@ -83,5 +84,6 @@ class DrawableBadgeBuilderTest {
8384
assertEquals(Gravity.CENTER_VERTICAL or Gravity.END, actual.badgeGravity)
8485
assertEquals(false, actual.isShowBorder)
8586
assertEquals(50, actual.maximumCounter)
87+
assertEquals(false, actual.isShowCounter)
8688
}
8789
}

0 commit comments

Comments
 (0)