Skip to content

Commit

Permalink
Apply badge color mask only for default color (#35)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
andremion authored Sep 26, 2019
1 parent 3d0d19b commit 8cee56b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions counterfab/src/main/java/com/andremion/counterfab/CounterFab.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ import android.util.AttributeSet
import android.util.Property
import android.view.animation.OvershootInterpolator
import androidx.annotation.IntRange
import androidx.core.graphics.ColorUtils
import androidx.core.os.bundleOf
import androidx.core.view.ViewCompat
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.stateful.ExtendableSavedState
import kotlin.math.max

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

private val circlePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.FILL
}
private val maskPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Paint.Style.FILL
color = MASK_COLOR
style = Style.FILL
}
private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
style = Style.FILL_AND_STROKE
Expand Down Expand Up @@ -158,7 +156,9 @@ class CounterFab @JvmOverloads constructor(
circlePaint.color
}
}
}
}.applyColorMask()

private fun Int.applyColorMask() = ColorUtils.compositeColors(MASK_COLOR, this)

/**
* Increase the current count value by 1
Expand Down Expand Up @@ -199,7 +199,7 @@ class CounterFab @JvmOverloads constructor(
}

private fun calculateCircleBounds() {
val circleRadius = Math.max(textBounds.width(), textBounds.height()) / 2f + textPadding
val circleRadius = max(textBounds.width(), textBounds.height()) / 2f + textPadding
val circleEnd = (circleRadius * 2).toInt()
if (isSizeMini) {
val circleStart = (circleRadius / 2).toInt()
Expand Down Expand Up @@ -245,8 +245,6 @@ class CounterFab @JvmOverloads constructor(
val radius = circleBounds.width() / 2f * animationFactor
// Solid circle
canvas.drawCircle(cx, cy, radius, circlePaint)
// Mask circle
canvas.drawCircle(cx, cy, radius, maskPaint)
// Count text
textPaint.textSize = textSize * animationFactor
canvas.drawText(countText, cx, cy + textBounds.height() / 2f, textPaint)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

4 comments on commit 8cee56b

@pierogonzales
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi!. cannot change colors in counter 'badgebackground'. No exist :(.

@andremion
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pierogonzales, you mean this app:badgeBackgroundColor attribute?

@pierogonzales
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.
There is no attribute about the badge and its configuration.

@andremion
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems you're missing something.. 🤔
Could you please add your code/layout snippet here to see what's wrong?

Please sign in to comment.