Skip to content

Commit 8cee56b

Browse files
authored
Apply badge color mask only for default color (#35)
* Reuse super state instead of instantiate a new one during the saving instance * Apply badge color mask only if there is no badge color defined * Remove lint warnings * Update screenshots for tests
1 parent 3d0d19b commit 8cee56b

File tree

3 files changed

+7
-9
lines changed

3 files changed

+7
-9
lines changed

counterfab/src/main/java/com/andremion/counterfab/CounterFab.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ import android.util.AttributeSet
3030
import android.util.Property
3131
import android.view.animation.OvershootInterpolator
3232
import androidx.annotation.IntRange
33+
import androidx.core.graphics.ColorUtils
3334
import androidx.core.os.bundleOf
3435
import androidx.core.view.ViewCompat
3536
import com.google.android.material.floatingactionbutton.FloatingActionButton
3637
import com.google.android.material.stateful.ExtendableSavedState
38+
import kotlin.math.max
3739

3840
private val STATE_KEY = CounterFab::class.java.name + ".STATE"
3941
private const val COUNT_STATE = "COUNT"
@@ -78,11 +80,7 @@ class CounterFab @JvmOverloads constructor(
7880
private val textPadding = TEXT_PADDING_DP * resources.displayMetrics.density
7981

8082
private val circlePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
81-
style = Paint.Style.FILL
82-
}
83-
private val maskPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
84-
style = Paint.Style.FILL
85-
color = MASK_COLOR
83+
style = Style.FILL
8684
}
8785
private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
8886
style = Style.FILL_AND_STROKE
@@ -158,7 +156,9 @@ class CounterFab @JvmOverloads constructor(
158156
circlePaint.color
159157
}
160158
}
161-
}
159+
}.applyColorMask()
160+
161+
private fun Int.applyColorMask() = ColorUtils.compositeColors(MASK_COLOR, this)
162162

163163
/**
164164
* Increase the current count value by 1
@@ -199,7 +199,7 @@ class CounterFab @JvmOverloads constructor(
199199
}
200200

201201
private fun calculateCircleBounds() {
202-
val circleRadius = Math.max(textBounds.width(), textBounds.height()) / 2f + textPadding
202+
val circleRadius = max(textBounds.width(), textBounds.height()) / 2f + textPadding
203203
val circleEnd = (circleRadius * 2).toInt()
204204
if (isSizeMini) {
205205
val circleStart = (circleRadius / 2).toInt()
@@ -245,8 +245,6 @@ class CounterFab @JvmOverloads constructor(
245245
val radius = circleBounds.width() / 2f * animationFactor
246246
// Solid circle
247247
canvas.drawCircle(cx, cy, radius, circlePaint)
248-
// Mask circle
249-
canvas.drawCircle(cx, cy, radius, maskPaint)
250248
// Count text
251249
textPaint.textSize = textSize * animationFactor
252250
canvas.drawText(countText, cx, cy + textBounds.height() / 2f, textPaint)
-11 Bytes
Loading
4 Bytes
Loading

0 commit comments

Comments
 (0)