ui: cleanup wavy seekbar
This commit is contained in:
@@ -38,17 +38,20 @@ import androidx.core.graphics.withSave
|
|||||||
import androidx.core.view.isVisible
|
import androidx.core.view.isVisible
|
||||||
import androidx.dynamicanimation.animation.FloatValueHolder
|
import androidx.dynamicanimation.animation.FloatValueHolder
|
||||||
import androidx.dynamicanimation.animation.SpringAnimation
|
import androidx.dynamicanimation.animation.SpringAnimation
|
||||||
import com.google.android.material.R as MR
|
|
||||||
import com.google.android.material.motion.MotionUtils
|
import com.google.android.material.motion.MotionUtils
|
||||||
|
import org.oxycblt.auxio.R
|
||||||
|
import timber.log.Timber
|
||||||
import kotlin.math.abs
|
import kotlin.math.abs
|
||||||
import kotlin.math.atan2
|
import kotlin.math.atan2
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
import org.oxycblt.auxio.R
|
import com.google.android.material.R as MR
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Slider with active-track wave rendering that ports MDC LinearProgressIndicator's wavy draw
|
* Slider with active-track wave rendering that ports MDC LinearProgressIndicator's wavy draw
|
||||||
* behavior.
|
* behavior.
|
||||||
|
*
|
||||||
|
* @author Codex, Alexander Capehart (OxygenCobalt) (Clean Up/Rewrite/Cognitive Ownership)
|
||||||
*/
|
*/
|
||||||
class WavySlider
|
class WavySlider
|
||||||
@JvmOverloads
|
@JvmOverloads
|
||||||
@@ -57,11 +60,7 @@ constructor(
|
|||||||
attrs: AttributeSet? = null,
|
attrs: AttributeSet? = null,
|
||||||
defStyleAttr: Int = MR.attr.sliderStyle,
|
defStyleAttr: Int = MR.attr.sliderStyle,
|
||||||
) : Slider(context, attrs, defStyleAttr) {
|
) : Slider(context, attrs, defStyleAttr) {
|
||||||
private val wavePaint =
|
private val wavePaint: Paint
|
||||||
Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
|
||||||
style = Paint.Style.STROKE
|
|
||||||
strokeCap = Paint.Cap.BUTT
|
|
||||||
}
|
|
||||||
|
|
||||||
private val displayedWavePath = Path()
|
private val displayedWavePath = Path()
|
||||||
private val pathMeasure = PathMeasure()
|
private val pathMeasure = PathMeasure()
|
||||||
@@ -72,16 +71,17 @@ constructor(
|
|||||||
private var cachedWavelength = -1
|
private var cachedWavelength = -1
|
||||||
private var cachedTrackLength = -1f
|
private var cachedTrackLength = -1f
|
||||||
|
|
||||||
private val drawRect = RectF()
|
private val drawRect =
|
||||||
|
RectF(-trackHeight / 2f, -trackHeight / 2f, trackHeight / 2f, trackHeight / 2f)
|
||||||
private val patchRect = RectF()
|
private val patchRect = RectF()
|
||||||
private val clipRect = RectF()
|
private val clipRect = RectF()
|
||||||
private val roundedRectPath = Path()
|
private val roundedRectPath = Path()
|
||||||
|
|
||||||
private val startPoint = PathPoint()
|
private val startPoint = PointF()
|
||||||
private val endPoint = PathPoint()
|
private val endPoint = PointF()
|
||||||
|
|
||||||
private val transparentTrackTint = ColorStateList.valueOf(Color.TRANSPARENT)
|
private val transparentTrackTint = ColorStateList.valueOf(Color.TRANSPARENT)
|
||||||
private var waveTrackTintList: ColorStateList = trackActiveTintList
|
private var tmpTrackTintList: ColorStateList = trackActiveTintList
|
||||||
private var linearActiveTrackSuppressed = false
|
private var linearActiveTrackSuppressed = false
|
||||||
|
|
||||||
private var waveTransitionAnimation: SpringAnimation? = null
|
private var waveTransitionAnimation: SpringAnimation? = null
|
||||||
@@ -146,6 +146,28 @@ constructor(
|
|||||||
"Invalid wave dimensions, must be >0"
|
"Invalid wave dimensions, must be >0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wavePaint =
|
||||||
|
Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
||||||
|
style = Paint.Style.STROKE
|
||||||
|
strokeCap = Paint.Cap.BUTT
|
||||||
|
strokeWidth = trackHeight.toFloat()
|
||||||
|
isAntiAlias = true
|
||||||
|
color = trackActiveTintList.getColorForState(
|
||||||
|
drawableState,
|
||||||
|
trackActiveTintList.defaultColor
|
||||||
|
)
|
||||||
|
}
|
||||||
|
updateActiveTrackSuppression()
|
||||||
|
|
||||||
|
if (trackCornerSize < trackHeight) {
|
||||||
|
Timber.w("WavySlider can only be fully rounded. Clamping corner size down to trackHeight / 2")
|
||||||
|
trackCornerSize = trackHeight
|
||||||
|
}
|
||||||
|
if (trackInsideCornerSize < trackHeight) {
|
||||||
|
Timber.w("WavySlider can only be fully rounded. Clamping inner corner size down to trackHeight / 2")
|
||||||
|
trackInsideCornerSize = trackHeight
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onAttachedToWindow() {
|
override fun onAttachedToWindow() {
|
||||||
@@ -173,7 +195,7 @@ constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun setTrackActiveTintList(trackColor: ColorStateList) {
|
override fun setTrackActiveTintList(trackColor: ColorStateList) {
|
||||||
waveTrackTintList = trackColor
|
tmpTrackTintList = trackColor
|
||||||
if (!linearActiveTrackSuppressed) {
|
if (!linearActiveTrackSuppressed) {
|
||||||
super.setTrackActiveTintList(trackColor)
|
super.setTrackActiveTintList(trackColor)
|
||||||
}
|
}
|
||||||
@@ -211,209 +233,172 @@ constructor(
|
|||||||
override fun onDraw(canvas: Canvas) {
|
override fun onDraw(canvas: Canvas) {
|
||||||
super.onDraw(canvas)
|
super.onDraw(canvas)
|
||||||
|
|
||||||
if (!shouldDrawWave()) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
val trackLength = trackWidth.toFloat()
|
val trackLength = trackWidth.toFloat()
|
||||||
if (trackLength <= 0f) {
|
if (trackLength <= 0f) {
|
||||||
|
// Nothing to draw width-wise
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
val range = valueTo - valueFrom
|
val range = valueTo - valueFrom
|
||||||
if (range <= 0f) {
|
if (range <= 0f) {
|
||||||
|
// Nothing to draw length-wise
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
val progressFraction = ((value - valueFrom) / range).coerceIn(0f, 1f)
|
val progressFraction = ((value - valueFrom) / range).coerceIn(0f, 1f)
|
||||||
if (progressFraction <= 0f) {
|
if (progressFraction <= 0f) {
|
||||||
|
// Nothing to draw progress-wise (the wavy part is only from active process)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
displayedTrackThickness = trackHeight.toFloat()
|
displayedTrackThickness = trackHeight.toFloat()
|
||||||
displayedCornerRadius = min(displayedTrackThickness / 2f, trackCornerSize.toFloat())
|
displayedCornerRadius =
|
||||||
displayedInnerCornerRadius =
|
min(
|
||||||
min(displayedTrackThickness / 2f, trackInsideCornerSize.toFloat())
|
min(displayedTrackThickness / 2f, trackCornerSize.toFloat()),
|
||||||
displayedAmplitude = configuredAmplitudePx.toFloat()
|
trackLength / 2f,
|
||||||
|
)
|
||||||
|
displayedInnerCornerRadius = displayedCornerRadius
|
||||||
|
|
||||||
|
// Calculate the actual wave path or re-use it
|
||||||
ensureCachedWavePath(trackLength, configuredWavelengthPx)
|
ensureCachedWavePath(trackLength, configuredWavelengthPx)
|
||||||
if (pathMeasure.length <= 0f || adjustedWavelength <= 0f) {
|
if (pathMeasure.length <= 0f || adjustedWavelength <= 0f) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tune down the amplitude by the progress ramping
|
||||||
|
// Important since short wavy progress looks odd and we want to interpolate
|
||||||
|
// in all things considered
|
||||||
|
val startRampFraction =
|
||||||
|
if (shouldDrawWave()) {
|
||||||
|
calculateStartRampFraction(progressFraction, trackLength)
|
||||||
|
} else {
|
||||||
|
0f
|
||||||
|
}
|
||||||
|
val rampedAmplitudeFraction =
|
||||||
|
if (shouldDrawWave()) {
|
||||||
|
applyStartWaveRamp(
|
||||||
|
amplitudeFraction = currentAmplitudeFraction,
|
||||||
|
startRampFraction = startRampFraction,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
0f
|
||||||
|
}
|
||||||
|
displayedAmplitude = configuredAmplitudePx.toFloat() * rampedAmplitudeFraction
|
||||||
|
|
||||||
val activeTrackColor =
|
val activeTrackColor =
|
||||||
waveTrackTintList.getColorForState(drawableState, waveTrackTintList.defaultColor)
|
tmpTrackTintList.getColorForState(drawableState, tmpTrackTintList.defaultColor)
|
||||||
wavePaint.color = activeTrackColor
|
wavePaint.color = activeTrackColor
|
||||||
wavePaint.strokeWidth = displayedTrackThickness
|
wavePaint.strokeWidth = displayedTrackThickness
|
||||||
val startRampFraction = calculateStartRampFraction(progressFraction, trackLength)
|
|
||||||
val rampedAmplitudeFraction =
|
// Now
|
||||||
applyStartWaveRamp(
|
val isRtl = layoutDirection == LAYOUT_DIRECTION_RTL
|
||||||
amplitudeFraction = currentAmplitudeFraction,
|
|
||||||
startRampFraction = startRampFraction,
|
// Where the thumb should be
|
||||||
)
|
val thumbTrackPosition =
|
||||||
drawActiveWaveSegment(
|
if (isRtl) {
|
||||||
|
// RTL - Invert progress
|
||||||
|
(1f - progressFraction) * trackLength
|
||||||
|
} else {
|
||||||
|
progressFraction * trackLength
|
||||||
|
}
|
||||||
|
|
||||||
|
// Where the progress track should start
|
||||||
|
val startBound =
|
||||||
|
if (isRtl) {
|
||||||
|
// RTL, anchor start to pos (making room for gap)
|
||||||
|
thumbTrackPosition + thumbTrackGapSize
|
||||||
|
} else {
|
||||||
|
// LTR, anchor start to right (making room for corner radius)
|
||||||
|
-displayedCornerRadius
|
||||||
|
}
|
||||||
|
|
||||||
|
// Where the progress track should end
|
||||||
|
val endBound =
|
||||||
|
if (isRtl) {
|
||||||
|
// RTL, anchor end to left (making room for corner radius)
|
||||||
|
trackLength + displayedCornerRadius
|
||||||
|
} else {
|
||||||
|
// LTR, anchor end to progress (make room for gap)
|
||||||
|
thumbTrackPosition - thumbTrackGapSize
|
||||||
|
}
|
||||||
|
if (startBound >= endBound) {
|
||||||
|
// No progress to draw.
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up the start/end corner radii (this changes for inner/outer radii)
|
||||||
|
drawTrack(
|
||||||
canvas = canvas,
|
canvas = canvas,
|
||||||
trackLength = trackLength,
|
trackLength = trackLength,
|
||||||
progressFraction = progressFraction,
|
startBound = startBound,
|
||||||
paintColor = activeTrackColor,
|
endBound = endBound,
|
||||||
amplitudeFraction = rampedAmplitudeFraction,
|
amplitudeFraction = rampedAmplitudeFraction,
|
||||||
phaseFraction = phaseFraction,
|
phaseFraction = phaseFraction,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun drawActiveWaveSegment(
|
private fun drawTrack(
|
||||||
canvas: Canvas,
|
|
||||||
trackLength: Float,
|
|
||||||
progressFraction: Float,
|
|
||||||
paintColor: Int,
|
|
||||||
amplitudeFraction: Float,
|
|
||||||
phaseFraction: Float,
|
|
||||||
) {
|
|
||||||
val clampedProgress = progressFraction.coerceIn(0f, 1f)
|
|
||||||
val isRtl = layoutDirection == LAYOUT_DIRECTION_RTL
|
|
||||||
val direction = if (isRtl) ActiveTrackDirection.RIGHT else ActiveTrackDirection.LEFT
|
|
||||||
val thumbTrackPosition =
|
|
||||||
if (isRtl) {
|
|
||||||
(1f - clampedProgress) * trackLength
|
|
||||||
} else {
|
|
||||||
clampedProgress * trackLength
|
|
||||||
}
|
|
||||||
|
|
||||||
val startBound =
|
|
||||||
if (direction == ActiveTrackDirection.LEFT) {
|
|
||||||
-displayedCornerRadius
|
|
||||||
} else {
|
|
||||||
thumbTrackPosition + thumbTrackGapSize
|
|
||||||
}
|
|
||||||
val endBound =
|
|
||||||
if (direction == ActiveTrackDirection.LEFT) {
|
|
||||||
thumbTrackPosition - thumbTrackGapSize
|
|
||||||
} else {
|
|
||||||
trackLength + displayedCornerRadius
|
|
||||||
}
|
|
||||||
if (startBound >= endBound) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
val startCornerRadius = calculateStartTrackCornerSize(thumbTrackPosition)
|
|
||||||
val endCornerRadius = calculateEndTrackCornerSize(trackLength, thumbTrackPosition)
|
|
||||||
|
|
||||||
drawSegment(
|
|
||||||
canvas = canvas,
|
|
||||||
trackLength = trackLength,
|
|
||||||
startBound = startBound,
|
|
||||||
endBound = endBound,
|
|
||||||
startCornerRadius = startCornerRadius,
|
|
||||||
endCornerRadius = endCornerRadius,
|
|
||||||
paintColor = paintColor,
|
|
||||||
amplitudeFraction = amplitudeFraction,
|
|
||||||
phaseFraction = phaseFraction,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun drawSegment(
|
|
||||||
canvas: Canvas,
|
canvas: Canvas,
|
||||||
trackLength: Float,
|
trackLength: Float,
|
||||||
startBound: Float,
|
startBound: Float,
|
||||||
endBound: Float,
|
endBound: Float,
|
||||||
startCornerRadius: Float,
|
|
||||||
endCornerRadius: Float,
|
|
||||||
paintColor: Int,
|
|
||||||
amplitudeFraction: Float,
|
amplitudeFraction: Float,
|
||||||
phaseFraction: Float,
|
phaseFraction: Float,
|
||||||
) {
|
) {
|
||||||
|
// This logic seems more complex than it should be largely because
|
||||||
|
// we have to gracefully collapse the active track as we scroll to
|
||||||
|
// the minimum
|
||||||
val originX = trackSidePadding.toFloat()
|
val originX = trackSidePadding.toFloat()
|
||||||
val trackCenterY = height / 2f
|
val trackCenterY = height / 2f
|
||||||
|
|
||||||
val startBlockCenterX = startBound + startCornerRadius
|
val startBlockCenterX = startBound + displayedCornerRadius
|
||||||
val endBlockCenterX = endBound - endCornerRadius
|
val endBlockCenterX = endBound - displayedCornerRadius
|
||||||
val startBlockWidth = startCornerRadius * 2f
|
|
||||||
val endBlockWidth = endCornerRadius * 2f
|
|
||||||
|
|
||||||
wavePaint.color = paintColor
|
|
||||||
wavePaint.isAntiAlias = true
|
|
||||||
wavePaint.strokeWidth = displayedTrackThickness
|
|
||||||
|
|
||||||
startPoint.reset()
|
startPoint.reset()
|
||||||
endPoint.reset()
|
endPoint.reset()
|
||||||
startPoint.translate(startBlockCenterX + originX, trackCenterY)
|
startPoint.translate(startBlockCenterX + originX, trackCenterY)
|
||||||
endPoint.translate(endBlockCenterX + originX, trackCenterY)
|
endPoint.translate(endBlockCenterX + originX, trackCenterY)
|
||||||
|
|
||||||
val drawWavyPath = amplitudeFraction > 0f
|
|
||||||
if (
|
if (
|
||||||
startBound <= 0f &&
|
startBound <= 0f && endBlockCenterX < startBlockCenterX
|
||||||
endBlockCenterX + endCornerRadius < startBlockCenterX + startCornerRadius
|
|
||||||
) {
|
) {
|
||||||
drawRoundedBlock(
|
drawRoundedBlock(
|
||||||
canvas = canvas,
|
canvas = canvas,
|
||||||
drawCenter = startPoint,
|
drawCenter = startPoint,
|
||||||
drawWidth = startBlockWidth,
|
|
||||||
drawHeight = displayedTrackThickness,
|
|
||||||
drawCornerSize = startCornerRadius,
|
|
||||||
clipCenter = endPoint,
|
clipCenter = endPoint,
|
||||||
clipWidth = endBlockWidth,
|
|
||||||
clipHeight = displayedTrackThickness,
|
|
||||||
clipCornerSize = endCornerRadius,
|
|
||||||
clipRight = true,
|
clipRight = true,
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (startBlockCenterX - startCornerRadius > endBlockCenterX - endCornerRadius) {
|
if (startBlockCenterX > endBlockCenterX) {
|
||||||
drawRoundedBlock(
|
drawRoundedBlock(
|
||||||
canvas = canvas,
|
canvas = canvas,
|
||||||
drawCenter = endPoint,
|
drawCenter = endPoint,
|
||||||
drawWidth = endBlockWidth,
|
|
||||||
drawHeight = displayedTrackThickness,
|
|
||||||
drawCornerSize = endCornerRadius,
|
|
||||||
clipCenter = startPoint,
|
clipCenter = startPoint,
|
||||||
clipWidth = startBlockWidth,
|
|
||||||
clipHeight = displayedTrackThickness,
|
|
||||||
clipCornerSize = startCornerRadius,
|
|
||||||
clipRight = false,
|
clipRight = false,
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
wavePaint.style = Paint.Style.STROKE
|
wavePaint.style = Paint.Style.STROKE
|
||||||
wavePaint.strokeCap = if (useStrokeCap()) Paint.Cap.ROUND else Paint.Cap.BUTT
|
wavePaint.strokeCap = Paint.Cap.ROUND
|
||||||
|
|
||||||
if (!drawWavyPath) {
|
if (amplitudeFraction > 0f) {
|
||||||
canvas.drawLine(
|
|
||||||
startPoint.posVec.x,
|
|
||||||
startPoint.posVec.y,
|
|
||||||
endPoint.posVec.x,
|
|
||||||
endPoint.posVec.y,
|
|
||||||
wavePaint,
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
calculateDisplayedWavePath(
|
calculateDisplayedWavePath(
|
||||||
trackLength = trackLength,
|
trackLength = trackLength,
|
||||||
start = startBlockCenterX / trackLength,
|
start = startBlockCenterX / trackLength,
|
||||||
end = endBlockCenterX / trackLength,
|
end = endBlockCenterX / trackLength,
|
||||||
amplitudeFraction = amplitudeFraction,
|
|
||||||
phaseFraction = phaseFraction,
|
phaseFraction = phaseFraction,
|
||||||
trackCenterY = trackCenterY,
|
trackCenterY = trackCenterY,
|
||||||
)
|
)
|
||||||
canvas.drawPath(displayedWavePath, wavePaint)
|
canvas.drawPath(displayedWavePath, wavePaint)
|
||||||
}
|
} else {
|
||||||
|
canvas.drawLine(
|
||||||
if (!useStrokeCap()) {
|
startPoint.x,
|
||||||
if (startCornerRadius > 0f) {
|
startPoint.y,
|
||||||
drawRoundedBlock(
|
endPoint.x,
|
||||||
canvas = canvas,
|
endPoint.y,
|
||||||
drawCenter = startPoint,
|
wavePaint,
|
||||||
drawWidth = startBlockWidth,
|
)
|
||||||
drawHeight = displayedTrackThickness,
|
|
||||||
drawCornerSize = startCornerRadius,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (endCornerRadius > 0f) {
|
|
||||||
drawRoundedBlock(
|
|
||||||
canvas = canvas,
|
|
||||||
drawCenter = endPoint,
|
|
||||||
drawWidth = endBlockWidth,
|
|
||||||
drawHeight = displayedTrackThickness,
|
|
||||||
drawCornerSize = endCornerRadius,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -421,7 +406,6 @@ constructor(
|
|||||||
trackLength: Float,
|
trackLength: Float,
|
||||||
start: Float,
|
start: Float,
|
||||||
end: Float,
|
end: Float,
|
||||||
amplitudeFraction: Float,
|
|
||||||
phaseFraction: Float,
|
phaseFraction: Float,
|
||||||
trackCenterY: Float,
|
trackCenterY: Float,
|
||||||
) {
|
) {
|
||||||
@@ -429,7 +413,6 @@ constructor(
|
|||||||
trackLength = trackLength,
|
trackLength = trackLength,
|
||||||
start = start,
|
start = start,
|
||||||
end = end,
|
end = end,
|
||||||
amplitudeFraction = amplitudeFraction,
|
|
||||||
phaseFraction = phaseFraction,
|
phaseFraction = phaseFraction,
|
||||||
baseTranslationX = trackSidePadding.toFloat(),
|
baseTranslationX = trackSidePadding.toFloat(),
|
||||||
baseTranslationY = trackCenterY,
|
baseTranslationY = trackCenterY,
|
||||||
@@ -438,74 +421,69 @@ constructor(
|
|||||||
|
|
||||||
private fun drawRoundedBlock(
|
private fun drawRoundedBlock(
|
||||||
canvas: Canvas,
|
canvas: Canvas,
|
||||||
drawCenter: PathPoint,
|
drawCenter: PointF,
|
||||||
drawWidth: Float,
|
clipCenter: PointF,
|
||||||
drawHeight: Float,
|
clipRight: Boolean
|
||||||
drawCornerSize: Float,
|
|
||||||
) {
|
) {
|
||||||
drawRoundedBlock(
|
wavePaint.style = Paint.Style.FILL
|
||||||
canvas = canvas,
|
canvas.withSave {
|
||||||
paint = wavePaint,
|
var clipWidth = trackHeight.toFloat()
|
||||||
drawCenter = drawCenter,
|
val halfHeight = trackHeight / 2f
|
||||||
drawWidth = drawWidth,
|
if (clipRight) {
|
||||||
drawHeight = drawHeight,
|
val leftEdgeDiff =
|
||||||
drawCornerSize = drawCornerSize,
|
(clipCenter.x - trackHeight / 2f) - (drawCenter.x - trackHeight)
|
||||||
)
|
if (leftEdgeDiff > 0f) {
|
||||||
}
|
clipCenter.translate(-leftEdgeDiff / 2f, 0f)
|
||||||
|
clipWidth += leftEdgeDiff
|
||||||
|
}
|
||||||
|
patchRect.set(
|
||||||
|
drawCenter.x,
|
||||||
|
drawCenter.y - halfHeight,
|
||||||
|
drawCenter.x + trackHeight / 2f,
|
||||||
|
drawCenter.y + halfHeight,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
val rightEdgeDiff =
|
||||||
|
(clipCenter.x + trackHeight / 2f) - (drawCenter.x + trackHeight)
|
||||||
|
if (rightEdgeDiff < 0f) {
|
||||||
|
clipCenter.translate(-rightEdgeDiff / 2f, 0f)
|
||||||
|
clipWidth -= rightEdgeDiff
|
||||||
|
}
|
||||||
|
patchRect.set(
|
||||||
|
drawCenter.x - trackHeight / 2f,
|
||||||
|
drawCenter.y - halfHeight,
|
||||||
|
drawCenter.x,
|
||||||
|
drawCenter.y + halfHeight,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
private fun drawRoundedBlock(
|
val halfClipWidth = clipWidth / 2f
|
||||||
canvas: Canvas,
|
clipRect.set(
|
||||||
drawCenter: PathPoint,
|
clipCenter.x - halfClipWidth,
|
||||||
drawWidth: Float,
|
clipCenter.y - halfHeight,
|
||||||
drawHeight: Float,
|
clipCenter.x + halfClipWidth,
|
||||||
drawCornerSize: Float,
|
clipCenter.y + halfHeight,
|
||||||
clipCenter: PathPoint?,
|
)
|
||||||
clipWidth: Float,
|
|
||||||
clipHeight: Float,
|
|
||||||
clipCornerSize: Float,
|
|
||||||
clipRight: Boolean,
|
|
||||||
) {
|
|
||||||
drawRoundedBlock(
|
|
||||||
canvas = canvas,
|
|
||||||
paint = wavePaint,
|
|
||||||
drawCenter = drawCenter,
|
|
||||||
drawWidth = drawWidth,
|
|
||||||
drawHeight = drawHeight,
|
|
||||||
drawCornerSize = drawCornerSize,
|
|
||||||
clipCenter = clipCenter,
|
|
||||||
clipWidth = clipWidth,
|
|
||||||
clipHeight = clipHeight,
|
|
||||||
clipCornerSize = clipCornerSize,
|
|
||||||
clipRight = clipRight,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun calculateStartTrackCornerSize(thumbTrackPosition: Float): Float {
|
drawRect.set(
|
||||||
if (thumbTrackGapSize <= 0) {
|
drawCenter.x - halfHeight,
|
||||||
return displayedCornerRadius
|
drawCenter.y - halfHeight,
|
||||||
}
|
drawCenter.x + halfHeight,
|
||||||
return if (thumbTrackPosition < displayedCornerRadius) {
|
drawCenter.y + halfHeight,
|
||||||
max(thumbTrackPosition, displayedInnerCornerRadius)
|
)
|
||||||
} else {
|
|
||||||
displayedCornerRadius
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun calculateEndTrackCornerSize(trackLength: Float, thumbTrackPosition: Float): Float {
|
roundedRectPath.reset()
|
||||||
if (thumbTrackGapSize <= 0) {
|
roundedRectPath.addRoundRect(
|
||||||
return displayedCornerRadius
|
clipRect,
|
||||||
}
|
trackHeight.toFloat(),
|
||||||
return if (thumbTrackPosition > trackLength - displayedCornerRadius) {
|
trackHeight.toFloat(),
|
||||||
max(trackLength - thumbTrackPosition, displayedInnerCornerRadius)
|
Path.Direction.CCW
|
||||||
} else {
|
)
|
||||||
displayedCornerRadius
|
clipPath(roundedRectPath)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun useStrokeCap(): Boolean {
|
drawRect(patchRect, wavePaint)
|
||||||
val fullyRounded = abs(displayedCornerRadius - displayedTrackThickness / 2f) < EPSILON
|
drawRoundRect(drawRect, trackHeight.toFloat(), trackHeight.toFloat(), wavePaint)
|
||||||
val sameInnerOuter = abs(displayedInnerCornerRadius - displayedCornerRadius) < EPSILON
|
}
|
||||||
return fullyRounded && sameInnerOuter
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun applyStartWaveRamp(amplitudeFraction: Float, startRampFraction: Float): Float {
|
private fun applyStartWaveRamp(amplitudeFraction: Float, startRampFraction: Float): Float {
|
||||||
@@ -522,19 +500,12 @@ constructor(
|
|||||||
return progressRamp
|
return progressRamp
|
||||||
}
|
}
|
||||||
val thresholdPx = displayedCornerRadius + thumbTrackGapSize
|
val thresholdPx = displayedCornerRadius + thumbTrackGapSize
|
||||||
if (thresholdPx <= 0f) {
|
val rampDistancePx = max(displayedCornerRadius, configuredAmplitudePx.toFloat())
|
||||||
|
if (thresholdPx <= 0f || rampDistancePx <= 0f) {
|
||||||
return progressRamp
|
return progressRamp
|
||||||
}
|
}
|
||||||
val thresholdFraction = (thresholdPx / trackLength).coerceIn(0f, 1f)
|
val activeLengthPx = progressFraction * trackLength
|
||||||
val fullFraction = (thresholdFraction * 2f).coerceIn(0f, 1f)
|
val edgeRamp = ((activeLengthPx - thresholdPx) / rampDistancePx).coerceIn(0f, 1f)
|
||||||
if (fullFraction - thresholdFraction <= EPSILON) {
|
|
||||||
return progressRamp
|
|
||||||
}
|
|
||||||
val edgeRamp =
|
|
||||||
((progressFraction - thresholdFraction) / (fullFraction - thresholdFraction)).coerceIn(
|
|
||||||
0f,
|
|
||||||
1f,
|
|
||||||
)
|
|
||||||
return min(edgeRamp, progressRamp)
|
return min(edgeRamp, progressRamp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -640,7 +611,7 @@ constructor(
|
|||||||
applyRampEasing(calculateStartRampFraction(progressFraction, trackLength))
|
applyRampEasing(calculateStartRampFraction(progressFraction, trackLength))
|
||||||
val effectiveSpeed = speed.toFloat() * phaseSpeedScale
|
val effectiveSpeed = speed.toFloat() * phaseSpeedScale
|
||||||
if (effectiveSpeed == 0f) {
|
if (effectiveSpeed == 0f) {
|
||||||
// not actually moving once ramp is taken in, move on
|
// Not actually moving once ramp is taken in, move on
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -648,8 +619,8 @@ constructor(
|
|||||||
if (lastPhaseFrameNanos != 0L) {
|
if (lastPhaseFrameNanos != 0L) {
|
||||||
val deltaSeconds = (nowNanos - lastPhaseFrameNanos) / 1_000_000_000f
|
val deltaSeconds = (nowNanos - lastPhaseFrameNanos) / 1_000_000_000f
|
||||||
val delta = deltaSeconds * (effectiveSpeed / configuredWavelengthPx.toFloat())
|
val delta = deltaSeconds * (effectiveSpeed / configuredWavelengthPx.toFloat())
|
||||||
// move phase forward, take the decimal
|
// Move phase forward, take the decimal
|
||||||
// all incoming inputs (phase & delta) are not-zero so no need
|
// All incoming inputs (phase & delta) are not-zero so no need
|
||||||
// to care about negative modulo
|
// to care about negative modulo
|
||||||
phaseFraction = (phaseFraction + delta) % 1f
|
phaseFraction = (phaseFraction + delta) % 1f
|
||||||
}
|
}
|
||||||
@@ -664,53 +635,23 @@ constructor(
|
|||||||
private fun shouldDrawWave(): Boolean = waveEnabled || waveTransitionAnimation != null
|
private fun shouldDrawWave(): Boolean = waveEnabled || waveTransitionAnimation != null
|
||||||
|
|
||||||
private fun updateActiveTrackSuppression() {
|
private fun updateActiveTrackSuppression() {
|
||||||
val suppress = shouldDrawWave()
|
val suppress = true
|
||||||
if (suppress == linearActiveTrackSuppressed) {
|
if (suppress == linearActiveTrackSuppressed) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (suppress) {
|
if (suppress) {
|
||||||
waveTrackTintList = trackActiveTintList
|
|
||||||
super.setTrackActiveTintList(transparentTrackTint)
|
super.setTrackActiveTintList(transparentTrackTint)
|
||||||
} else {
|
} else {
|
||||||
super.setTrackActiveTintList(waveTrackTintList)
|
super.setTrackActiveTintList(tmpTrackTintList)
|
||||||
}
|
}
|
||||||
linearActiveTrackSuppressed = suppress
|
linearActiveTrackSuppressed = suppress
|
||||||
}
|
}
|
||||||
|
|
||||||
private enum class ActiveTrackDirection {
|
|
||||||
LEFT,
|
|
||||||
RIGHT,
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun applyRampEasing(fraction: Float): Float {
|
private fun applyRampEasing(fraction: Float): Float {
|
||||||
val clamped = fraction.coerceIn(0f, 1f)
|
val clamped = fraction.coerceIn(0f, 1f)
|
||||||
return clamped * clamped * (3f - 2f * clamped)
|
return clamped * clamped * (3f - 2f * clamped)
|
||||||
}
|
}
|
||||||
|
|
||||||
private class PathPoint(
|
|
||||||
val posVec: PointF = PointF(0f, 0f),
|
|
||||||
val tanVec: PointF = PointF(1f, 0f),
|
|
||||||
) {
|
|
||||||
fun reset() {
|
|
||||||
posVec.x = 0f
|
|
||||||
posVec.y = 0f
|
|
||||||
tanVec.x = 1f
|
|
||||||
tanVec.y = 0f
|
|
||||||
}
|
|
||||||
|
|
||||||
fun translate(dx: Float, dy: Float) {
|
|
||||||
posVec.x += dx
|
|
||||||
posVec.y += dy
|
|
||||||
}
|
|
||||||
|
|
||||||
fun scale(sx: Float, sy: Float, pivotY: Float = 0f) {
|
|
||||||
posVec.x *= sx
|
|
||||||
posVec.y = (posVec.y - pivotY) * sy + pivotY
|
|
||||||
tanVec.x *= sx
|
|
||||||
tanVec.y *= sy
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun markDirty() {
|
private fun markDirty() {
|
||||||
cachedWavelength = -1
|
cachedWavelength = -1
|
||||||
cachedTrackLength = -1f
|
cachedTrackLength = -1f
|
||||||
@@ -771,7 +712,6 @@ constructor(
|
|||||||
trackLength: Float,
|
trackLength: Float,
|
||||||
start: Float,
|
start: Float,
|
||||||
end: Float,
|
end: Float,
|
||||||
amplitudeFraction: Float,
|
|
||||||
phaseFraction: Float,
|
phaseFraction: Float,
|
||||||
baseTranslationX: Float,
|
baseTranslationX: Float,
|
||||||
baseTranslationY: Float,
|
baseTranslationY: Float,
|
||||||
@@ -806,128 +746,26 @@ constructor(
|
|||||||
val endDistance = adjustedEnd * pathMeasure.length
|
val endDistance = adjustedEnd * pathMeasure.length
|
||||||
pathMeasure.getSegment(startDistance, endDistance, displayedWavePath, true)
|
pathMeasure.getSegment(startDistance, endDistance, displayedWavePath, true)
|
||||||
|
|
||||||
startPoint.reset()
|
|
||||||
val startPosArr = floatArrayOf(startPoint.posVec.x, startPoint.posVec.y)
|
|
||||||
val startTanArr = floatArrayOf(startPoint.tanVec.x, startPoint.tanVec.y)
|
|
||||||
pathMeasure.getPosTan(
|
|
||||||
startDistance,
|
|
||||||
startPosArr,
|
|
||||||
startTanArr
|
|
||||||
)
|
|
||||||
startPoint.posVec.x = startPosArr[0]
|
|
||||||
startPoint.posVec.y = startPosArr[1]
|
|
||||||
startPoint.tanVec.x = startTanArr[0]
|
|
||||||
startPoint.tanVec.y = startTanArr[1]
|
|
||||||
|
|
||||||
endPoint.reset()
|
|
||||||
val endPosArr = floatArrayOf(endPoint.posVec.x, endPoint.posVec.y)
|
|
||||||
val endTanArr = floatArrayOf(endPoint.tanVec.x, endPoint.tanVec.y)
|
|
||||||
pathMeasure.getPosTan(
|
|
||||||
endDistance,
|
|
||||||
endPosArr,
|
|
||||||
endTanArr
|
|
||||||
)
|
|
||||||
endPoint.posVec.x = endPosArr[0]
|
|
||||||
endPoint.posVec.y = endPosArr[1]
|
|
||||||
endPoint.tanVec.x = endTanArr[0]
|
|
||||||
endPoint.tanVec.y = endTanArr[1]
|
|
||||||
transform.reset()
|
transform.reset()
|
||||||
transform.setTranslate(resultTranslationX, baseTranslationY)
|
transform.setTranslate(resultTranslationX, baseTranslationY)
|
||||||
startPoint.translate(resultTranslationX, baseTranslationY)
|
val scaleY = displayedAmplitude
|
||||||
endPoint.translate(resultTranslationX, baseTranslationY)
|
|
||||||
|
|
||||||
val scaleY = displayedAmplitude * amplitudeFraction
|
|
||||||
if (scaleY > 0f) {
|
if (scaleY > 0f) {
|
||||||
transform.postScale(1f, scaleY, resultTranslationX, baseTranslationY)
|
transform.postScale(1f, scaleY, resultTranslationX, baseTranslationY)
|
||||||
startPoint.scale(1f, scaleY, baseTranslationY)
|
|
||||||
endPoint.scale(1f, scaleY, baseTranslationY)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
displayedWavePath.transform(transform)
|
displayedWavePath.transform(transform)
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun drawRoundedBlock(
|
private fun PointF.reset() {
|
||||||
canvas: Canvas,
|
this.x = 0f
|
||||||
paint: Paint,
|
this.y = 0f
|
||||||
drawCenter: PathPoint,
|
|
||||||
drawWidth: Float,
|
|
||||||
drawHeight: Float,
|
|
||||||
drawCornerSize: Float,
|
|
||||||
clipCenter: PathPoint? = null,
|
|
||||||
clipWidth: Float = 0f,
|
|
||||||
clipHeight: Float = 0f,
|
|
||||||
clipCornerSize: Float = 0f,
|
|
||||||
clipRight: Boolean = false,
|
|
||||||
) {
|
|
||||||
val localDrawHeight = min(drawHeight, displayedTrackThickness)
|
|
||||||
var localClipWidth = clipWidth
|
|
||||||
var localClipHeight = clipHeight
|
|
||||||
var localClipCornerSize = clipCornerSize
|
|
||||||
|
|
||||||
drawRect.set(-drawWidth / 2f, -localDrawHeight / 2f, drawWidth / 2f, localDrawHeight / 2f)
|
|
||||||
paint.style = Paint.Style.FILL
|
|
||||||
canvas.withSave {
|
|
||||||
if (clipCenter != null) {
|
|
||||||
localClipHeight = min(localClipHeight, displayedTrackThickness)
|
|
||||||
localClipCornerSize =
|
|
||||||
min(
|
|
||||||
localClipWidth / 2f,
|
|
||||||
localClipCornerSize * localClipHeight / displayedTrackThickness,
|
|
||||||
)
|
|
||||||
if (clipRight) {
|
|
||||||
val leftEdgeDiff =
|
|
||||||
(clipCenter.posVec.x - localClipCornerSize) -
|
|
||||||
(drawCenter.posVec.x - drawCornerSize)
|
|
||||||
if (leftEdgeDiff > 0f) {
|
|
||||||
clipCenter.translate(-leftEdgeDiff / 2f, 0f)
|
|
||||||
localClipWidth += leftEdgeDiff
|
|
||||||
}
|
|
||||||
patchRect.set(0f, -localDrawHeight / 2f, drawWidth / 2f, localDrawHeight / 2f)
|
|
||||||
} else {
|
|
||||||
val rightEdgeDiff =
|
|
||||||
(clipCenter.posVec.x + localClipCornerSize) -
|
|
||||||
(drawCenter.posVec.x + drawCornerSize)
|
|
||||||
if (rightEdgeDiff < 0f) {
|
|
||||||
clipCenter.translate(-rightEdgeDiff / 2f, 0f)
|
|
||||||
localClipWidth -= rightEdgeDiff
|
|
||||||
}
|
|
||||||
patchRect.set(-drawWidth / 2f, -localDrawHeight / 2f, 0f, localDrawHeight / 2f)
|
|
||||||
}
|
|
||||||
|
|
||||||
clipRect.set(
|
|
||||||
-localClipWidth / 2f,
|
|
||||||
-localClipHeight / 2f,
|
|
||||||
localClipWidth / 2f,
|
|
||||||
localClipHeight / 2f,
|
|
||||||
)
|
|
||||||
translate(clipCenter.posVec.x, clipCenter.posVec.y)
|
|
||||||
rotate(vectorToCanvasRotation(clipCenter.tanVec))
|
|
||||||
roundedRectPath.reset()
|
|
||||||
roundedRectPath.addRoundRect(
|
|
||||||
clipRect,
|
|
||||||
localClipCornerSize,
|
|
||||||
localClipCornerSize,
|
|
||||||
Path.Direction.CCW,
|
|
||||||
)
|
|
||||||
clipPath(roundedRectPath)
|
|
||||||
|
|
||||||
rotate(-vectorToCanvasRotation(clipCenter.tanVec))
|
|
||||||
translate(-clipCenter.posVec.x, -clipCenter.posVec.y)
|
|
||||||
translate(drawCenter.posVec.x, drawCenter.posVec.y)
|
|
||||||
rotate(vectorToCanvasRotation(drawCenter.tanVec))
|
|
||||||
drawRect(patchRect, paint)
|
|
||||||
drawRoundRect(drawRect, drawCornerSize, drawCornerSize, paint)
|
|
||||||
} else {
|
|
||||||
translate(drawCenter.posVec.x, drawCenter.posVec.y)
|
|
||||||
rotate(vectorToCanvasRotation(drawCenter.tanVec))
|
|
||||||
drawRoundRect(drawRect, drawCornerSize, drawCornerSize, paint)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun vectorToCanvasRotation(vector: PointF): Float =
|
private fun PointF.translate(x: Float, y: Float) {
|
||||||
Math.toDegrees(atan2(vector.y, vector.x).toDouble()).toFloat()
|
this.x += x
|
||||||
|
this.y += y
|
||||||
|
}
|
||||||
|
|
||||||
private companion object {
|
private companion object {
|
||||||
const val MIN_SPRING_VISIBLE_CHANGE = 0.001f
|
const val MIN_SPRING_VISIBLE_CHANGE = 0.001f
|
||||||
|
|||||||
@@ -33,6 +33,8 @@
|
|||||||
<item name="tickVisible">false</item>
|
<item name="tickVisible">false</item>
|
||||||
<item name="trackStopIndicatorSize">0dp</item>
|
<item name="trackStopIndicatorSize">0dp</item>
|
||||||
<item name="trackHeight">@dimen/slider_track_height</item>
|
<item name="trackHeight">@dimen/slider_track_height</item>
|
||||||
|
<item name="trackCornerSize">@dimen/slider_track_height</item>
|
||||||
|
<item name="trackInsideCornerSize">@dimen/slider_track_height</item>
|
||||||
<item name="thumbHeight">@dimen/slider_thumb_height</item>
|
<item name="thumbHeight">@dimen/slider_thumb_height</item>
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user