Add fallback drawing logic to ShaderEffectView

- Implement `drawFallback` using standard Canvas APIs to support devices below Android 13 (API 33).
- Refactor `onDraw` to toggle between shader-based rendering and the fallback animation.
- Update `setColor` to store the current color and trigger a redraw via `invalidate()`.
- Add anti-aliasing to the paint instance and use a reusable `RectF` for fallback rendering.
This commit is contained in:
Michatec committed 2026-06-30 13:49:14 +02:00
1 parent 906b23188e
commit b65b666b81
1 file changed
+60 -6
@@ -2,20 +2,25 @@ package com.michatec.radio.ui
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.graphics.RuntimeShader
import android.os.Build
import android.util.AttributeSet
import android.view.View
import kotlin.math.sin
class ShaderEffectView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
private var shader: RuntimeShader? = null
private val paint = Paint()
private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
private var startTime = System.currentTimeMillis()
private var isAnimating = false
private var currentColor: Int = Color.BLUE
private val fallbackRect = RectF()
private val shaderCode = """
uniform float2 iResolution;
@@ -70,29 +75,78 @@ class ShaderEffectView @JvmOverloads constructor(
}
fun setColor(color: Int) {
currentColor = color
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
val r = (color shr 16 and 0xFF) / 255f
val g = (color shr 8 and 0xFF) / 255f
val b = (color and 0xFF) / 255f
shader?.setFloatUniform("iColor", r, g, b, 1.0f)
}
invalidate()
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
if (!isAnimating || Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
return
}
if (!isAnimating) return
val currentTime = (System.currentTimeMillis() - startTime) / 1000f
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
drawWithShader(canvas)
} else {
drawFallback(canvas)
}
invalidate()
}
private fun drawWithShader(canvas: Canvas) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return
val currentTime = (System.currentTimeMillis() - startTime) / 1000f
shader?.let { s ->
s.setFloatUniform("iResolution", width.toFloat(), height.toFloat())
s.setFloatUniform("iTime", currentTime)
paint.shader = s
canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), paint)
invalidate()
}
}
private fun drawFallback(canvas: Canvas) {
val currentTime = (System.currentTimeMillis() - startTime) / 1000f
val pulse = 0.5f + 0.5f * sin(currentTime * 2.5f)
val minRes = minOf(width, height).toFloat()
val centerX = width / 2f
val centerY = height / 2f
val halfSize = minRes * 0.38f
val radius = halfSize * 0.3f
paint.shader = null
paint.style = Paint.Style.FILL
for (i in 1..3) {
val glowExpand = (i * 10f * (0.5f + 0.5f * pulse))
val alpha = (40 / i * (0.6f + 0.4f * pulse)).toInt()
paint.color = currentColor
paint.alpha = alpha
fallbackRect.set(
centerX - halfSize - glowExpand,
centerY - halfSize - glowExpand,
centerX + halfSize + glowExpand,
centerY + halfSize + glowExpand
)
canvas.drawRoundRect(fallbackRect, radius + glowExpand, radius + glowExpand, paint)
}
paint.alpha = 255
paint.color = currentColor
fallbackRect.set(
centerX - halfSize,
centerY - halfSize,
centerX + halfSize,
centerY + halfSize
)
canvas.drawRoundRect(fallbackRect, radius, radius, paint)
}
}