ui: share wave code
This commit is contained in:
+48
-236
@@ -22,11 +22,7 @@ import android.content.Context
|
|||||||
import android.content.res.TypedArray
|
import android.content.res.TypedArray
|
||||||
import android.graphics.Canvas
|
import android.graphics.Canvas
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
import android.graphics.Matrix
|
|
||||||
import android.graphics.Paint
|
import android.graphics.Paint
|
||||||
import android.graphics.Path
|
|
||||||
import android.graphics.PathMeasure
|
|
||||||
import android.graphics.RectF
|
|
||||||
import android.util.AttributeSet
|
import android.util.AttributeSet
|
||||||
import android.util.TypedValue
|
import android.util.TypedValue
|
||||||
import android.view.View
|
import android.view.View
|
||||||
@@ -35,11 +31,13 @@ import androidx.core.graphics.ColorUtils
|
|||||||
import androidx.core.math.MathUtils
|
import androidx.core.math.MathUtils
|
||||||
import androidx.dynamicanimation.animation.FloatValueHolder
|
import androidx.dynamicanimation.animation.FloatValueHolder
|
||||||
import androidx.dynamicanimation.animation.SpringAnimation
|
import androidx.dynamicanimation.animation.SpringAnimation
|
||||||
import androidx.dynamicanimation.animation.SpringForce
|
|
||||||
import com.google.android.material.R as MR
|
import com.google.android.material.R as MR
|
||||||
import com.google.android.material.motion.MotionUtils
|
import com.google.android.material.wave.PathPoint
|
||||||
|
import com.google.android.material.wave.RoundedBlockRenderer
|
||||||
|
import com.google.android.material.wave.WaveMotionUtils
|
||||||
|
import com.google.android.material.wave.WavePathCache
|
||||||
|
import com.google.android.material.wave.applyRampEasing
|
||||||
import kotlin.math.abs
|
import kotlin.math.abs
|
||||||
import kotlin.math.atan2
|
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
@@ -61,18 +59,11 @@ constructor(
|
|||||||
private val trackPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.FILL }
|
private val trackPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.FILL }
|
||||||
private val activePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.FILL }
|
private val activePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.FILL }
|
||||||
|
|
||||||
private val cachedWavePath = Path()
|
private val wavePathCache = WavePathCache()
|
||||||
private val displayedWavePath = Path()
|
private val roundedBlockRenderer = RoundedBlockRenderer()
|
||||||
private val pathMeasure = PathMeasure()
|
|
||||||
private val waveTransform = Matrix()
|
|
||||||
|
|
||||||
// Pre-allocated objects used in drawRoundedBlock.
|
|
||||||
private val startPoint = PathPoint()
|
private val startPoint = PathPoint()
|
||||||
private val endPoint = PathPoint()
|
private val endPoint = PathPoint()
|
||||||
private val drawRect = RectF()
|
|
||||||
private val patchRect = RectF()
|
|
||||||
private val clipRect = RectF()
|
|
||||||
private val roundedRectPath = Path()
|
|
||||||
|
|
||||||
private var trackThicknessPx =
|
private var trackThicknessPx =
|
||||||
context.resources.getDimensionPixelSize(MR.dimen.mtrl_progress_track_thickness)
|
context.resources.getDimensionPixelSize(MR.dimen.mtrl_progress_track_thickness)
|
||||||
@@ -91,10 +82,6 @@ constructor(
|
|||||||
private var trackLength = 0f
|
private var trackLength = 0f
|
||||||
private var totalTrackLengthFraction = 1f
|
private var totalTrackLengthFraction = 1f
|
||||||
|
|
||||||
private var cachedWavelength = -1
|
|
||||||
private var cachedTrackLength = -1f
|
|
||||||
private var adjustedWavelength = 0f
|
|
||||||
|
|
||||||
private var configuredWavelengthPx = 0
|
private var configuredWavelengthPx = 0
|
||||||
private var configuredAmplitudePx = 0
|
private var configuredAmplitudePx = 0
|
||||||
private var configuredSpeedPx = 0
|
private var configuredSpeedPx = 0
|
||||||
@@ -246,7 +233,7 @@ constructor(
|
|||||||
) {
|
) {
|
||||||
if (wavelengthPx > 0) {
|
if (wavelengthPx > 0) {
|
||||||
configuredWavelengthPx = abs(wavelengthPx)
|
configuredWavelengthPx = abs(wavelengthPx)
|
||||||
cachedWavelength = -1
|
wavePathCache.markDirty()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (amplitudePx > 0) {
|
if (amplitudePx > 0) {
|
||||||
@@ -549,7 +536,7 @@ constructor(
|
|||||||
|
|
||||||
if (drawWavyPath) {
|
if (drawWavyPath) {
|
||||||
ensureCachedWavePath(trackLength, configuredWavelengthPx)
|
ensureCachedWavePath(trackLength, configuredWavelengthPx)
|
||||||
if (adjustedWavelength <= EPSILON || pathMeasure.length <= EPSILON) {
|
if (wavePathCache.adjustedWavelength <= EPSILON || wavePathCache.pathMeasure.length <= EPSILON) {
|
||||||
drawWavyPath = false
|
drawWavyPath = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -569,7 +556,7 @@ constructor(
|
|||||||
amplitudeFraction = amplitudeFraction,
|
amplitudeFraction = amplitudeFraction,
|
||||||
phaseFraction = phaseFraction,
|
phaseFraction = phaseFraction,
|
||||||
)
|
)
|
||||||
canvas.drawPath(displayedWavePath, paint)
|
canvas.drawPath(wavePathCache.displayedWavePath, paint)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!useStrokeCap) {
|
if (!useStrokeCap) {
|
||||||
@@ -602,99 +589,29 @@ constructor(
|
|||||||
amplitudeFraction: Float,
|
amplitudeFraction: Float,
|
||||||
phaseFraction: Float,
|
phaseFraction: Float,
|
||||||
) {
|
) {
|
||||||
displayedWavePath.rewind()
|
wavePathCache.calculateDisplayedWavePath(
|
||||||
if (pathMeasure.length <= EPSILON) {
|
trackLength = trackLength,
|
||||||
return
|
start = start,
|
||||||
}
|
end = end,
|
||||||
|
amplitudeFraction = amplitudeFraction,
|
||||||
var adjustedStart = start
|
phaseFraction = phaseFraction,
|
||||||
var adjustedEnd = end
|
displayedAmplitude = displayedAmplitude,
|
||||||
var trackStartX = -trackLength / 2f
|
startPoint = startPoint,
|
||||||
|
endPoint = endPoint,
|
||||||
if (adjustedWavelength > EPSILON) {
|
baseTranslationX = -trackLength / 2f,
|
||||||
val cycleCount = trackLength / adjustedWavelength
|
baseTranslationY = 0f,
|
||||||
if (cycleCount > EPSILON) {
|
scaleAroundPivotY = false,
|
||||||
val phaseFractionInPath = phaseFraction / cycleCount
|
clampSegmentFractions = false,
|
||||||
val ratio = cycleCount / (cycleCount + 1f)
|
epsilon = EPSILON,
|
||||||
adjustedStart = (adjustedStart + phaseFractionInPath) * ratio
|
)
|
||||||
adjustedEnd = (adjustedEnd + phaseFractionInPath) * ratio
|
|
||||||
}
|
|
||||||
trackStartX -= phaseFraction * adjustedWavelength
|
|
||||||
}
|
|
||||||
|
|
||||||
val startDistance = adjustedStart * pathMeasure.length
|
|
||||||
val endDistance = adjustedEnd * pathMeasure.length
|
|
||||||
pathMeasure.getSegment(startDistance, endDistance, displayedWavePath, true)
|
|
||||||
|
|
||||||
startPoint.reset()
|
|
||||||
pathMeasure.getPosTan(startDistance, startPoint.posVec, startPoint.tanVec)
|
|
||||||
endPoint.reset()
|
|
||||||
pathMeasure.getPosTan(endDistance, endPoint.posVec, endPoint.tanVec)
|
|
||||||
|
|
||||||
waveTransform.reset()
|
|
||||||
waveTransform.setTranslate(trackStartX, 0f)
|
|
||||||
startPoint.translate(trackStartX, 0f)
|
|
||||||
endPoint.translate(trackStartX, 0f)
|
|
||||||
|
|
||||||
val scaledAmplitude = displayedAmplitude * amplitudeFraction
|
|
||||||
waveTransform.postScale(1f, scaledAmplitude)
|
|
||||||
startPoint.scale(1f, scaledAmplitude)
|
|
||||||
endPoint.scale(1f, scaledAmplitude)
|
|
||||||
|
|
||||||
displayedWavePath.transform(waveTransform)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun ensureCachedWavePath(trackLength: Float, wavelength: Int) {
|
private fun ensureCachedWavePath(trackLength: Float, wavelength: Int) {
|
||||||
if (
|
wavePathCache.ensureCachedWavePath(trackLength, wavelength)
|
||||||
cachedTrackLength == trackLength &&
|
|
||||||
cachedWavelength == wavelength &&
|
|
||||||
adjustedWavelength > 0f
|
|
||||||
) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cachedTrackLength = trackLength
|
|
||||||
cachedWavelength = wavelength
|
|
||||||
invalidateCachedWavePath(trackLength, wavelength)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun invalidateCachedWavePath(trackLength: Float, wavelength: Int) {
|
private fun invalidateCachedWavePath(trackLength: Float, wavelength: Int) {
|
||||||
cachedWavePath.rewind()
|
wavePathCache.invalidateCachedWavePath(trackLength, wavelength)
|
||||||
adjustedWavelength = 0f
|
|
||||||
|
|
||||||
if (trackLength <= 0f || wavelength <= 0) {
|
|
||||||
pathMeasure.setPath(cachedWavePath, false)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
val cycleCount = max(1, (trackLength / wavelength).toInt())
|
|
||||||
adjustedWavelength = trackLength / cycleCount
|
|
||||||
|
|
||||||
for (i in 0..cycleCount) {
|
|
||||||
val cycle = i.toFloat()
|
|
||||||
cachedWavePath.cubicTo(
|
|
||||||
2 * cycle + WAVE_SMOOTHNESS,
|
|
||||||
0f,
|
|
||||||
2 * cycle + 1 - WAVE_SMOOTHNESS,
|
|
||||||
1f,
|
|
||||||
2 * cycle + 1,
|
|
||||||
1f,
|
|
||||||
)
|
|
||||||
cachedWavePath.cubicTo(
|
|
||||||
2 * cycle + 1 + WAVE_SMOOTHNESS,
|
|
||||||
1f,
|
|
||||||
2 * cycle + 2 - WAVE_SMOOTHNESS,
|
|
||||||
0f,
|
|
||||||
2 * cycle + 2,
|
|
||||||
0f,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
waveTransform.reset()
|
|
||||||
waveTransform.setScale(adjustedWavelength / 2f, -2f)
|
|
||||||
waveTransform.postTranslate(0f, 1f)
|
|
||||||
cachedWavePath.transform(waveTransform)
|
|
||||||
pathMeasure.setPath(cachedWavePath, false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun canAnimateWave(): Boolean {
|
private fun canAnimateWave(): Boolean {
|
||||||
@@ -778,11 +695,6 @@ constructor(
|
|||||||
return fullyRounded && sameInnerOuter
|
return fullyRounded && sameInnerOuter
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun applyRampEasing(fraction: Float): Float {
|
|
||||||
val clamped = fraction.coerceIn(0f, 1f)
|
|
||||||
return clamped * clamped * (3f - 2f * clamped)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun drawRoundedBlock(
|
private fun drawRoundedBlock(
|
||||||
canvas: Canvas,
|
canvas: Canvas,
|
||||||
paint: Paint,
|
paint: Paint,
|
||||||
@@ -791,18 +703,14 @@ constructor(
|
|||||||
drawHeight: Float,
|
drawHeight: Float,
|
||||||
drawCornerSize: Float,
|
drawCornerSize: Float,
|
||||||
) {
|
) {
|
||||||
drawRoundedBlock(
|
roundedBlockRenderer.drawRoundedBlock(
|
||||||
canvas = canvas,
|
canvas = canvas,
|
||||||
paint = paint,
|
paint = paint,
|
||||||
drawCenter = drawCenter,
|
drawCenter = drawCenter,
|
||||||
drawWidth = drawWidth,
|
drawWidth = drawWidth,
|
||||||
drawHeight = drawHeight,
|
drawHeight = drawHeight,
|
||||||
|
displayedTrackThickness = displayedTrackThickness,
|
||||||
drawCornerSize = drawCornerSize,
|
drawCornerSize = drawCornerSize,
|
||||||
clipCenter = null,
|
|
||||||
clipWidth = 0f,
|
|
||||||
clipHeight = 0f,
|
|
||||||
clipCornerSize = 0f,
|
|
||||||
clipRight = false,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -819,80 +727,22 @@ constructor(
|
|||||||
clipCornerSize: Float,
|
clipCornerSize: Float,
|
||||||
clipRight: Boolean,
|
clipRight: Boolean,
|
||||||
) {
|
) {
|
||||||
var localDrawHeight = min(drawHeight, displayedTrackThickness)
|
roundedBlockRenderer.drawRoundedBlock(
|
||||||
var localClipWidth = clipWidth
|
canvas = canvas,
|
||||||
var localClipHeight = clipHeight
|
paint = paint,
|
||||||
var localClipCornerSize = clipCornerSize
|
drawCenter = drawCenter,
|
||||||
|
drawWidth = drawWidth,
|
||||||
drawRect.set(-drawWidth / 2f, -localDrawHeight / 2f, drawWidth / 2f, localDrawHeight / 2f)
|
drawHeight = drawHeight,
|
||||||
paint.style = Paint.Style.FILL
|
displayedTrackThickness = displayedTrackThickness,
|
||||||
|
drawCornerSize = drawCornerSize,
|
||||||
canvas.save()
|
clipCenter = clipCenter,
|
||||||
if (clipCenter != null) {
|
clipWidth = clipWidth,
|
||||||
localClipHeight = min(localClipHeight, displayedTrackThickness)
|
clipHeight = clipHeight,
|
||||||
localClipCornerSize =
|
clipCornerSize = clipCornerSize,
|
||||||
min(
|
clipRight = clipRight,
|
||||||
localClipWidth / 2f,
|
)
|
||||||
localClipCornerSize * localClipHeight / displayedTrackThickness,
|
|
||||||
)
|
|
||||||
|
|
||||||
if (clipRight) {
|
|
||||||
val leftEdgeDiff =
|
|
||||||
(clipCenter.posVec[0] - localClipCornerSize) -
|
|
||||||
(drawCenter.posVec[0] - 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[0] + localClipCornerSize) -
|
|
||||||
(drawCenter.posVec[0] + 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,
|
|
||||||
)
|
|
||||||
|
|
||||||
canvas.translate(clipCenter.posVec[0], clipCenter.posVec[1])
|
|
||||||
canvas.rotate(vectorToCanvasRotation(clipCenter.tanVec))
|
|
||||||
roundedRectPath.reset()
|
|
||||||
roundedRectPath.addRoundRect(
|
|
||||||
clipRect,
|
|
||||||
localClipCornerSize,
|
|
||||||
localClipCornerSize,
|
|
||||||
Path.Direction.CCW,
|
|
||||||
)
|
|
||||||
canvas.clipPath(roundedRectPath)
|
|
||||||
|
|
||||||
canvas.rotate(-vectorToCanvasRotation(clipCenter.tanVec))
|
|
||||||
canvas.translate(-clipCenter.posVec[0], -clipCenter.posVec[1])
|
|
||||||
|
|
||||||
canvas.translate(drawCenter.posVec[0], drawCenter.posVec[1])
|
|
||||||
canvas.rotate(vectorToCanvasRotation(drawCenter.tanVec))
|
|
||||||
canvas.drawRect(patchRect, paint)
|
|
||||||
canvas.drawRoundRect(drawRect, drawCornerSize, drawCornerSize, paint)
|
|
||||||
} else {
|
|
||||||
canvas.translate(drawCenter.posVec[0], drawCenter.posVec[1])
|
|
||||||
canvas.rotate(vectorToCanvasRotation(drawCenter.tanVec))
|
|
||||||
canvas.drawRoundRect(drawRect, drawCornerSize, drawCornerSize, paint)
|
|
||||||
}
|
|
||||||
|
|
||||||
canvas.restore()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun vectorToCanvasRotation(vector: FloatArray): Float =
|
|
||||||
Math.toDegrees(atan2(vector[1], vector[0]).toDouble()).toFloat()
|
|
||||||
|
|
||||||
private fun transitionAmplitudeTo(target: Float, onFinished: (() -> Unit)? = null) {
|
private fun transitionAmplitudeTo(target: Float, onFinished: (() -> Unit)? = null) {
|
||||||
val clampedTarget = MathUtils.clamp(target, 0f, 1f)
|
val clampedTarget = MathUtils.clamp(target, 0f, 1f)
|
||||||
if (abs(currentAmplitudeFraction - clampedTarget) < EPSILON) {
|
if (abs(currentAmplitudeFraction - clampedTarget) < EPSILON) {
|
||||||
@@ -908,7 +758,11 @@ constructor(
|
|||||||
|
|
||||||
val springAnimation =
|
val springAnimation =
|
||||||
SpringAnimation(FloatValueHolder(currentAmplitudeFraction)).apply {
|
SpringAnimation(FloatValueHolder(currentAmplitudeFraction)).apply {
|
||||||
spring = resolveWaveTransitionSpring(clampedTarget)
|
spring =
|
||||||
|
WaveMotionUtils.resolveWaveTransitionSpring(
|
||||||
|
context = context,
|
||||||
|
animateOn = clampedTarget > currentAmplitudeFraction,
|
||||||
|
)
|
||||||
setStartValue(currentAmplitudeFraction)
|
setStartValue(currentAmplitudeFraction)
|
||||||
setMinimumVisibleChange(MIN_SPRING_VISIBLE_CHANGE)
|
setMinimumVisibleChange(MIN_SPRING_VISIBLE_CHANGE)
|
||||||
addUpdateListener { _, value, _ ->
|
addUpdateListener { _, value, _ ->
|
||||||
@@ -1015,23 +869,6 @@ constructor(
|
|||||||
lastPhaseFrameNanos = 0L
|
lastPhaseFrameNanos = 0L
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun resolveWaveTransitionSpring(target: Float): SpringForce {
|
|
||||||
val animateOn = target > currentAmplitudeFraction
|
|
||||||
val attr =
|
|
||||||
if (animateOn) {
|
|
||||||
MR.attr.motionSpringFastEffects
|
|
||||||
} else {
|
|
||||||
MR.attr.motionSpringDefaultEffects
|
|
||||||
}
|
|
||||||
val defaultStyle =
|
|
||||||
if (animateOn) {
|
|
||||||
MR.style.Motion_Material3_Spring_Standard_Fast_Effects
|
|
||||||
} else {
|
|
||||||
MR.style.Motion_Material3_Spring_Standard_Default_Effects
|
|
||||||
}
|
|
||||||
return MotionUtils.resolveThemeSpringForce(context, attr, defaultStyle)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun resolveInnerCornerRadius(linearAttrs: TypedArray): Float {
|
private fun resolveInnerCornerRadius(linearAttrs: TypedArray): Float {
|
||||||
val typedValue =
|
val typedValue =
|
||||||
linearAttrs.peekValue(MR.styleable.LinearProgressIndicator_trackInnerCornerRadius)
|
linearAttrs.peekValue(MR.styleable.LinearProgressIndicator_trackInnerCornerRadius)
|
||||||
@@ -1072,32 +909,7 @@ constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private data class PathPoint(
|
|
||||||
val posVec: FloatArray = floatArrayOf(0f, 0f),
|
|
||||||
val tanVec: FloatArray = floatArrayOf(1f, 0f),
|
|
||||||
) {
|
|
||||||
fun reset() {
|
|
||||||
posVec[0] = 0f
|
|
||||||
posVec[1] = 0f
|
|
||||||
tanVec[0] = 1f
|
|
||||||
tanVec[1] = 0f
|
|
||||||
}
|
|
||||||
|
|
||||||
fun translate(dx: Float, dy: Float) {
|
|
||||||
posVec[0] += dx
|
|
||||||
posVec[1] += dy
|
|
||||||
}
|
|
||||||
|
|
||||||
fun scale(sx: Float, sy: Float) {
|
|
||||||
posVec[0] *= sx
|
|
||||||
posVec[1] *= sy
|
|
||||||
tanVec[0] *= sx
|
|
||||||
tanVec[1] *= sy
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private companion object {
|
private companion object {
|
||||||
const val WAVE_SMOOTHNESS = 0.48f
|
|
||||||
const val MIN_SPRING_VISIBLE_CHANGE = 0.001f
|
const val MIN_SPRING_VISIBLE_CHANGE = 0.001f
|
||||||
const val MIN_PHASE_FRACTION = 0.0001f
|
const val MIN_PHASE_FRACTION = 0.0001f
|
||||||
const val EPSILON = 0.0001f
|
const val EPSILON = 0.0001f
|
||||||
|
|||||||
@@ -22,22 +22,20 @@ import android.content.Context
|
|||||||
import android.content.res.ColorStateList
|
import android.content.res.ColorStateList
|
||||||
import android.graphics.Canvas
|
import android.graphics.Canvas
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
import android.graphics.Matrix
|
|
||||||
import android.graphics.Paint
|
import android.graphics.Paint
|
||||||
import android.graphics.Path
|
|
||||||
import android.graphics.PathMeasure
|
|
||||||
import android.graphics.RectF
|
|
||||||
import android.util.AttributeSet
|
import android.util.AttributeSet
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import androidx.annotation.Px
|
import androidx.annotation.Px
|
||||||
import androidx.dynamicanimation.animation.FloatValueHolder
|
import androidx.dynamicanimation.animation.FloatValueHolder
|
||||||
import androidx.dynamicanimation.animation.SpringAnimation
|
import androidx.dynamicanimation.animation.SpringAnimation
|
||||||
import androidx.dynamicanimation.animation.SpringForce
|
|
||||||
import com.google.android.material.R as MR
|
import com.google.android.material.R as MR
|
||||||
import com.google.android.material.motion.MotionUtils
|
|
||||||
import com.google.android.material.progressindicator.PatchedLinearProgressIndicator
|
import com.google.android.material.progressindicator.PatchedLinearProgressIndicator
|
||||||
|
import com.google.android.material.wave.PathPoint
|
||||||
|
import com.google.android.material.wave.RoundedBlockRenderer
|
||||||
|
import com.google.android.material.wave.WaveMotionUtils
|
||||||
|
import com.google.android.material.wave.WavePathCache
|
||||||
|
import com.google.android.material.wave.applyRampEasing
|
||||||
import kotlin.math.abs
|
import kotlin.math.abs
|
||||||
import kotlin.math.atan2
|
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
|
|
||||||
@@ -59,23 +57,11 @@ constructor(
|
|||||||
strokeCap = Paint.Cap.BUTT
|
strokeCap = Paint.Cap.BUTT
|
||||||
}
|
}
|
||||||
|
|
||||||
private val cachedWavePath = Path()
|
private val wavePathCache = WavePathCache()
|
||||||
private val displayedWavePath = Path()
|
private val roundedBlockRenderer = RoundedBlockRenderer()
|
||||||
private val pathMeasure = PathMeasure()
|
|
||||||
private val transform = Matrix()
|
|
||||||
|
|
||||||
// Pre-allocates objects used in draw.
|
|
||||||
private val startPoint = PathPoint()
|
private val startPoint = PathPoint()
|
||||||
private val endPoint = PathPoint()
|
private val endPoint = PathPoint()
|
||||||
private val cornerRect = RectF()
|
|
||||||
private val patchRect = RectF()
|
|
||||||
private val clipRect = RectF()
|
|
||||||
private val drawRect = RectF()
|
|
||||||
private val roundedRectPath = Path()
|
|
||||||
|
|
||||||
private var cachedWavelength = -1
|
|
||||||
private var cachedTrackLength = -1f
|
|
||||||
private var adjustedWavelength = 0f
|
|
||||||
|
|
||||||
private val transparentTrackTint = ColorStateList.valueOf(Color.TRANSPARENT)
|
private val transparentTrackTint = ColorStateList.valueOf(Color.TRANSPARENT)
|
||||||
private var waveTrackTintList: ColorStateList = trackActiveTintList
|
private var waveTrackTintList: ColorStateList = trackActiveTintList
|
||||||
@@ -162,7 +148,7 @@ constructor(
|
|||||||
if (sanitized > 0) {
|
if (sanitized > 0) {
|
||||||
configuredWavelengthPx = sanitized
|
configuredWavelengthPx = sanitized
|
||||||
}
|
}
|
||||||
cachedWavelength = -1
|
wavePathCache.markDirty()
|
||||||
updateActiveTrackSuppression()
|
updateActiveTrackSuppression()
|
||||||
ensurePhaseTickerState()
|
ensurePhaseTickerState()
|
||||||
invalidate()
|
invalidate()
|
||||||
@@ -292,7 +278,7 @@ constructor(
|
|||||||
displayedAmplitude = waveAmplitude.toFloat()
|
displayedAmplitude = waveAmplitude.toFloat()
|
||||||
|
|
||||||
ensureCachedWavePath(trackLength, wavelengthDeterminate)
|
ensureCachedWavePath(trackLength, wavelengthDeterminate)
|
||||||
if (pathMeasure.length <= 0f || adjustedWavelength <= 0f) {
|
if (wavePathCache.pathMeasure.length <= 0f || wavePathCache.adjustedWavelength <= 0f) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -450,7 +436,7 @@ constructor(
|
|||||||
phaseFraction = phaseFraction,
|
phaseFraction = phaseFraction,
|
||||||
trackCenterY = trackCenterY,
|
trackCenterY = trackCenterY,
|
||||||
)
|
)
|
||||||
canvas.drawPath(displayedWavePath, wavePaint)
|
canvas.drawPath(wavePathCache.displayedWavePath, wavePaint)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!useStrokeCap()) {
|
if (!useStrokeCap()) {
|
||||||
@@ -483,94 +469,29 @@ constructor(
|
|||||||
phaseFraction: Float,
|
phaseFraction: Float,
|
||||||
trackCenterY: Float,
|
trackCenterY: Float,
|
||||||
) {
|
) {
|
||||||
var adjustedStart = start
|
wavePathCache.calculateDisplayedWavePath(
|
||||||
var adjustedEnd = end
|
trackLength = trackLength,
|
||||||
var resultTranslationX = trackSidePadding.toFloat()
|
start = start,
|
||||||
val hasWavyEffect = wavelengthDeterminate > 0 && waveAmplitude > 0
|
end = end,
|
||||||
|
amplitudeFraction = amplitudeFraction,
|
||||||
if (hasWavyEffect) {
|
phaseFraction = phaseFraction,
|
||||||
val cycleCount = trackLength / adjustedWavelength
|
displayedAmplitude = displayedAmplitude,
|
||||||
val phaseFractionInPath = phaseFraction / cycleCount
|
startPoint = startPoint,
|
||||||
val ratio = cycleCount / (cycleCount + 1f)
|
endPoint = endPoint,
|
||||||
adjustedStart = (adjustedStart + phaseFractionInPath) * ratio
|
baseTranslationX = trackSidePadding.toFloat(),
|
||||||
adjustedEnd = (adjustedEnd + phaseFractionInPath) * ratio
|
baseTranslationY = trackCenterY,
|
||||||
resultTranslationX -= phaseFraction * adjustedWavelength
|
scaleAroundPivotY = true,
|
||||||
}
|
clampSegmentFractions = true,
|
||||||
|
epsilon = EPSILON,
|
||||||
val clampedStart = clamp(adjustedStart, 0f, 1f)
|
)
|
||||||
val clampedEnd = clamp(adjustedEnd, 0f, 1f)
|
|
||||||
if (clampedEnd <= clampedStart) {
|
|
||||||
displayedWavePath.rewind()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
displayedWavePath.rewind()
|
|
||||||
val startDistance = clampedStart * pathMeasure.length
|
|
||||||
val endDistance = clampedEnd * pathMeasure.length
|
|
||||||
pathMeasure.getSegment(startDistance, endDistance, displayedWavePath, true)
|
|
||||||
pathMeasure.getPosTan(startDistance, startPoint.posVec, startPoint.tanVec)
|
|
||||||
pathMeasure.getPosTan(endDistance, endPoint.posVec, endPoint.tanVec)
|
|
||||||
|
|
||||||
transform.reset()
|
|
||||||
transform.setTranslate(resultTranslationX, trackCenterY)
|
|
||||||
startPoint.translate(resultTranslationX, trackCenterY)
|
|
||||||
endPoint.translate(resultTranslationX, trackCenterY)
|
|
||||||
if (hasWavyEffect) {
|
|
||||||
val scaleY = displayedAmplitude * amplitudeFraction
|
|
||||||
transform.postScale(1f, scaleY, resultTranslationX, trackCenterY)
|
|
||||||
startPoint.scale(1f, scaleY, trackCenterY)
|
|
||||||
endPoint.scale(1f, scaleY, trackCenterY)
|
|
||||||
}
|
|
||||||
displayedWavePath.transform(transform)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun ensureCachedWavePath(trackLength: Float, wavelength: Int) {
|
private fun ensureCachedWavePath(trackLength: Float, wavelength: Int) {
|
||||||
if (
|
wavePathCache.ensureCachedWavePath(trackLength, wavelength)
|
||||||
cachedTrackLength == trackLength &&
|
|
||||||
cachedWavelength == wavelength &&
|
|
||||||
adjustedWavelength > 0f
|
|
||||||
) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
cachedTrackLength = trackLength
|
|
||||||
cachedWavelength = wavelength
|
|
||||||
invalidateCachedWavePath(trackLength, wavelength)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun invalidateCachedWavePath(trackLength: Float, wavelength: Int) {
|
private fun invalidateCachedWavePath(trackLength: Float, wavelength: Int) {
|
||||||
cachedWavePath.rewind()
|
wavePathCache.invalidateCachedWavePath(trackLength, wavelength)
|
||||||
adjustedWavelength = 0f
|
|
||||||
if (trackLength <= 0f || wavelength <= 0) {
|
|
||||||
pathMeasure.setPath(cachedWavePath, false)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
val cycleCount = max(1, (trackLength / wavelength).toInt())
|
|
||||||
adjustedWavelength = trackLength / cycleCount
|
|
||||||
for (i in 0..cycleCount) {
|
|
||||||
val cycle = i.toFloat()
|
|
||||||
cachedWavePath.cubicTo(
|
|
||||||
2 * cycle + WAVE_SMOOTHNESS,
|
|
||||||
0f,
|
|
||||||
2 * cycle + 1 - WAVE_SMOOTHNESS,
|
|
||||||
1f,
|
|
||||||
2 * cycle + 1,
|
|
||||||
1f,
|
|
||||||
)
|
|
||||||
cachedWavePath.cubicTo(
|
|
||||||
2 * cycle + 1 + WAVE_SMOOTHNESS,
|
|
||||||
1f,
|
|
||||||
2 * cycle + 2 - WAVE_SMOOTHNESS,
|
|
||||||
0f,
|
|
||||||
2 * cycle + 2,
|
|
||||||
0f,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
transform.reset()
|
|
||||||
transform.setScale(adjustedWavelength / 2f, -2f)
|
|
||||||
transform.postTranslate(0f, 1f)
|
|
||||||
cachedWavePath.transform(transform)
|
|
||||||
pathMeasure.setPath(cachedWavePath, false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun drawRoundedBlock(
|
private fun drawRoundedBlock(
|
||||||
@@ -580,17 +501,14 @@ constructor(
|
|||||||
drawHeight: Float,
|
drawHeight: Float,
|
||||||
drawCornerSize: Float,
|
drawCornerSize: Float,
|
||||||
) {
|
) {
|
||||||
drawRoundedBlock(
|
roundedBlockRenderer.drawRoundedBlock(
|
||||||
canvas = canvas,
|
canvas = canvas,
|
||||||
|
paint = wavePaint,
|
||||||
drawCenter = drawCenter,
|
drawCenter = drawCenter,
|
||||||
drawWidth = drawWidth,
|
drawWidth = drawWidth,
|
||||||
drawHeight = drawHeight,
|
drawHeight = drawHeight,
|
||||||
|
displayedTrackThickness = displayedTrackThickness,
|
||||||
drawCornerSize = drawCornerSize,
|
drawCornerSize = drawCornerSize,
|
||||||
clipCenter = null,
|
|
||||||
clipWidth = 0f,
|
|
||||||
clipHeight = 0f,
|
|
||||||
clipCornerSize = 0f,
|
|
||||||
clipRight = false,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -606,75 +524,22 @@ constructor(
|
|||||||
clipCornerSize: Float,
|
clipCornerSize: Float,
|
||||||
clipRight: Boolean,
|
clipRight: Boolean,
|
||||||
) {
|
) {
|
||||||
var localDrawHeight = min(drawHeight, displayedTrackThickness)
|
roundedBlockRenderer.drawRoundedBlock(
|
||||||
var localClipWidth = clipWidth
|
canvas = canvas,
|
||||||
var localClipHeight = clipHeight
|
paint = wavePaint,
|
||||||
var localClipCornerSize = clipCornerSize
|
drawCenter = drawCenter,
|
||||||
|
drawWidth = drawWidth,
|
||||||
drawRect.set(-drawWidth / 2f, -localDrawHeight / 2f, drawWidth / 2f, localDrawHeight / 2f)
|
drawHeight = drawHeight,
|
||||||
wavePaint.style = Paint.Style.FILL
|
displayedTrackThickness = displayedTrackThickness,
|
||||||
canvas.save()
|
drawCornerSize = drawCornerSize,
|
||||||
if (clipCenter != null) {
|
clipCenter = clipCenter,
|
||||||
localClipHeight = min(localClipHeight, displayedTrackThickness)
|
clipWidth = clipWidth,
|
||||||
localClipCornerSize =
|
clipHeight = clipHeight,
|
||||||
min(
|
clipCornerSize = clipCornerSize,
|
||||||
localClipWidth / 2f,
|
clipRight = clipRight,
|
||||||
localClipCornerSize * localClipHeight / displayedTrackThickness,
|
)
|
||||||
)
|
|
||||||
if (clipRight) {
|
|
||||||
val leftEdgeDiff =
|
|
||||||
(clipCenter.posVec[0] - localClipCornerSize) -
|
|
||||||
(drawCenter.posVec[0] - 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[0] + localClipCornerSize) -
|
|
||||||
(drawCenter.posVec[0] + 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,
|
|
||||||
)
|
|
||||||
canvas.translate(clipCenter.posVec[0], clipCenter.posVec[1])
|
|
||||||
canvas.rotate(vectorToCanvasRotation(clipCenter.tanVec))
|
|
||||||
roundedRectPath.reset()
|
|
||||||
roundedRectPath.addRoundRect(
|
|
||||||
clipRect,
|
|
||||||
localClipCornerSize,
|
|
||||||
localClipCornerSize,
|
|
||||||
Path.Direction.CCW,
|
|
||||||
)
|
|
||||||
canvas.clipPath(roundedRectPath)
|
|
||||||
|
|
||||||
canvas.rotate(-vectorToCanvasRotation(clipCenter.tanVec))
|
|
||||||
canvas.translate(-clipCenter.posVec[0], -clipCenter.posVec[1])
|
|
||||||
canvas.translate(drawCenter.posVec[0], drawCenter.posVec[1])
|
|
||||||
canvas.rotate(vectorToCanvasRotation(drawCenter.tanVec))
|
|
||||||
canvas.drawRect(patchRect, wavePaint)
|
|
||||||
canvas.drawRoundRect(drawRect, drawCornerSize, drawCornerSize, wavePaint)
|
|
||||||
} else {
|
|
||||||
canvas.translate(drawCenter.posVec[0], drawCenter.posVec[1])
|
|
||||||
canvas.rotate(vectorToCanvasRotation(drawCenter.tanVec))
|
|
||||||
canvas.drawRoundRect(drawRect, drawCornerSize, drawCornerSize, wavePaint)
|
|
||||||
}
|
|
||||||
canvas.restore()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun vectorToCanvasRotation(vector: FloatArray): Float =
|
|
||||||
Math.toDegrees(atan2(vector[1], vector[0]).toDouble()).toFloat()
|
|
||||||
|
|
||||||
private fun calculateStartTrackCornerSize(thumbTrackPosition: Float): Float {
|
private fun calculateStartTrackCornerSize(thumbTrackPosition: Float): Float {
|
||||||
if (thumbTrackGapSize <= 0) {
|
if (thumbTrackGapSize <= 0) {
|
||||||
return displayedCornerRadius
|
return displayedCornerRadius
|
||||||
@@ -741,11 +606,6 @@ constructor(
|
|||||||
return ((progressFraction - waveRampProgressMin) / span).coerceIn(0f, 1f)
|
return ((progressFraction - waveRampProgressMin) / span).coerceIn(0f, 1f)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun applyRampEasing(fraction: Float): Float {
|
|
||||||
val clamped = fraction.coerceIn(0f, 1f)
|
|
||||||
return clamped * clamped * (3f - 2f * clamped)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun canAnimateWave(): Boolean =
|
private fun canAnimateWave(): Boolean =
|
||||||
configuredWavelengthPx > 0 && configuredAmplitudePx > 0 && configuredSpeedPx != 0
|
configuredWavelengthPx > 0 && configuredAmplitudePx > 0 && configuredSpeedPx != 0
|
||||||
|
|
||||||
@@ -773,7 +633,11 @@ constructor(
|
|||||||
|
|
||||||
val springAnimation =
|
val springAnimation =
|
||||||
SpringAnimation(FloatValueHolder(currentAmplitudeFraction)).apply {
|
SpringAnimation(FloatValueHolder(currentAmplitudeFraction)).apply {
|
||||||
spring = resolveWaveTransitionSpring(clampedTarget)
|
spring =
|
||||||
|
WaveMotionUtils.resolveWaveTransitionSpring(
|
||||||
|
context = context,
|
||||||
|
animateOn = clampedTarget > currentAmplitudeFraction,
|
||||||
|
)
|
||||||
setStartValue(currentAmplitudeFraction)
|
setStartValue(currentAmplitudeFraction)
|
||||||
setMinimumVisibleChange(MIN_SPRING_VISIBLE_CHANGE)
|
setMinimumVisibleChange(MIN_SPRING_VISIBLE_CHANGE)
|
||||||
addUpdateListener { _, value, _ ->
|
addUpdateListener { _, value, _ ->
|
||||||
@@ -873,23 +737,6 @@ constructor(
|
|||||||
lastPhaseFrameNanos = 0L
|
lastPhaseFrameNanos = 0L
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun resolveWaveTransitionSpring(target: Float): SpringForce {
|
|
||||||
val animateOn = target > currentAmplitudeFraction
|
|
||||||
val attr =
|
|
||||||
if (animateOn) {
|
|
||||||
MR.attr.motionSpringFastEffects
|
|
||||||
} else {
|
|
||||||
MR.attr.motionSpringDefaultEffects
|
|
||||||
}
|
|
||||||
val defaultStyle =
|
|
||||||
if (animateOn) {
|
|
||||||
MR.style.Motion_Material3_Spring_Standard_Fast_Effects
|
|
||||||
} else {
|
|
||||||
MR.style.Motion_Material3_Spring_Standard_Default_Effects
|
|
||||||
}
|
|
||||||
return MotionUtils.resolveThemeSpringForce(context, attr, defaultStyle)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun shouldDrawWave(): Boolean =
|
private fun shouldDrawWave(): Boolean =
|
||||||
waveAmplitude > 0 &&
|
waveAmplitude > 0 &&
|
||||||
wavelengthDeterminate > 0 &&
|
wavelengthDeterminate > 0 &&
|
||||||
@@ -913,49 +760,16 @@ constructor(
|
|||||||
linearActiveTrackSuppressed = suppress
|
linearActiveTrackSuppressed = suppress
|
||||||
}
|
}
|
||||||
|
|
||||||
private data class PathPoint(
|
|
||||||
val posVec: FloatArray = floatArrayOf(0f, 0f),
|
|
||||||
val tanVec: FloatArray = floatArrayOf(1f, 0f),
|
|
||||||
) {
|
|
||||||
fun reset() {
|
|
||||||
posVec[0] = 0f
|
|
||||||
posVec[1] = 0f
|
|
||||||
tanVec[0] = 1f
|
|
||||||
tanVec[1] = 0f
|
|
||||||
}
|
|
||||||
|
|
||||||
fun translate(dx: Float, dy: Float) {
|
|
||||||
posVec[0] += dx
|
|
||||||
posVec[1] += dy
|
|
||||||
}
|
|
||||||
|
|
||||||
fun scale(sx: Float, sy: Float, pivotY: Float) {
|
|
||||||
posVec[0] *= sx
|
|
||||||
posVec[1] = (posVec[1] - pivotY) * sy + pivotY
|
|
||||||
tanVec[0] *= sx
|
|
||||||
tanVec[1] *= sy
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private enum class ActiveTrackDirection {
|
private enum class ActiveTrackDirection {
|
||||||
LEFT,
|
LEFT,
|
||||||
RIGHT,
|
RIGHT,
|
||||||
}
|
}
|
||||||
|
|
||||||
private companion object {
|
private companion object {
|
||||||
const val WAVE_SMOOTHNESS = 0.48f
|
|
||||||
const val MIN_SPRING_VISIBLE_CHANGE = 0.001f
|
const val MIN_SPRING_VISIBLE_CHANGE = 0.001f
|
||||||
const val DEFAULT_WAVE_RAMP_PROGRESS_MAX = 0.03f
|
const val DEFAULT_WAVE_RAMP_PROGRESS_MAX = 0.03f
|
||||||
const val MIN_VISIBLE_WAVE_FRACTION = 0.001f
|
const val MIN_VISIBLE_WAVE_FRACTION = 0.001f
|
||||||
const val MIN_PHASE_FRACTION = 0.0001f
|
const val MIN_PHASE_FRACTION = 0.0001f
|
||||||
const val EPSILON = 0.0001f
|
const val EPSILON = 0.0001f
|
||||||
|
|
||||||
fun clamp(value: Float, min: Float, max: Float): Float {
|
|
||||||
return when {
|
|
||||||
value < min -> min
|
|
||||||
value > max -> max
|
|
||||||
else -> value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,314 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2026 Auxio Project
|
||||||
|
* WaveRenderHelpers.kt is part of Auxio.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.google.android.material.wave
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.graphics.Canvas
|
||||||
|
import android.graphics.Matrix
|
||||||
|
import android.graphics.Paint
|
||||||
|
import android.graphics.Path
|
||||||
|
import android.graphics.PathMeasure
|
||||||
|
import android.graphics.RectF
|
||||||
|
import androidx.dynamicanimation.animation.SpringForce
|
||||||
|
import com.google.android.material.R as MR
|
||||||
|
import com.google.android.material.motion.MotionUtils
|
||||||
|
import kotlin.math.atan2
|
||||||
|
import kotlin.math.max
|
||||||
|
import kotlin.math.min
|
||||||
|
|
||||||
|
internal data class PathPoint(
|
||||||
|
val posVec: FloatArray = floatArrayOf(0f, 0f),
|
||||||
|
val tanVec: FloatArray = floatArrayOf(1f, 0f),
|
||||||
|
) {
|
||||||
|
fun reset() {
|
||||||
|
posVec[0] = 0f
|
||||||
|
posVec[1] = 0f
|
||||||
|
tanVec[0] = 1f
|
||||||
|
tanVec[1] = 0f
|
||||||
|
}
|
||||||
|
|
||||||
|
fun translate(dx: Float, dy: Float) {
|
||||||
|
posVec[0] += dx
|
||||||
|
posVec[1] += dy
|
||||||
|
}
|
||||||
|
|
||||||
|
fun scale(sx: Float, sy: Float, pivotY: Float = 0f) {
|
||||||
|
posVec[0] *= sx
|
||||||
|
posVec[1] = (posVec[1] - pivotY) * sy + pivotY
|
||||||
|
tanVec[0] *= sx
|
||||||
|
tanVec[1] *= sy
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class WavePathCache {
|
||||||
|
val displayedWavePath = Path()
|
||||||
|
val pathMeasure = PathMeasure()
|
||||||
|
var adjustedWavelength = 0f
|
||||||
|
private set
|
||||||
|
|
||||||
|
private val cachedWavePath = Path()
|
||||||
|
private val transform = Matrix()
|
||||||
|
private var cachedWavelength = -1
|
||||||
|
private var cachedTrackLength = -1f
|
||||||
|
|
||||||
|
fun markDirty() {
|
||||||
|
cachedWavelength = -1
|
||||||
|
cachedTrackLength = -1f
|
||||||
|
adjustedWavelength = 0f
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ensureCachedWavePath(trackLength: Float, wavelength: Int) {
|
||||||
|
if (
|
||||||
|
cachedTrackLength == trackLength &&
|
||||||
|
cachedWavelength == wavelength &&
|
||||||
|
adjustedWavelength > 0f
|
||||||
|
) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cachedTrackLength = trackLength
|
||||||
|
cachedWavelength = wavelength
|
||||||
|
invalidateCachedWavePath(trackLength, wavelength)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun invalidateCachedWavePath(trackLength: Float, wavelength: Int) {
|
||||||
|
cachedWavePath.rewind()
|
||||||
|
adjustedWavelength = 0f
|
||||||
|
if (trackLength <= 0f || wavelength <= 0) {
|
||||||
|
pathMeasure.setPath(cachedWavePath, false)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val cycleCount = max(1, (trackLength / wavelength).toInt())
|
||||||
|
adjustedWavelength = trackLength / cycleCount
|
||||||
|
for (i in 0..cycleCount) {
|
||||||
|
val cycle = i.toFloat()
|
||||||
|
cachedWavePath.cubicTo(
|
||||||
|
2 * cycle + WAVE_SMOOTHNESS,
|
||||||
|
0f,
|
||||||
|
2 * cycle + 1 - WAVE_SMOOTHNESS,
|
||||||
|
1f,
|
||||||
|
2 * cycle + 1,
|
||||||
|
1f,
|
||||||
|
)
|
||||||
|
cachedWavePath.cubicTo(
|
||||||
|
2 * cycle + 1 + WAVE_SMOOTHNESS,
|
||||||
|
1f,
|
||||||
|
2 * cycle + 2 - WAVE_SMOOTHNESS,
|
||||||
|
0f,
|
||||||
|
2 * cycle + 2,
|
||||||
|
0f,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
transform.reset()
|
||||||
|
transform.setScale(adjustedWavelength / 2f, -2f)
|
||||||
|
transform.postTranslate(0f, 1f)
|
||||||
|
cachedWavePath.transform(transform)
|
||||||
|
pathMeasure.setPath(cachedWavePath, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun calculateDisplayedWavePath(
|
||||||
|
trackLength: Float,
|
||||||
|
start: Float,
|
||||||
|
end: Float,
|
||||||
|
amplitudeFraction: Float,
|
||||||
|
phaseFraction: Float,
|
||||||
|
displayedAmplitude: Float,
|
||||||
|
startPoint: PathPoint,
|
||||||
|
endPoint: PathPoint,
|
||||||
|
baseTranslationX: Float,
|
||||||
|
baseTranslationY: Float,
|
||||||
|
scaleAroundPivotY: Boolean,
|
||||||
|
clampSegmentFractions: Boolean,
|
||||||
|
epsilon: Float,
|
||||||
|
): Boolean {
|
||||||
|
displayedWavePath.rewind()
|
||||||
|
if (pathMeasure.length <= epsilon) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
var adjustedStart = start
|
||||||
|
var adjustedEnd = end
|
||||||
|
var resultTranslationX = baseTranslationX
|
||||||
|
|
||||||
|
if (adjustedWavelength > epsilon) {
|
||||||
|
val cycleCount = trackLength / adjustedWavelength
|
||||||
|
if (cycleCount > epsilon) {
|
||||||
|
val phaseFractionInPath = phaseFraction / cycleCount
|
||||||
|
val ratio = cycleCount / (cycleCount + 1f)
|
||||||
|
adjustedStart = (adjustedStart + phaseFractionInPath) * ratio
|
||||||
|
adjustedEnd = (adjustedEnd + phaseFractionInPath) * ratio
|
||||||
|
}
|
||||||
|
resultTranslationX -= phaseFraction * adjustedWavelength
|
||||||
|
}
|
||||||
|
|
||||||
|
if (clampSegmentFractions) {
|
||||||
|
adjustedStart = adjustedStart.coerceIn(0f, 1f)
|
||||||
|
adjustedEnd = adjustedEnd.coerceIn(0f, 1f)
|
||||||
|
if (adjustedEnd <= adjustedStart) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val startDistance = adjustedStart * pathMeasure.length
|
||||||
|
val endDistance = adjustedEnd * pathMeasure.length
|
||||||
|
pathMeasure.getSegment(startDistance, endDistance, displayedWavePath, true)
|
||||||
|
|
||||||
|
startPoint.reset()
|
||||||
|
pathMeasure.getPosTan(startDistance, startPoint.posVec, startPoint.tanVec)
|
||||||
|
endPoint.reset()
|
||||||
|
pathMeasure.getPosTan(endDistance, endPoint.posVec, endPoint.tanVec)
|
||||||
|
|
||||||
|
transform.reset()
|
||||||
|
transform.setTranslate(resultTranslationX, baseTranslationY)
|
||||||
|
startPoint.translate(resultTranslationX, baseTranslationY)
|
||||||
|
endPoint.translate(resultTranslationX, baseTranslationY)
|
||||||
|
|
||||||
|
val scaleY = displayedAmplitude * amplitudeFraction
|
||||||
|
if (scaleY > 0f) {
|
||||||
|
if (scaleAroundPivotY) {
|
||||||
|
transform.postScale(1f, scaleY, resultTranslationX, baseTranslationY)
|
||||||
|
startPoint.scale(1f, scaleY, baseTranslationY)
|
||||||
|
endPoint.scale(1f, scaleY, baseTranslationY)
|
||||||
|
} else {
|
||||||
|
transform.postScale(1f, scaleY)
|
||||||
|
startPoint.scale(1f, scaleY)
|
||||||
|
endPoint.scale(1f, scaleY)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
displayedWavePath.transform(transform)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class RoundedBlockRenderer {
|
||||||
|
private val drawRect = RectF()
|
||||||
|
private val patchRect = RectF()
|
||||||
|
private val clipRect = RectF()
|
||||||
|
private val roundedRectPath = Path()
|
||||||
|
|
||||||
|
fun drawRoundedBlock(
|
||||||
|
canvas: Canvas,
|
||||||
|
paint: Paint,
|
||||||
|
drawCenter: PathPoint,
|
||||||
|
drawWidth: Float,
|
||||||
|
drawHeight: Float,
|
||||||
|
displayedTrackThickness: Float,
|
||||||
|
drawCornerSize: Float,
|
||||||
|
clipCenter: PathPoint? = null,
|
||||||
|
clipWidth: Float = 0f,
|
||||||
|
clipHeight: Float = 0f,
|
||||||
|
clipCornerSize: Float = 0f,
|
||||||
|
clipRight: Boolean = false,
|
||||||
|
) {
|
||||||
|
var 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.save()
|
||||||
|
if (clipCenter != null) {
|
||||||
|
localClipHeight = min(localClipHeight, displayedTrackThickness)
|
||||||
|
localClipCornerSize =
|
||||||
|
min(
|
||||||
|
localClipWidth / 2f,
|
||||||
|
localClipCornerSize * localClipHeight / displayedTrackThickness,
|
||||||
|
)
|
||||||
|
if (clipRight) {
|
||||||
|
val leftEdgeDiff =
|
||||||
|
(clipCenter.posVec[0] - localClipCornerSize) -
|
||||||
|
(drawCenter.posVec[0] - 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[0] + localClipCornerSize) -
|
||||||
|
(drawCenter.posVec[0] + 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,
|
||||||
|
)
|
||||||
|
canvas.translate(clipCenter.posVec[0], clipCenter.posVec[1])
|
||||||
|
canvas.rotate(vectorToCanvasRotation(clipCenter.tanVec))
|
||||||
|
roundedRectPath.reset()
|
||||||
|
roundedRectPath.addRoundRect(
|
||||||
|
clipRect,
|
||||||
|
localClipCornerSize,
|
||||||
|
localClipCornerSize,
|
||||||
|
Path.Direction.CCW,
|
||||||
|
)
|
||||||
|
canvas.clipPath(roundedRectPath)
|
||||||
|
|
||||||
|
canvas.rotate(-vectorToCanvasRotation(clipCenter.tanVec))
|
||||||
|
canvas.translate(-clipCenter.posVec[0], -clipCenter.posVec[1])
|
||||||
|
canvas.translate(drawCenter.posVec[0], drawCenter.posVec[1])
|
||||||
|
canvas.rotate(vectorToCanvasRotation(drawCenter.tanVec))
|
||||||
|
canvas.drawRect(patchRect, paint)
|
||||||
|
canvas.drawRoundRect(drawRect, drawCornerSize, drawCornerSize, paint)
|
||||||
|
} else {
|
||||||
|
canvas.translate(drawCenter.posVec[0], drawCenter.posVec[1])
|
||||||
|
canvas.rotate(vectorToCanvasRotation(drawCenter.tanVec))
|
||||||
|
canvas.drawRoundRect(drawRect, drawCornerSize, drawCornerSize, paint)
|
||||||
|
}
|
||||||
|
canvas.restore()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun vectorToCanvasRotation(vector: FloatArray): Float =
|
||||||
|
Math.toDegrees(atan2(vector[1], vector[0]).toDouble()).toFloat()
|
||||||
|
}
|
||||||
|
|
||||||
|
internal object WaveMotionUtils {
|
||||||
|
fun resolveWaveTransitionSpring(context: Context, animateOn: Boolean): SpringForce {
|
||||||
|
val attr =
|
||||||
|
if (animateOn) {
|
||||||
|
MR.attr.motionSpringFastEffects
|
||||||
|
} else {
|
||||||
|
MR.attr.motionSpringDefaultEffects
|
||||||
|
}
|
||||||
|
val defaultStyle =
|
||||||
|
if (animateOn) {
|
||||||
|
MR.style.Motion_Material3_Spring_Standard_Fast_Effects
|
||||||
|
} else {
|
||||||
|
MR.style.Motion_Material3_Spring_Standard_Default_Effects
|
||||||
|
}
|
||||||
|
return MotionUtils.resolveThemeSpringForce(context, attr, defaultStyle)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun applyRampEasing(fraction: Float): Float {
|
||||||
|
val clamped = fraction.coerceIn(0f, 1f)
|
||||||
|
return clamped * clamped * (3f - 2f * clamped)
|
||||||
|
}
|
||||||
|
|
||||||
|
private const val WAVE_SMOOTHNESS = 0.48f
|
||||||
Reference in New Issue
Block a user