Add animation to SeekBar

This commit is contained in:
vfsfitvnm
2022-07-27 18:06:41 +02:00
parent 4e460a1427
commit 193c98e20e

View File

@@ -1,5 +1,8 @@
package it.vfsfitvnm.vimusic.ui.components package it.vfsfitvnm.vimusic.ui.components
import androidx.compose.animation.core.MutableTransitionState
import androidx.compose.animation.core.animateDp
import androidx.compose.animation.core.updateTransition
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectHorizontalDragGestures import androidx.compose.foundation.gestures.detectHorizontalDragGestures
import androidx.compose.foundation.gestures.detectTapGestures import androidx.compose.foundation.gestures.detectTapGestures
@@ -9,6 +12,8 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithContent import androidx.compose.ui.draw.drawWithContent
@@ -31,12 +36,21 @@ fun SeekBar(
color: Color, color: Color,
backgroundColor: Color, backgroundColor: Color,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
barHeight: Dp = 3.5.dp, barHeight: Dp = 3.dp,
scrubberColor: Color = color, scrubberColor: Color = color,
scrubberRadius: Dp = 6.dp, scrubberRadius: Dp = 6.dp,
shape: Shape = RectangleShape, shape: Shape = RectangleShape,
drawSteps: Boolean = false, drawSteps: Boolean = false,
) { ) {
val isDragging = remember {
MutableTransitionState(false)
}
val transition = updateTransition(transitionState = isDragging, label = null)
val currentBarHeight by transition.animateDp(label = "") { if (it) scrubberRadius else barHeight }
val currentScrubberRadius by transition.animateDp(label = "") { if (it) 0.dp else scrubberRadius }
Box( Box(
modifier = modifier modifier = modifier
.pointerInput(minimumValue, maximumValue) { .pointerInput(minimumValue, maximumValue) {
@@ -45,6 +59,9 @@ fun SeekBar(
var acc = 0f var acc = 0f
detectHorizontalDragGestures( detectHorizontalDragGestures(
onDragStart = {
isDragging.targetState = true
},
onHorizontalDrag = { _, delta -> onHorizontalDrag = { _, delta ->
acc += delta / size.width * (maximumValue - minimumValue) acc += delta / size.width * (maximumValue - minimumValue)
@@ -54,10 +71,12 @@ fun SeekBar(
} }
}, },
onDragEnd = { onDragEnd = {
isDragging.targetState = false
acc = 0f acc = 0f
onDragEnd() onDragEnd()
}, },
onDragCancel = { onDragCancel = {
isDragging.targetState = false
acc = 0f acc = 0f
onDragEnd() onDragEnd()
} }
@@ -87,7 +106,7 @@ fun SeekBar(
drawCircle( drawCircle(
color = scrubberColor, color = scrubberColor,
radius = scrubberRadius.toPx(), radius = currentScrubberRadius.toPx(),
center = center.copy(x = scrubberPosition) center = center.copy(x = scrubberPosition)
) )
@@ -103,10 +122,11 @@ fun SeekBar(
} }
} }
} }
.height(scrubberRadius)
) { ) {
Spacer( Spacer(
modifier = Modifier modifier = Modifier
.height(barHeight) .height(currentBarHeight)
.fillMaxWidth() .fillMaxWidth()
.background(color = backgroundColor, shape = shape) .background(color = backgroundColor, shape = shape)
.align(Alignment.Center) .align(Alignment.Center)
@@ -114,9 +134,10 @@ fun SeekBar(
Spacer( Spacer(
modifier = Modifier modifier = Modifier
.height(barHeight) .height(currentBarHeight)
.fillMaxWidth((value.toFloat() - minimumValue) / (maximumValue - minimumValue)) .fillMaxWidth((value.toFloat() - minimumValue) / (maximumValue - minimumValue))
.background(color = color, shape = shape) .background(color = color, shape = shape)
.align(Alignment.CenterStart)
) )
} }
} }