Fix issue where line alpha would be overridden

A Paint doesn't have separate components for color and alpha, so when
setting a color, the alpha will be lost.

We need to take that into account when updating the tint.

Test: manual
Test: atest SquigglyProgressTest
Fixes: 227388771
Change-Id: Ibb85305a01c552c1476e502d23614de097f4680c
This commit is contained in:
Lucas Dupin
2022-03-29 16:24:13 -07:00
parent 74807869b8
commit 41ea86f6c8
2 changed files with 13 additions and 7 deletions

View File

@@ -13,6 +13,7 @@ import android.graphics.PixelFormat
import android.graphics.drawable.Drawable
import android.os.SystemClock
import androidx.annotation.VisibleForTesting
import com.android.internal.graphics.ColorUtils
import com.android.systemui.animation.Interpolators
import kotlin.math.abs
import kotlin.math.cos
@@ -157,8 +158,7 @@ class SquigglyProgress : Drawable() {
}
override fun setAlpha(alpha: Int) {
wavePaint.alpha = alpha
linePaint.alpha = (DISABLED_ALPHA * (alpha / 255f)).toInt()
updateColors(wavePaint.color, alpha)
}
override fun getAlpha(): Int {
@@ -166,8 +166,7 @@ class SquigglyProgress : Drawable() {
}
override fun setTint(tintColor: Int) {
wavePaint.color = tintColor
linePaint.color = tintColor
updateColors(tintColor, alpha)
}
override fun onLevelChange(level: Int): Boolean {
@@ -178,7 +177,12 @@ class SquigglyProgress : Drawable() {
if (tint == null) {
return
}
wavePaint.color = tint.defaultColor
linePaint.color = tint.defaultColor
updateColors(tint.defaultColor, alpha)
}
private fun updateColors(tintColor: Int, alpha: Int) {
wavePaint.color = ColorUtils.setAlphaComponent(tintColor, alpha)
linePaint.color = ColorUtils.setAlphaComponent(tintColor,
(DISABLED_ALPHA * (alpha / 255f)).toInt())
}
}

View File

@@ -8,6 +8,7 @@ import android.graphics.Rect
import android.testing.AndroidTestingRunner
import android.testing.TestableLooper
import androidx.test.filters.SmallTest
import com.android.internal.graphics.ColorUtils
import com.android.systemui.SysuiTestCase
import com.android.systemui.util.mockito.any
import com.google.common.truth.Truth.assertThat
@@ -113,6 +114,7 @@ class SquigglyProgressTest : SysuiTestCase() {
linePaintCaptor.capture())
assertThat(wavePaintCaptor.value.color).isEqualTo(tint)
assertThat(linePaintCaptor.value.color).isEqualTo(tint)
assertThat(linePaintCaptor.value.color).isEqualTo(
ColorUtils.setAlphaComponent(tint, DISABLED_ALPHA))
}
}