mirror of
https://github.com/Michatec/Radio.git
synced 2026-11-23 14:53:13 +01:00
feat(ui): add pulsing shader effect to station icon
Introduce a custom `ShaderEffectView` that uses AGSL (`RuntimeShader`) to display a pulsing glow animation behind the station icon during playback on supported Android versions (API 33+). * **Shader Animation**: Created `ShaderEffectView` to render a hardware-accelerated rounded rectangle glow effect with configurable colors and pulse timing. * **Layout Integration**: Added the shader view to both mobile and Android TV layouts, ensuring it is properly layered behind the station artwork. * **Lifecycle Management**: Updated `PlayerFragment` and `LayoutHolder` to start or stop the animation based on playback state and to synchronize the glow color with the station's primary image color. * **Visibility Logic**: Implementation ensures the effect is only visible when a station image is present and playback is active.
This commit is contained in:
1 parent
7e7c6871ff
commit
906b23188e
6 files changed
+145
-1
No files matched your search
@@ -61,6 +61,7 @@ import com.michatec.radio.extensions.*
|
||||
import com.michatec.radio.helpers.*
|
||||
import com.michatec.radio.ui.LayoutHolder
|
||||
import com.michatec.radio.ui.PlayerState
|
||||
import com.michatec.radio.ui.ShaderEffectView
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
import kotlinx.coroutines.Dispatchers.Main
|
||||
@@ -95,6 +96,7 @@ class PlayerFragment : Fragment(),
|
||||
private val handler: Handler = Handler(Looper.getMainLooper())
|
||||
private var tempStationUuid: String = String()
|
||||
private var itemTouchHelper: ItemTouchHelper? = null
|
||||
private var shaderEffectView: ShaderEffectView? = null
|
||||
|
||||
// Check if the device running the app is an Android TV instance
|
||||
private val isAndroidTV: Boolean by lazy {
|
||||
@@ -180,6 +182,7 @@ class PlayerFragment : Fragment(),
|
||||
val rootView: View = inflater.inflate(R.layout.fragment_player, container, false)
|
||||
layout = LayoutHolder(rootView)
|
||||
|
||||
shaderEffectView = rootView.findViewById(R.id.player_shader_effect)
|
||||
initializeViews()
|
||||
|
||||
// hide action bar
|
||||
@@ -298,6 +301,7 @@ class PlayerFragment : Fragment(),
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
queue.cancelAll(TAG)
|
||||
shaderEffectView?.stopAnimation()
|
||||
}
|
||||
|
||||
/* Overrides onSharedPreferenceChanged from OnSharedPreferenceChangeListener */
|
||||
@@ -559,8 +563,8 @@ class PlayerFragment : Fragment(),
|
||||
playerState.stationUuid = station.uuid
|
||||
}
|
||||
// update views
|
||||
layout.togglePlayButton(playerState.isPlaying)
|
||||
layout.updatePlayerViews(activity as Context, station, playerState.isPlaying)
|
||||
layout.togglePlayButton(playerState.isPlaying)
|
||||
|
||||
// main play/pause button
|
||||
layout.playButtonView.setOnClickListener {
|
||||
|
||||
@@ -49,6 +49,7 @@ data class LayoutHolder(var rootView: View) {
|
||||
private var sleepTimerRunningViews: Group? = rootView.findViewById(R.id.sleep_timer_running_views)
|
||||
private var downloadProgressIndicator: ProgressBar? = rootView.findViewById(R.id.download_progress_indicator)
|
||||
private var stationImageView: ImageView? = rootView.findViewById(R.id.station_icon)
|
||||
private var shaderEffectView: ShaderEffectView? = rootView.findViewById(R.id.player_shader_effect)
|
||||
private var stationNameView: TextView? = rootView.findViewById(R.id.player_station_name)
|
||||
private var metadataView: TextView? = rootView.findViewById(R.id.player_station_metadata)
|
||||
var playButtonView: ImageButton = rootView.findViewById(R.id.player_play_button)
|
||||
@@ -73,6 +74,7 @@ data class LayoutHolder(var rootView: View) {
|
||||
private var metadataHistory: MutableList<String>
|
||||
private var metadataHistoryPosition: Int
|
||||
private var isBuffering: Boolean
|
||||
private var hasStationImage: Boolean = false
|
||||
|
||||
|
||||
/* Init block */
|
||||
@@ -148,8 +150,17 @@ data class LayoutHolder(var rootView: View) {
|
||||
stationNameView?.setFadingEdgeLength(8)
|
||||
|
||||
// update cover
|
||||
hasStationImage = station.smallImage.isNotEmpty()
|
||||
if (hasStationImage) {
|
||||
shaderEffectView?.isVisible = true
|
||||
} else {
|
||||
shaderEffectView?.isGone = true
|
||||
shaderEffectView?.stopAnimation()
|
||||
}
|
||||
|
||||
if (station.imageColor != -1) {
|
||||
stationImageView?.setBackgroundColor(station.imageColor)
|
||||
shaderEffectView?.setColor(station.imageColor)
|
||||
}
|
||||
stationImageView?.setImageBitmap(ImageHelper.getStationImage(context, station.smallImage))
|
||||
stationImageView?.contentDescription = "${context.getString(R.string.descr_player_station_image)}: ${station.name}"
|
||||
@@ -302,9 +313,13 @@ data class LayoutHolder(var rootView: View) {
|
||||
playButtonView.setImageResource(R.drawable.ic_audio_waves_animated)
|
||||
val animatedVectorDrawable = playButtonView.drawable as? AnimatedVectorDrawable
|
||||
animatedVectorDrawable?.start()
|
||||
if (hasStationImage) {
|
||||
shaderEffectView?.startAnimation()
|
||||
}
|
||||
sheetSleepTimerStartButtonView?.isVisible = true
|
||||
} else {
|
||||
playButtonView.setImageResource(R.drawable.ic_player_play_symbol_42dp)
|
||||
shaderEffectView?.stopAnimation()
|
||||
sheetSleepTimerStartButtonView?.isVisible = false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.michatec.radio.ui
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.Paint
|
||||
import android.graphics.RuntimeShader
|
||||
import android.os.Build
|
||||
import android.util.AttributeSet
|
||||
import android.view.View
|
||||
|
||||
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 var startTime = System.currentTimeMillis()
|
||||
private var isAnimating = false
|
||||
|
||||
private val shaderCode = """
|
||||
uniform float2 iResolution;
|
||||
uniform float iTime;
|
||||
uniform float4 iColor;
|
||||
|
||||
// Signed distance function for a rounded rectangle
|
||||
float sdRoundRect(vec2 p, vec2 b, float r) {
|
||||
vec2 d = abs(p) - b + vec2(r);
|
||||
return min(max(d.x, d.y), 0.0) + length(max(d, 0.0)) - r;
|
||||
}
|
||||
|
||||
vec4 main(vec2 fragCoord) {
|
||||
vec2 center = 0.5 * iResolution.xy;
|
||||
vec2 p = fragCoord - center;
|
||||
float minRes = min(iResolution.x, iResolution.y);
|
||||
|
||||
float halfSize = minRes * 0.33;
|
||||
float radius = halfSize * 0.3;
|
||||
|
||||
float dist = sdRoundRect(p, vec2(halfSize), radius);
|
||||
|
||||
float pulse = 0.5 + 0.5 * sin(iTime * 2.5);
|
||||
|
||||
float glow = exp(-max(0.0, dist) / (minRes * (0.01 + 0.01 * pulse)));
|
||||
|
||||
vec3 col = iColor.rgb * glow * (1.0 + 0.6 * pulse);
|
||||
|
||||
float edgeFade = 1.0 - smoothstep(minRes * 0.35, minRes * 0.48, length(p));
|
||||
|
||||
return vec4(col, glow * edgeFade * 0.6);
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
init {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
shader = RuntimeShader(shaderCode)
|
||||
}
|
||||
}
|
||||
|
||||
fun startAnimation() {
|
||||
if (!isAnimating) {
|
||||
isAnimating = true
|
||||
startTime = System.currentTimeMillis()
|
||||
invalidate()
|
||||
}
|
||||
}
|
||||
|
||||
fun stopAnimation() {
|
||||
isAnimating = false
|
||||
invalidate()
|
||||
}
|
||||
|
||||
fun setColor(color: Int) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDraw(canvas: Canvas) {
|
||||
super.onDraw(canvas)
|
||||
|
||||
if (!isAnimating || 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user