ui: migrate fully to physics animations

More or less looks the same, mostly since I can't
figure out how to make springs work well. But
it's fine enough.
This commit is contained in:
Alexander Capehart
2026-03-28 09:46:11 -06:00
parent fff09c1672
commit d37f5b2742
6 changed files with 154 additions and 506 deletions
@@ -40,6 +40,7 @@ import androidx.core.view.doOnLayout
import androidx.core.view.isEmpty
import androidx.core.view.updateMarginsRelative
import androidx.core.widget.ImageViewCompat
import androidx.dynamicanimation.animation.SpringAnimation
import coil3.ImageLoader
import coil3.asImage
import coil3.request.ImageRequest
@@ -60,8 +61,8 @@ import org.oxycblt.auxio.image.coil.RoundedRectTransformation
import org.oxycblt.auxio.image.coil.SmatteringCoverCollection
import org.oxycblt.auxio.image.coil.SquareCropTransformation
import org.oxycblt.auxio.image.coil.StackCoverCollection
import org.oxycblt.auxio.ui.MaterialFader
import org.oxycblt.auxio.ui.MotionHandle
import org.oxycblt.auxio.ui.Effect
import org.oxycblt.auxio.ui.Spatial
import org.oxycblt.auxio.ui.UISettings
import org.oxycblt.auxio.util.getAttrColorCompat
import org.oxycblt.auxio.util.getColorCompat
@@ -104,8 +105,9 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
private val selectionBadge: ImageView?
private val iconSize: Int?
private val fader = MaterialFader.new(context)
private var fadeAnimator: MotionHandle? = null
private val checkScaleSpring = Spatial.FAST
private val checkAlphaSpring = Effect.FAST
private var fadeAnimators: List<SpringAnimation>? = null
private val indicatorMatrix = Matrix()
private val indicatorMatrixSrc = RectF()
private val indicatorMatrixDst = RectF()
@@ -340,10 +342,17 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
}
private fun invalidateSelectionIndicatorAlpha(selectionBadge: ImageView) {
fadeAnimator?.cancel()
fadeAnimator =
(if (isActivated) fader.fadeIn(selectionBadge) else fader.fadeOut(selectionBadge))
.also { it.start() }
fadeAnimators?.forEach { it.cancel() }
val scaleAnim: SpringAnimation
val alphaAnim: SpringAnimation
if (isActivated) {
scaleAnim = checkScaleSpring.scale(selectionBadge, 1.0f)
alphaAnim = checkAlphaSpring.alpha(selectionBadge, 1.0f)
} else {
scaleAnim = checkScaleSpring.scale(selectionBadge, 0.9f)
alphaAnim = checkAlphaSpring.alpha(selectionBadge, 0.0f)
}
fadeAnimators = listOf(scaleAnim, alphaAnim)
}
/**
@@ -60,15 +60,16 @@ import kotlin.math.max
import kotlin.math.min
import kotlin.math.roundToInt
import org.oxycblt.auxio.R
import org.oxycblt.auxio.ui.Effect
import org.oxycblt.auxio.ui.ExpressiveShapes
import org.oxycblt.auxio.ui.MaterialSlider
import org.oxycblt.auxio.ui.MotionHandle
import org.oxycblt.auxio.ui.Spatial
import org.oxycblt.auxio.util.getAttrColorCompat
import org.oxycblt.auxio.util.getAttrResourceId
import org.oxycblt.auxio.util.getDimenPixels
import org.oxycblt.auxio.util.inflater
import org.oxycblt.auxio.util.isRtl
import org.oxycblt.auxio.util.isUnder
import org.oxycblt.auxio.util.scale
import org.oxycblt.auxio.util.systemBarInsetsCompat
/**
@@ -107,17 +108,22 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
// Thumb
private val thumbWidth = context.getDimenPixels(R.dimen.spacing_mid_medium)
private val thumbHeight = context.getDimenPixels(R.dimen.size_touchable_medium)
private val thumbSlider = MaterialSlider.small(context, thumbWidth)
private var thumbAnimator: MotionHandle? = null
private var thumbAnimator: SpringAnimation? = null
@SuppressLint("InflateParams")
private val thumbView =
context.inflater.inflate(R.layout.view_scroll_thumb, null).apply {
thumbSlider.jumpOut(this)
translationX = thumbWidth.toFloat()
}
private val thumbPadding = Rect(0, 0, 0, 0)
private var thumbOffset = 0
private val thumbTranslateInSpring = Spatial.DEFAULT
private val thumbTranslateOutSpring = Spatial.FAST
private val popupScaleSpring = Spatial.DEFAULT
private val popupAlphaSpring = Effect.DEFAULT
private var showingThumb = false
private val hideThumbRunnable = Runnable {
if (!dragging) {
@@ -186,6 +192,8 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
marginEnd = context.getDimenPixels(R.dimen.size_touchable_small)
gravity = Gravity.CENTER_HORIZONTAL or Gravity.TOP
}
scale = 0.5f
alpha = 0.0f
}
private val popupTextStaggerDelayMillis =
MotionUtils.resolveThemeDuration(context, MR.attr.motionDurationShort2, 100).toLong()
@@ -207,7 +215,8 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
private var popupTextAlphaAnimation: SpringAnimation? = null
private val popupTextRevealRunnable = Runnable {
if (showingPopup) {
animatePopupTextIn()
popupShapeScaleAnimation?.cancel()
popupShapeScaleAnimation = popupScaleSpring.scale(popupView, 0f)
}
}
private var showingPopup = false
@@ -272,7 +281,6 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
init {
overlay.add(thumbView)
overlay.add(popupView)
jumpOutPopup()
addItemDecoration(
object : ItemDecoration() {
@@ -543,7 +551,7 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
showingThumb = true
thumbAnimator?.cancel()
thumbAnimator = thumbSlider.slideIn(thumbView).also { it.start() }
thumbAnimator = thumbTranslateInSpring.translateX(thumbView, 0f)
}
private fun hideThumb() {
@@ -553,7 +561,7 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
showingThumb = false
thumbAnimator?.cancel()
thumbAnimator = thumbSlider.slideOut(thumbView).also { it.start() }
thumbAnimator = thumbTranslateOutSpring.translateX(thumbView, thumbWidth.toFloat())
}
private fun showPopup() {
@@ -566,9 +574,10 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
showingPopup = true
resetPopupTextAutoScaleConfig()
cancelPopupAnimations()
animatePopupShapeIn()
popupView.postDelayed(popupTextRevealRunnable, popupTextStaggerDelayMillis)
popupShapeScaleAnimation?.cancel()
popupShapeAlphaAnimation?.cancel()
popupShapeScaleAnimation = popupScaleSpring.scale(popupView, 1f)
popupShapeAlphaAnimation = popupAlphaSpring.alpha(popupView, 1f)
}
private fun hidePopup() {
@@ -577,115 +586,10 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
}
showingPopup = false
cancelPopupAnimations()
animatePopupTextOut()
animatePopupShapeOut()
}
private fun jumpOutPopup() {
popupBackgroundDrawable.revealScale = POPUP_SHAPE_HIDDEN_SCALE
popupBackgroundDrawable.revealAlpha = 0f
popupTextView.alpha = 0f
popupTextView.scaleX = POPUP_TEXT_HIDDEN_SCALE
popupTextView.scaleY = POPUP_TEXT_HIDDEN_SCALE
}
private fun cancelPopupAnimations() {
popupView.removeCallbacks(popupTextRevealRunnable)
popupShapeScaleAnimation?.cancel()
popupShapeScaleAnimation = null
popupShapeAlphaAnimation?.cancel()
popupShapeAlphaAnimation = null
popupTextScaleAnimation?.cancel()
popupTextScaleAnimation = null
popupTextAlphaAnimation?.cancel()
popupTextAlphaAnimation = null
}
private fun animatePopupShapeIn() {
popupShapeScaleAnimation =
springTo(
startValue = popupBackgroundDrawable.revealScale,
finalValue = 1f,
springTemplate = popupSpatialSpring,
minimumVisibleChange = 0.001f,
) {
popupBackgroundDrawable.revealScale = it
}
popupShapeAlphaAnimation =
springTo(
startValue = popupBackgroundDrawable.revealAlpha,
finalValue = 1f,
springTemplate = popupEffectsSpring,
minimumVisibleChange = 1f / 255f,
) {
popupBackgroundDrawable.revealAlpha = it
}
}
private fun animatePopupShapeOut() {
popupShapeScaleAnimation =
springTo(
startValue = popupBackgroundDrawable.revealScale,
finalValue = POPUP_SHAPE_HIDDEN_SCALE,
springTemplate = popupSpatialSpring,
minimumVisibleChange = 0.001f,
) {
popupBackgroundDrawable.revealScale = it
}
popupShapeAlphaAnimation =
springTo(
startValue = popupBackgroundDrawable.revealAlpha,
finalValue = 0f,
springTemplate = popupEffectsSpring,
minimumVisibleChange = 1f / 255f,
) {
popupBackgroundDrawable.revealAlpha = it
}
}
private fun animatePopupTextIn() {
popupTextScaleAnimation =
springTo(
startValue = popupTextView.scaleX,
finalValue = 1f,
springTemplate = popupSpatialSpring,
minimumVisibleChange = 0.001f,
) {
popupTextView.scaleX = it
popupTextView.scaleY = it
}
popupTextAlphaAnimation =
springTo(
startValue = popupTextView.alpha,
finalValue = 1f,
springTemplate = popupEffectsSpring,
minimumVisibleChange = 1f / 255f,
) {
popupTextView.alpha = it
}
}
private fun animatePopupTextOut() {
popupTextScaleAnimation =
springTo(
startValue = popupTextView.scaleX,
finalValue = POPUP_TEXT_HIDDEN_SCALE,
springTemplate = popupSpatialSpring,
minimumVisibleChange = 0.001f,
) {
popupTextView.scaleX = it
popupTextView.scaleY = it
}
popupTextAlphaAnimation =
springTo(
startValue = popupTextView.alpha,
finalValue = 0f,
springTemplate = popupEffectsSpring,
minimumVisibleChange = 1f / 255f,
) {
popupTextView.alpha = it
}
popupShapeScaleAnimation = popupScaleSpring.scale(popupView, 0.5f)
popupShapeAlphaAnimation = popupAlphaSpring.alpha(popupView, 0f)
}
private fun createPopupTextView(context: Context): MaterialTextView =
@@ -787,13 +691,14 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
finalValue: Float,
springTemplate: SpringForce,
minimumVisibleChange: Float,
dampingOverride: Float? = null,
update: (Float) -> Unit,
): SpringAnimation {
val animation =
SpringAnimation(FloatValueHolder(startValue)).apply {
spring =
SpringForce().apply {
dampingRatio = springTemplate.dampingRatio
dampingRatio = dampingOverride ?: springTemplate.dampingRatio
stiffness = springTemplate.stiffness
finalPosition = finalValue
}
@@ -877,7 +782,7 @@ private class SoftBurstPopupDrawable(context: Context) : Drawable() {
private var shapeAlpha = 255
var revealScale = 1f
set(value) {
val clampedValue = value.coerceIn(0f, 1f)
val clampedValue = value.coerceAtLeast(0f)
if (field == clampedValue) {
return
}
@@ -23,7 +23,6 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.XmlRes
import androidx.appcompat.widget.Toolbar
import androidx.core.view.updatePadding
import androidx.navigation.fragment.findNavController
import androidx.preference.Preference
@@ -37,6 +36,7 @@ import org.oxycblt.auxio.settings.ui.IntListPreference
import org.oxycblt.auxio.settings.ui.IntListPreferenceDialog
import org.oxycblt.auxio.settings.ui.PreferenceHeaderItemDecoration
import org.oxycblt.auxio.settings.ui.WrappedDialogPreference
import org.oxycblt.auxio.ui.AuxioToolbar
import org.oxycblt.auxio.util.systemBarInsetsCompat
import timber.log.Timber as L
@@ -74,7 +74,7 @@ abstract class BasePreferenceFragment(@XmlRes private val screen: Int) :
view.findViewById<AppBarLayout>(R.id.preferences_appbar).liftOnScrollTargetViewId =
androidx.preference.R.id.recycler_view
view.findViewById<Toolbar>(R.id.preferences_toolbar).apply {
view.findViewById<AuxioToolbar>(R.id.preferences_toolbar).apply {
title = preferenceScreen.title
setNavigationOnClickListener { findNavController().navigateUp() }
}
@@ -18,403 +18,102 @@
package org.oxycblt.auxio.ui
import android.animation.TimeInterpolator
import android.animation.ValueAnimator
import android.content.Context
import android.view.View
import androidx.annotation.AttrRes
import androidx.annotation.StyleRes
import androidx.core.view.isInvisible
import androidx.dynamicanimation.animation.FloatValueHolder
import androidx.dynamicanimation.animation.SpringAnimation
import androidx.dynamicanimation.animation.SpringForce
import androidx.interpolator.view.animation.FastOutSlowInInterpolator
import com.google.android.material.R as MR
import com.google.android.material.motion.MotionUtils
import org.oxycblt.auxio.util.scale
class AnimConfig(
context: Context,
@AttrRes interpolatorRes: Int,
@AttrRes durationRes: Int,
defaultDuration: Int,
) {
val interpolator: TimeInterpolator =
MotionUtils.resolveThemeInterpolator(context, interpolatorRes, FastOutSlowInInterpolator())
val duration: Long =
MotionUtils.resolveThemeDuration(context, durationRes, defaultDuration).toLong()
class Spatial private constructor(@AttrRes val attr: Int, val defaultStyle: Int) {
private fun resolve(context: Context) =
MotionUtils.resolveThemeSpringForce(context, attr, defaultStyle)
companion object {
val STANDARD = MR.attr.motionEasingStandardInterpolator
val MEDIUM2 = MR.attr.motionDurationMedium2 to 300
fun of(context: Context, @AttrRes interpolator: Int, duration: Pair<Int, Int>) =
AnimConfig(context, interpolator, duration.first, duration.second)
}
inline fun genericFloat(
from: Float,
to: Float,
delayMs: Long = 0,
crossinline update: (Float) -> Unit,
): ValueAnimator =
ValueAnimator.ofFloat(from, to).apply {
startDelay = delayMs
duration = this@AnimConfig.duration
interpolator = this@AnimConfig.interpolator
addUpdateListener { update(animatedValue as Float) }
}
}
interface MotionHandle {
fun start()
fun cancel()
}
private object NoOpMotionHandle : MotionHandle {
override fun start() = Unit
override fun cancel() = Unit
}
private class SnapMotionHandle(private val action: () -> Unit) : MotionHandle {
private var canceled = false
override fun start() {
if (!canceled) {
action()
}
}
override fun cancel() {
canceled = true
}
}
private class CompositeMotionHandle(
private val handles: List<MotionHandle>,
private val onStart: (() -> Unit)? = null,
) : MotionHandle {
override fun start() {
onStart?.invoke()
for (handle in handles) {
handle.start()
}
}
override fun cancel() {
for (handle in handles) {
handle.cancel()
}
}
}
private data class SpringToken(@AttrRes val attr: Int, @StyleRes val defaultStyle: Int)
private object MotionSpringTokens {
val FAST_EFFECTS =
SpringToken(
MR.attr.motionSpringFastEffects,
MR.style.Motion_Material3_Spring_Standard_Fast_Effects,
)
// val DEFAULT_EFFECTS =
// SpringToken(
// MR.attr.motionSpringDefaultEffects,
// MR.style.Motion_Material3_Spring_Standard_Default_Effects,
// )
// val SLOW_EFFECTS =
// SpringToken(
// MR.attr.motionSpringSlowEffects,
// MR.style.Motion_Material3_Spring_Standard_Slow_Effects,
// )
val FAST_SPATIAL =
SpringToken(
MR.attr.motionSpringFastSpatial,
MR.style.Motion_Material3_Spring_Standard_Fast_Spatial,
)
val DEFAULT_SPATIAL =
SpringToken(
MR.attr.motionSpringDefaultSpatial,
MR.style.Motion_Material3_Spring_Standard_Default_Spatial,
)
// val SLOW_SPATIAL =
// SpringToken(
// MR.attr.motionSpringSlowSpatial,
// MR.style.Motion_Material3_Spring_Standard_Slow_Spatial,
// )
}
private class MotionSpring(
context: Context,
token: SpringToken,
private val dampingRatioOverride: Float? = null,
) {
private val springTemplate =
MotionUtils.resolveThemeSpringForce(context, token.attr, token.defaultStyle)
fun create(
startValue: Float,
finalValue: Float,
minimumVisibleChange: Float,
update: (Float) -> Unit,
onEnd: ((canceled: Boolean) -> Unit)? = null,
): MotionHandle {
val animation =
SpringAnimation(FloatValueHolder(startValue)).apply {
spring =
SpringForce().apply {
dampingRatio = dampingRatioOverride ?: springTemplate.dampingRatio
stiffness = springTemplate.stiffness
finalPosition = finalValue
}
setStartValue(startValue)
setMinimumVisibleChange(minimumVisibleChange)
addUpdateListener { _, value, _ -> update(value) }
addEndListener { _, canceled, value, _ ->
update(if (canceled) value else finalValue)
onEnd?.invoke(canceled)
}
fun scale(view: View, to: Float, jumpOnCancellation: Boolean = false): SpringAnimation {
val from = view.scale
val springForce = resolve(view.context)
return SpringAnimation(FloatValueHolder(from)).apply {
spring = springForce
setStartValue(from)
setMinimumVisibleChange(0.0001f)
addUpdateListener { _, value, _ -> view.scale = value }
addEndListener { _, canceled, value, _ ->
view.scale = if (!canceled || jumpOnCancellation) to else value
}
return object : MotionHandle {
override fun start() {
animation.animateToFinalPosition(finalValue)
}
animateToFinalPosition(to)
}
}
override fun cancel() {
animation.cancel()
fun translateX(view: View, to: Float, jumpOnCancellation: Boolean = false): SpringAnimation {
val from = view.translationX
val springForce = resolve(view.context)
return SpringAnimation(FloatValueHolder(from)).apply {
spring = springForce
setStartValue(from)
setMinimumVisibleChange(0.0001f)
addUpdateListener { _, value, _ -> view.translationX = value }
addEndListener { _, canceled, value, _ ->
view.translationX = if (!canceled || jumpOnCancellation) to else value
}
animateToFinalPosition(to)
}
}
}
private fun View.resolveSlideTarget(x: Int?): Float {
if (x != null) {
return x.toFloat()
}
val resolvedWidth = width.takeIf { it > 0 } ?: measuredWidth.takeIf { it > 0 }
return (resolvedWidth ?: OFFSCREEN_TRANSLATION_PX.toInt()).toFloat()
}
class MaterialFader
private constructor(
context: Context,
private val scale: Float,
alphaOutToken: SpringToken,
scaleOutToken: SpringToken,
alphaInToken: SpringToken,
scaleInToken: SpringToken,
scaleOutDampingRatioOverride: Float? = null,
scaleInDampingRatioOverride: Float? = null,
) {
private val alphaOutSpring = MotionSpring(context, alphaOutToken)
private val scaleOutSpring =
MotionSpring(context, scaleOutToken, dampingRatioOverride = scaleOutDampingRatioOverride)
private val alphaInSpring = MotionSpring(context, alphaInToken)
private val scaleInSpring =
MotionSpring(context, scaleInToken, dampingRatioOverride = scaleInDampingRatioOverride)
fun jumpToFadeOut(view: View) {
view.apply {
alpha = 0f
scaleX = scale
scaleY = scale
isInvisible = true
}
}
private fun jumpToFadeIn(view: View) {
view.apply {
alpha = 1f
scaleX = 1.0f
scaleY = 1.0f
isInvisible = false
}
}
fun fadeOut(view: View, onEnd: (() -> Unit)? = null): MotionHandle {
if (!view.isLaidOut) {
return SnapMotionHandle {
jumpToFadeOut(view)
onEnd?.invoke()
}
}
val alphaMotion =
alphaOutSpring.create(
view.alpha,
0f,
ALPHA_MIN_VISIBLE_CHANGE,
update = { view.alpha = it },
onEnd = { canceled ->
view.isInvisible = !canceled
if (!canceled) {
onEnd?.invoke()
}
},
)
val scaleXMotion =
scaleOutSpring.create(
view.scaleX,
scale,
SCALE_MIN_VISIBLE_CHANGE,
update = { view.scaleX = it },
)
val scaleYMotion =
scaleOutSpring.create(
view.scaleY,
scale,
SCALE_MIN_VISIBLE_CHANGE,
update = { view.scaleY = it },
)
return CompositeMotionHandle(
listOf(alphaMotion, scaleXMotion, scaleYMotion),
onStart = { view.isInvisible = false },
)
}
fun fadeIn(view: View, onEnd: (() -> Unit)? = null): MotionHandle {
if (!view.isLaidOut) {
return SnapMotionHandle {
jumpToFadeIn(view)
onEnd?.invoke()
}
}
val alphaMotion =
alphaInSpring.create(
view.alpha,
1f,
ALPHA_MIN_VISIBLE_CHANGE,
update = { view.alpha = it },
onEnd = { canceled ->
if (!canceled) {
onEnd?.invoke()
}
},
)
val scaleXMotion =
scaleInSpring.create(
view.scaleX,
1.0f,
SCALE_MIN_VISIBLE_CHANGE,
update = { view.scaleX = it },
)
val scaleYMotion =
scaleInSpring.create(
view.scaleY,
1.0f,
SCALE_MIN_VISIBLE_CHANGE,
update = { view.scaleY = it },
)
return CompositeMotionHandle(
listOf(alphaMotion, scaleXMotion, scaleYMotion),
onStart = { view.isInvisible = false },
)
}
companion object {
fun new(context: Context) =
MaterialFader(
context,
0.9f,
MotionSpringTokens.FAST_EFFECTS,
MotionSpringTokens.FAST_SPATIAL,
MotionSpringTokens.FAST_EFFECTS,
MotionSpringTokens.FAST_SPATIAL,
// Material's standard spatial springs are only slightly underdamped (0.9),
// which is too restrained for the toolbar swap to read as springy.
scaleOutDampingRatioOverride = 0.6f,
scaleInDampingRatioOverride = 0.6f,
val FAST =
Spatial(
MR.attr.motionSpringFastSpatial,
MR.style.Motion_Material3_Spring_Standard_Fast_Spatial,
)
val DEFAULT =
Spatial(
MR.attr.motionSpringDefaultSpatial,
MR.style.Motion_Material3_Spring_Standard_Default_Spatial,
)
val SLOW =
Spatial(
MR.attr.motionSpringSlowSpatial,
MR.style.Motion_Material3_Spring_Standard_Slow_Spatial,
)
}
}
class MaterialFlipper(context: Context) {
private val fader = MaterialFader.new(context)
class Effect private constructor(@AttrRes val attr: Int, @StyleRes val defaultStyle: Int) {
fun resolve(context: Context) = MotionUtils.resolveThemeSpringForce(context, attr, defaultStyle)
fun jump(from: View) {
fader.jumpToFadeOut(from)
}
fun flip(from: View, to: View): MotionHandle {
var canceled = false
var inMotion: MotionHandle = NoOpMotionHandle
val outMotion =
fader.fadeOut(from) {
if (!canceled) {
inMotion = fader.fadeIn(to)
inMotion.start()
}
}
return object : MotionHandle {
override fun start() {
canceled = false
outMotion.start()
}
override fun cancel() {
canceled = true
outMotion.cancel()
inMotion.cancel()
fun alpha(view: View, to: Float, jumpOnCancellation: Boolean = false): SpringAnimation {
val from = view.alpha
val springForce = resolve(view.context)
return SpringAnimation(FloatValueHolder(from)).apply {
spring = springForce
setStartValue(from)
setMinimumVisibleChange(1f / 255f)
addUpdateListener { _, value, _ -> view.alpha = value }
addEndListener { _, canceled, value, _ ->
view.scale = if (!canceled || jumpOnCancellation) to else value
}
animateToFinalPosition(to)
}
}
}
class MaterialSlider
private constructor(
context: Context,
private val x: Int?,
inSpringToken: SpringToken,
outSpringToken: SpringToken,
) {
private val outSpring = MotionSpring(context, outSpringToken)
private val inSpring = MotionSpring(context, inSpringToken)
fun jumpOut(view: View) {
view.translationX = view.resolveSlideTarget(x)
}
fun slideOut(view: View): MotionHandle {
val target = view.resolveSlideTarget(x)
if (!view.isLaidOut) {
return SnapMotionHandle { view.translationX = target }
}
if (view.translationX > target) {
view.translationX = target
}
return outSpring.create(
view.translationX,
target,
SPATIAL_MIN_VISIBLE_CHANGE,
update = { view.translationX = it },
)
}
fun slideIn(view: View): MotionHandle {
if (!view.isLaidOut) {
return SnapMotionHandle { view.translationX = 0f }
}
return inSpring.create(
view.translationX,
0f,
SPATIAL_MIN_VISIBLE_CHANGE,
update = { view.translationX = it },
)
}
companion object {
fun small(context: Context, x: Int?) =
MaterialSlider(
context,
x,
MotionSpringTokens.FAST_SPATIAL,
MotionSpringTokens.DEFAULT_SPATIAL,
val DEFAULT =
Effect(
MR.attr.motionSpringDefaultEffects,
MR.style.Motion_Material3_Spring_Standard_Default_Effects,
)
val SLOW =
Effect(
MR.attr.motionSpringSlowEffects,
MR.style.Motion_Material3_Spring_Standard_Slow_Effects,
)
val FAST =
Effect(
MR.attr.motionSpringFastEffects,
MR.style.Motion_Material3_Spring_Standard_Fast_Effects,
)
}
}
private const val ALPHA_MIN_VISIBLE_CHANGE = 1f / 512f
private const val SCALE_MIN_VISIBLE_CHANGE = 0.0001f
private const val SPATIAL_MIN_VISIBLE_CHANGE = 0.01f
private const val OFFSCREEN_TRANSLATION_PX = 100000f
@@ -24,20 +24,29 @@ import android.widget.FrameLayout
import androidx.annotation.AttrRes
import androidx.annotation.IdRes
import androidx.core.view.children
import androidx.dynamicanimation.animation.SpringAnimation
import org.oxycblt.auxio.util.scale
import timber.log.Timber as L
class MultiToolbar
@JvmOverloads
constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr: Int = 0) :
FrameLayout(context, attrs, defStyleAttr) {
private var animator: MotionHandle? = null
private var outAnimators: List<SpringAnimation>? = null
private var inAnimators: List<SpringAnimation>? = null
private var currentlyVisible = 0
private val flipper = MaterialFlipper(context)
private val outScaleSpring = Spatial.FAST
private val outAlphaSpring = Effect.FAST
private val inScaleSpring = Spatial.DEFAULT
private val inAlphaSpring = Effect.DEFAULT
override fun onFinishInflate() {
super.onFinishInflate()
for (i in 1 until childCount) {
getChildAt(i).apply { flipper.jump(this) }
getChildAt(i).apply {
scale = 0.9f
alpha = 0f
}
}
}
@@ -53,8 +62,26 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
// don't work due to translation)
// Set up the target transitions for both the inner and selection toolbars.
L.d("Changing toolbar visibility $from -> 0f, $to -> 1f")
animator?.cancel()
animator = flipper.flip(getChildAt(from), getChildAt(to)).also { it.start() }
inAnimators?.forEach { it.cancel() }
outAnimators?.forEach { it.cancel() }
val from = getChildAt(from)
val to = getChildAt(to)
// since we will lose track of the going-out view if we suddenly cancel it's probably better
// to just jump to the end rather than have overlapping views or a stuck toolbar
val outScaleAnimation = outScaleSpring.scale(from, 0.9f, jumpOnCancellation = true)
val outAlphaAnimation =
outAlphaSpring.alpha(from, 0.0f, jumpOnCancellation = true).addEndListener {
_,
cancelled,
_,
_ ->
if (!cancelled) {
val inScaleAnimation = inScaleSpring.scale(to, 1.0f)
val inAlphaAnimation = inAlphaSpring.alpha(to, 1.0f)
inAnimators = listOf(inScaleAnimation, inAlphaAnimation)
}
}
outAnimators = listOf(outScaleAnimation, outAlphaAnimation)
return true
}
@@ -98,6 +98,14 @@ private fun isUnderImpl(
val View.isRtl: Boolean
get() = layoutDirection == View.LAYOUT_DIRECTION_RTL
/** A single scale value **assuming that the View is always center-scaled** */
var View.scale: Float
get() = scaleX
set(it) {
scaleX = it
scaleY = it
}
/** Get a [Context] from a [ViewBinding]'s root [View]. */
val ViewBinding.context: Context
get() = root.context