ui: migrate motion to m3
This commit is contained in:
@@ -18,20 +18,18 @@
|
||||
|
||||
package org.oxycblt.auxio.home
|
||||
|
||||
import android.animation.Animator
|
||||
import android.animation.AnimatorListenerAdapter
|
||||
import android.animation.AnimatorSet
|
||||
import android.animation.ObjectAnimator
|
||||
import android.animation.ArgbEvaluator
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.ColorFilter
|
||||
import android.graphics.PixelFormat
|
||||
import android.graphics.PorterDuff
|
||||
import android.graphics.Rect
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.content.Context
|
||||
import android.content.res.ColorStateList
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.graphics.drawable.RotateDrawable
|
||||
import android.os.Bundle
|
||||
import android.os.Parcelable
|
||||
import android.util.AttributeSet
|
||||
import android.util.Property
|
||||
import android.view.View
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import androidx.annotation.AttrRes
|
||||
import androidx.annotation.FloatRange
|
||||
@@ -39,6 +37,11 @@ import androidx.core.os.BundleCompat
|
||||
import androidx.core.view.setMargins
|
||||
import androidx.core.view.updateLayoutParams
|
||||
import androidx.core.widget.TextViewCompat
|
||||
import androidx.dynamicanimation.animation.FloatValueHolder
|
||||
import androidx.dynamicanimation.animation.SpringAnimation
|
||||
import androidx.dynamicanimation.animation.SpringForce
|
||||
import com.google.android.material.R as MR
|
||||
import com.google.android.material.motion.MotionUtils
|
||||
import com.google.android.material.shape.MaterialShapeDrawable
|
||||
import com.leinardi.android.speeddial.FabWithLabelView
|
||||
import com.leinardi.android.speeddial.SpeedDialActionItem
|
||||
@@ -46,7 +49,6 @@ import com.leinardi.android.speeddial.SpeedDialView
|
||||
import kotlin.math.roundToInt
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.ui.AnimConfig
|
||||
import org.oxycblt.auxio.util.getAttrColorCompat
|
||||
import org.oxycblt.auxio.util.getAttrResourceId
|
||||
import org.oxycblt.auxio.util.getDimen
|
||||
@@ -65,9 +67,23 @@ import org.oxycblt.auxio.util.getDimenPixels
|
||||
* @author Hai Zhang, Alexander Capehart (OxygenCobalt)
|
||||
*/
|
||||
class ThemedSpeedDialView : SpeedDialView {
|
||||
private var mainFabAnimator: Animator? = null
|
||||
private var mainFabAnimation: MainFabAnimation? = null
|
||||
private val spacingSmall = context.getDimenPixels(R.dimen.spacing_small)
|
||||
private var innerChangeListener: ((Boolean) -> Unit)? = null
|
||||
private val mainFabDrawable = RotatingDrawable(checkNotNull(mainFab.drawable).mutate())
|
||||
private val mainFabSpatialSpring =
|
||||
MotionUtils.resolveThemeSpringForce(
|
||||
context,
|
||||
MR.attr.motionSpringFastSpatial,
|
||||
MR.style.Motion_Material3_Spring_Standard_Fast_Spatial,
|
||||
)
|
||||
private val mainFabEffectsSpring =
|
||||
MotionUtils.resolveThemeSpringForce(
|
||||
context,
|
||||
MR.attr.motionSpringFastEffects,
|
||||
MR.style.Motion_Material3_Spring_Standard_Fast_Effects,
|
||||
)
|
||||
private val argbEvaluator = ArgbEvaluator()
|
||||
|
||||
constructor(context: Context) : super(context)
|
||||
|
||||
@@ -79,8 +95,6 @@ class ThemedSpeedDialView : SpeedDialView {
|
||||
@AttrRes defStyleAttr: Int,
|
||||
) : super(context, attrs, defStyleAttr)
|
||||
|
||||
private val stationaryConfig = AnimConfig.of(context, AnimConfig.STANDARD, AnimConfig.MEDIUM2)
|
||||
|
||||
init {
|
||||
// Work around ripple bug on Android 12 when useCompatPadding = true.
|
||||
// @see https://github.com/material-components/material-components-android/issues/2617
|
||||
@@ -107,18 +121,15 @@ class ThemedSpeedDialView : SpeedDialView {
|
||||
.defaultColor
|
||||
|
||||
// Always use our own animation to fix the library issue that ripple is rotated as well.
|
||||
val mainFabDrawable =
|
||||
RotateDrawable().apply {
|
||||
drawable = mainFab.drawable
|
||||
toDegrees = 45f + 90f
|
||||
}
|
||||
mainFabAnimationRotateAngle = 0f
|
||||
setMainFabClosedDrawable(mainFabDrawable)
|
||||
setMainFabOpenedDrawable(null)
|
||||
setOnChangeListener(
|
||||
object : OnChangeListener {
|
||||
override fun onMainActionSelected(): Boolean = false
|
||||
|
||||
override fun onToggleChanged(isOpen: Boolean) {
|
||||
mainFabAnimation?.cancel()
|
||||
mainFab.backgroundTintList =
|
||||
ColorStateList.valueOf(
|
||||
if (isOpen) mainFabClosedBackgroundColor
|
||||
@@ -128,68 +139,84 @@ class ThemedSpeedDialView : SpeedDialView {
|
||||
ColorStateList.valueOf(
|
||||
if (isOpen) mainFabClosedIconColor else mainFabOpenedIconColor
|
||||
)
|
||||
mainFabAnimator?.cancel()
|
||||
mainFabAnimator =
|
||||
createMainFabAnimator(isOpen).apply {
|
||||
addListener(
|
||||
object : AnimatorListenerAdapter() {
|
||||
override fun onAnimationEnd(animation: Animator) {
|
||||
mainFabAnimator = null
|
||||
}
|
||||
}
|
||||
)
|
||||
start()
|
||||
}
|
||||
mainFabAnimation = createMainFabAnimation(isOpen).apply { start() }
|
||||
innerChangeListener?.invoke(isOpen)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private fun createMainFabAnimator(isOpen: Boolean): Animator {
|
||||
val totalDuration = stationaryConfig.duration
|
||||
val partialDuration = totalDuration / 2 // This is half of the total duration
|
||||
val delay = totalDuration / 4 // This is one fourth of the total duration
|
||||
private fun createMainFabAnimation(isOpen: Boolean): MainFabAnimation {
|
||||
val targetBackgroundTint =
|
||||
if (isOpen) mainFabOpenedBackgroundColor else mainFabClosedBackgroundColor
|
||||
val targetImageTint = if (isOpen) mainFabOpenedIconColor else mainFabClosedIconColor
|
||||
|
||||
val backgroundTintAnimator =
|
||||
ObjectAnimator.ofArgb(
|
||||
mainFab,
|
||||
VIEW_PROPERTY_BACKGROUND_TINT,
|
||||
if (isOpen) mainFabOpenedBackgroundColor else mainFabClosedBackgroundColor,
|
||||
)
|
||||
.apply {
|
||||
startDelay = delay
|
||||
duration = partialDuration
|
||||
}
|
||||
val backgroundTintAnimation =
|
||||
createColorSpringAnimation(
|
||||
startColor =
|
||||
if (isOpen) mainFabClosedBackgroundColor else mainFabOpenedBackgroundColor,
|
||||
endColor = targetBackgroundTint,
|
||||
) { mainFab.backgroundTintList = ColorStateList.valueOf(it) }
|
||||
val imageTintAnimation =
|
||||
createColorSpringAnimation(
|
||||
startColor = if (isOpen) mainFabClosedIconColor else mainFabOpenedIconColor,
|
||||
endColor = targetImageTint,
|
||||
) { mainFab.imageTintList = ColorStateList.valueOf(it) }
|
||||
val rotationAnimation =
|
||||
createSpringAnimation(
|
||||
startValue = mainFabDrawable.rotationDegrees,
|
||||
finalValue = if (isOpen) MAIN_FAB_OPEN_ROTATION_DEGREES else 0f,
|
||||
springTemplate = mainFabSpatialSpring,
|
||||
minimumVisibleChange = MAIN_FAB_ROTATION_MIN_VISIBLE_CHANGE,
|
||||
dampingRatioOverride = MAIN_FAB_ROTATION_DAMPING_RATIO_OVERRIDE,
|
||||
) { mainFabDrawable.rotationDegrees = it }
|
||||
|
||||
val imageTintAnimator =
|
||||
ObjectAnimator.ofArgb(
|
||||
mainFab,
|
||||
IMAGE_VIEW_PROPERTY_IMAGE_TINT,
|
||||
if (isOpen) mainFabOpenedIconColor else mainFabClosedIconColor,
|
||||
)
|
||||
.apply {
|
||||
startDelay = delay
|
||||
duration = partialDuration
|
||||
}
|
||||
|
||||
val levelAnimator =
|
||||
ObjectAnimator.ofInt(
|
||||
mainFab.drawable,
|
||||
DRAWABLE_PROPERTY_LEVEL,
|
||||
if (isOpen) 10000 else 0,
|
||||
)
|
||||
.apply { duration = totalDuration }
|
||||
|
||||
val animatorSet =
|
||||
AnimatorSet().apply {
|
||||
playTogether(backgroundTintAnimator, imageTintAnimator, levelAnimator)
|
||||
interpolator = stationaryConfig.interpolator
|
||||
}
|
||||
animatorSet.start()
|
||||
return animatorSet
|
||||
return MainFabAnimation(
|
||||
listOf(backgroundTintAnimation, imageTintAnimation, rotationAnimation)
|
||||
) {
|
||||
mainFabAnimation = null
|
||||
}
|
||||
}
|
||||
|
||||
private fun createColorSpringAnimation(
|
||||
startColor: Int,
|
||||
endColor: Int,
|
||||
update: (Int) -> Unit,
|
||||
): SpringAnimation =
|
||||
createSpringAnimation(
|
||||
startValue = 0f,
|
||||
finalValue = 1f,
|
||||
springTemplate = mainFabEffectsSpring,
|
||||
minimumVisibleChange = MAIN_FAB_COLOR_PROGRESS_MIN_VISIBLE_CHANGE,
|
||||
) { progress ->
|
||||
val color =
|
||||
argbEvaluator.evaluate(progress.coerceIn(0f, 1f), startColor, endColor) as Int
|
||||
update(color)
|
||||
}
|
||||
|
||||
private fun createSpringAnimation(
|
||||
startValue: Float,
|
||||
finalValue: Float,
|
||||
springTemplate: SpringForce,
|
||||
minimumVisibleChange: Float,
|
||||
dampingRatioOverride: Float? = null,
|
||||
update: (Float) -> Unit,
|
||||
): SpringAnimation =
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onAttachedToWindow() {
|
||||
super.onAttachedToWindow()
|
||||
|
||||
@@ -304,34 +331,140 @@ class ThemedSpeedDialView : SpeedDialView {
|
||||
innerChangeListener = listener
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val VIEW_PROPERTY_BACKGROUND_TINT =
|
||||
object : Property<View, Int>(Int::class.java, "backgroundTint") {
|
||||
override fun get(view: View): Int = view.backgroundTintList!!.defaultColor
|
||||
@Parcelize
|
||||
private class State(val superState: Parcelable?, val isOpen: Boolean) : Parcelable
|
||||
|
||||
override fun set(view: View, value: Int?) {
|
||||
view.backgroundTintList = ColorStateList.valueOf(value!!)
|
||||
private class MainFabAnimation(
|
||||
private val animations: List<SpringAnimation>,
|
||||
private val onEnd: () -> Unit,
|
||||
) {
|
||||
private var remainingAnimations = animations.size
|
||||
private var finished = false
|
||||
|
||||
init {
|
||||
animations.forEach { animation ->
|
||||
animation.addEndListener { _, _, _, _ ->
|
||||
if (finished) {
|
||||
return@addEndListener
|
||||
}
|
||||
remainingAnimations -= 1
|
||||
if (remainingAnimations == 0) {
|
||||
finished = true
|
||||
onEnd()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val IMAGE_VIEW_PROPERTY_IMAGE_TINT =
|
||||
object : Property<ImageView, Int>(Int::class.java, "imageTint") {
|
||||
override fun get(view: ImageView): Int = view.imageTintList!!.defaultColor
|
||||
|
||||
override fun set(view: ImageView, value: Int?) {
|
||||
view.imageTintList = ColorStateList.valueOf(value!!)
|
||||
}
|
||||
fun start() {
|
||||
animations.forEach { animation ->
|
||||
animation.animateToFinalPosition(animation.spring.finalPosition)
|
||||
}
|
||||
}
|
||||
|
||||
private val DRAWABLE_PROPERTY_LEVEL =
|
||||
object : Property<Drawable, Int>(Int::class.java, "level") {
|
||||
override fun get(drawable: Drawable): Int = drawable.level
|
||||
|
||||
override fun set(drawable: Drawable, value: Int?) {
|
||||
drawable.level = value!!
|
||||
}
|
||||
fun cancel() {
|
||||
if (finished) {
|
||||
return
|
||||
}
|
||||
finished = true
|
||||
animations.forEach { it.cancel() }
|
||||
onEnd()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class RotatingDrawable(
|
||||
drawable: Drawable,
|
||||
) : Drawable(), Drawable.Callback {
|
||||
private var wrappedDrawable = drawable
|
||||
|
||||
var rotationDegrees = 0f
|
||||
set(value) {
|
||||
if (field == value) {
|
||||
return
|
||||
}
|
||||
field = value
|
||||
invalidateSelf()
|
||||
}
|
||||
|
||||
init {
|
||||
wrappedDrawable.callback = this
|
||||
}
|
||||
|
||||
@Parcelize private class State(val superState: Parcelable?, val isOpen: Boolean) : Parcelable
|
||||
override fun draw(canvas: Canvas) {
|
||||
if (bounds.isEmpty) {
|
||||
return
|
||||
}
|
||||
|
||||
val saveCount = canvas.save()
|
||||
canvas.rotate(rotationDegrees, bounds.exactCenterX(), bounds.exactCenterY())
|
||||
wrappedDrawable.draw(canvas)
|
||||
canvas.restoreToCount(saveCount)
|
||||
}
|
||||
|
||||
override fun onBoundsChange(bounds: Rect) {
|
||||
wrappedDrawable.bounds = bounds
|
||||
}
|
||||
|
||||
override fun onStateChange(state: IntArray): Boolean {
|
||||
val changed = wrappedDrawable.setState(state)
|
||||
if (changed) {
|
||||
invalidateSelf()
|
||||
}
|
||||
return changed
|
||||
}
|
||||
|
||||
override fun isStateful(): Boolean = wrappedDrawable.isStateful
|
||||
|
||||
override fun getPadding(padding: Rect): Boolean = wrappedDrawable.getPadding(padding)
|
||||
|
||||
override fun getIntrinsicWidth(): Int = wrappedDrawable.intrinsicWidth
|
||||
|
||||
override fun getIntrinsicHeight(): Int = wrappedDrawable.intrinsicHeight
|
||||
|
||||
override fun setAlpha(alpha: Int) {
|
||||
wrappedDrawable.alpha = alpha
|
||||
}
|
||||
|
||||
override fun getAlpha(): Int = wrappedDrawable.alpha
|
||||
|
||||
override fun setColorFilter(colorFilter: ColorFilter?) {
|
||||
wrappedDrawable.colorFilter = colorFilter
|
||||
}
|
||||
|
||||
override fun getOpacity(): Int = PixelFormat.TRANSLUCENT
|
||||
|
||||
override fun setTint(tintColor: Int) {
|
||||
wrappedDrawable.setTint(tintColor)
|
||||
}
|
||||
|
||||
override fun setTintList(tint: ColorStateList?) {
|
||||
wrappedDrawable.setTintList(tint)
|
||||
}
|
||||
|
||||
override fun setTintMode(tintMode: PorterDuff.Mode?) {
|
||||
wrappedDrawable.setTintMode(tintMode)
|
||||
}
|
||||
|
||||
override fun setVisible(visible: Boolean, restart: Boolean): Boolean =
|
||||
super.setVisible(visible, restart) || wrappedDrawable.setVisible(visible, restart)
|
||||
|
||||
override fun onLevelChange(level: Int): Boolean = wrappedDrawable.setLevel(level)
|
||||
|
||||
override fun invalidateDrawable(who: Drawable) {
|
||||
invalidateSelf()
|
||||
}
|
||||
|
||||
override fun scheduleDrawable(who: Drawable, what: Runnable, `when`: Long) {
|
||||
scheduleSelf(what, `when`)
|
||||
}
|
||||
|
||||
override fun unscheduleDrawable(who: Drawable, what: Runnable) {
|
||||
unscheduleSelf(what)
|
||||
}
|
||||
}
|
||||
|
||||
private const val MAIN_FAB_OPEN_ROTATION_DEGREES = 135f
|
||||
private const val MAIN_FAB_ROTATION_MIN_VISIBLE_CHANGE = 0.1f
|
||||
private const val MAIN_FAB_ROTATION_DAMPING_RATIO_OVERRIDE = 0.6f
|
||||
private const val MAIN_FAB_COLOR_PROGRESS_MIN_VISIBLE_CHANGE = 1f / 255f
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
|
||||
package org.oxycblt.auxio.image
|
||||
|
||||
import android.animation.Animator
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.graphics.Canvas
|
||||
@@ -62,6 +61,7 @@ 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.UISettings
|
||||
import org.oxycblt.auxio.util.getAttrColorCompat
|
||||
import org.oxycblt.auxio.util.getColorCompat
|
||||
@@ -104,8 +104,8 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
|
||||
private val selectionBadge: ImageView?
|
||||
private val iconSize: Int?
|
||||
|
||||
private val fader = MaterialFader.quickLopsided(context)
|
||||
private var fadeAnimator: Animator? = null
|
||||
private val fader = MaterialFader.new(context)
|
||||
private var fadeAnimator: MotionHandle? = null
|
||||
private val indicatorMatrix = Matrix()
|
||||
private val indicatorMatrixSrc = RectF()
|
||||
private val indicatorMatrixDst = RectF()
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
|
||||
package org.oxycblt.auxio.list.recycler
|
||||
|
||||
import android.animation.Animator
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.graphics.Canvas
|
||||
@@ -63,6 +62,7 @@ import kotlin.math.roundToInt
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.ui.ExpressiveShapes
|
||||
import org.oxycblt.auxio.ui.MaterialSlider
|
||||
import org.oxycblt.auxio.ui.MotionHandle
|
||||
import org.oxycblt.auxio.util.getAttrColorCompat
|
||||
import org.oxycblt.auxio.util.getAttrResourceId
|
||||
import org.oxycblt.auxio.util.getDimenPixels
|
||||
@@ -108,7 +108,7 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
|
||||
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: Animator? = null
|
||||
private var thumbAnimator: MotionHandle? = null
|
||||
|
||||
@SuppressLint("InflateParams")
|
||||
private val thumbView =
|
||||
|
||||
@@ -18,19 +18,18 @@
|
||||
|
||||
package org.oxycblt.auxio.ui
|
||||
|
||||
import android.animation.Animator
|
||||
import android.animation.AnimatorSet
|
||||
import android.animation.TimeInterpolator
|
||||
import android.animation.ValueAnimator
|
||||
import android.content.Context
|
||||
import android.graphics.Rect
|
||||
import android.view.View
|
||||
import androidx.annotation.AttrRes
|
||||
import androidx.core.graphics.toRectF
|
||||
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.button.MaterialButton
|
||||
import com.google.android.material.motion.MotionUtils
|
||||
|
||||
class AnimConfig(
|
||||
@@ -46,26 +45,7 @@ class AnimConfig(
|
||||
|
||||
companion object {
|
||||
val STANDARD = MR.attr.motionEasingStandardInterpolator
|
||||
// val EMPHASIZED = MR.attr.motionEasingEmphasizedInterpolator
|
||||
val EMPHASIZED_ACCELERATE = MR.attr.motionEasingEmphasizedAccelerateInterpolator
|
||||
val EMPHASIZED_DECELERATE = MR.attr.motionEasingEmphasizedDecelerateInterpolator
|
||||
val SHORT1 = MR.attr.motionDurationShort1 to 50
|
||||
// val SHORT2 = MR.attr.motionDurationShort2 to 100
|
||||
val SHORT3 = MR.attr.motionDurationShort3 to 150
|
||||
// val SHORT4 = MR.attr.motionDurationShort4 to 200
|
||||
val MEDIUM1 = MR.attr.motionDurationMedium1 to 250
|
||||
val MEDIUM2 = MR.attr.motionDurationMedium2 to 300
|
||||
val MEDIUM3 = MR.attr.motionDurationMedium3 to 350
|
||||
|
||||
// val MEDIUM4 = MR.attr.motionDurationMedium4 to 400
|
||||
// val LONG1 = MR.attr.motionDurationLong1 to 450
|
||||
// val LONG2 = MR.attr.motionDurationLong2 to 500
|
||||
// val LONG3 = MR.attr.motionDurationLong3 to 550
|
||||
// val LONG4 = MR.attr.motionDurationLong4 to 600
|
||||
// val EXTRA_LONG1 = MR.attr.motionDurationExtraLong1 to 700
|
||||
// val EXTRA_LONG2 = MR.attr.motionDurationExtraLong2 to 800
|
||||
// val EXTRA_LONG3 = MR.attr.motionDurationExtraLong3 to 900
|
||||
// val EXTRA_LONG4 = MR.attr.motionDurationExtraLong4 to 1000
|
||||
|
||||
fun of(context: Context, @AttrRes interpolator: Int, duration: Pair<Int, Int>) =
|
||||
AnimConfig(context, interpolator, duration.first, duration.second)
|
||||
@@ -85,34 +65,162 @@ class AnimConfig(
|
||||
}
|
||||
}
|
||||
|
||||
class MaterialCornerAnim(context: Context) {
|
||||
private val config = AnimConfig.of(context, AnimConfig.STANDARD, AnimConfig.MEDIUM2)
|
||||
interface MotionHandle {
|
||||
fun start()
|
||||
|
||||
fun animate(button: MaterialButton, sizeDp: Float): Animator {
|
||||
val shapeModel = button.shapeAppearanceModel
|
||||
val bounds = Rect(0, 0, button.width, button.height)
|
||||
val start = shapeModel.topRightCornerSize.getCornerSize(bounds.toRectF())
|
||||
return config.genericFloat(start, sizeDp) { size ->
|
||||
button.shapeAppearanceModel = shapeModel.withCornerSize { size }
|
||||
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)
|
||||
}
|
||||
}
|
||||
return object : MotionHandle {
|
||||
override fun start() {
|
||||
animation.animateToFinalPosition(finalValue)
|
||||
}
|
||||
|
||||
override fun cancel() {
|
||||
animation.cancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
@AttrRes outInterpolator: Int,
|
||||
alphaOutDuration: Pair<Int, Int>,
|
||||
scaleOutDuration: Pair<Int, Int>,
|
||||
inInterpolator: Int,
|
||||
alphaInDuration: Pair<Int, Int>,
|
||||
scaleInDuration: Pair<Int, Int>,
|
||||
alphaOutToken: SpringToken,
|
||||
scaleOutToken: SpringToken,
|
||||
alphaInToken: SpringToken,
|
||||
scaleInToken: SpringToken,
|
||||
scaleOutDampingRatioOverride: Float? = null,
|
||||
scaleInDampingRatioOverride: Float? = null,
|
||||
) {
|
||||
private val alphaOutConfig = AnimConfig.of(context, outInterpolator, alphaOutDuration)
|
||||
private val scaleOutConfig = AnimConfig.of(context, outInterpolator, scaleOutDuration)
|
||||
private val alphaInConfig = AnimConfig.of(context, inInterpolator, alphaInDuration)
|
||||
private val scaleInConfig = AnimConfig.of(context, inInterpolator, scaleInDuration)
|
||||
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 {
|
||||
@@ -132,75 +240,132 @@ private constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun fadeOut(view: View): Animator {
|
||||
fun fadeOut(view: View, onEnd: (() -> Unit)? = null): MotionHandle {
|
||||
if (!view.isLaidOut) {
|
||||
jumpToFadeOut(view)
|
||||
return AnimatorSet()
|
||||
return SnapMotionHandle {
|
||||
jumpToFadeOut(view)
|
||||
onEnd?.invoke()
|
||||
}
|
||||
}
|
||||
|
||||
val alphaAnimator =
|
||||
alphaOutConfig.genericFloat(view.alpha, 0f) {
|
||||
view.alpha = it
|
||||
view.isInvisible = view.alpha == 0f
|
||||
}
|
||||
val scaleXAnimator = scaleOutConfig.genericFloat(view.scaleX, scale) { view.scaleX = it }
|
||||
val scaleYAnimator = scaleOutConfig.genericFloat(view.scaleY, scale) { view.scaleY = it }
|
||||
return AnimatorSet().apply { playTogether(alphaAnimator, scaleXAnimator, scaleYAnimator) }
|
||||
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): Animator {
|
||||
fun fadeIn(view: View, onEnd: (() -> Unit)? = null): MotionHandle {
|
||||
if (!view.isLaidOut) {
|
||||
jumpToFadeIn(view)
|
||||
return AnimatorSet()
|
||||
}
|
||||
val alphaAnimator =
|
||||
alphaInConfig.genericFloat(view.alpha, 1f) {
|
||||
view.alpha = it
|
||||
view.isInvisible = view.alpha == 0f
|
||||
return SnapMotionHandle {
|
||||
jumpToFadeIn(view)
|
||||
onEnd?.invoke()
|
||||
}
|
||||
val scaleXAnimator = scaleInConfig.genericFloat(view.scaleX, 1.0f) { view.scaleX = it }
|
||||
val scaleYAnimator = scaleInConfig.genericFloat(view.scaleY, 1.0f) { view.scaleY = it }
|
||||
return AnimatorSet().apply { playTogether(alphaAnimator, scaleXAnimator, scaleYAnimator) }
|
||||
}
|
||||
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 quickLopsided(context: Context) =
|
||||
MaterialFader(
|
||||
context,
|
||||
0.6f,
|
||||
AnimConfig.EMPHASIZED_ACCELERATE,
|
||||
AnimConfig.SHORT1,
|
||||
AnimConfig.SHORT3,
|
||||
AnimConfig.EMPHASIZED_DECELERATE,
|
||||
AnimConfig.SHORT1,
|
||||
AnimConfig.MEDIUM3,
|
||||
)
|
||||
|
||||
fun symmetric(context: Context) =
|
||||
fun new(context: Context) =
|
||||
MaterialFader(
|
||||
context,
|
||||
0.9f,
|
||||
AnimConfig.EMPHASIZED_ACCELERATE,
|
||||
AnimConfig.SHORT3,
|
||||
AnimConfig.MEDIUM1,
|
||||
AnimConfig.EMPHASIZED_DECELERATE,
|
||||
AnimConfig.SHORT3,
|
||||
AnimConfig.MEDIUM1,
|
||||
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,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
class MaterialFlipper(context: Context) {
|
||||
private val fader = MaterialFader.symmetric(context)
|
||||
private val fader = MaterialFader.new(context)
|
||||
|
||||
fun jump(from: View) {
|
||||
fader.jumpToFadeOut(from)
|
||||
}
|
||||
|
||||
fun flip(from: View, to: View): Animator {
|
||||
val outAnimator = fader.fadeOut(from)
|
||||
val inAnimator = fader.fadeIn(to).apply { startDelay = outAnimator.totalDuration }
|
||||
return AnimatorSet().apply { playTogether(outAnimator, inAnimator) }
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,65 +373,56 @@ class MaterialSlider
|
||||
private constructor(
|
||||
context: Context,
|
||||
private val x: Int?,
|
||||
inDuration: Pair<Int, Int>,
|
||||
outDuration: Pair<Int, Int>,
|
||||
inSpringToken: SpringToken,
|
||||
outSpringToken: SpringToken,
|
||||
) {
|
||||
private val outConfig = AnimConfig.of(context, AnimConfig.EMPHASIZED_ACCELERATE, outDuration)
|
||||
private val inConfig = AnimConfig.of(context, AnimConfig.EMPHASIZED_DECELERATE, inDuration)
|
||||
private val outSpring = MotionSpring(context, outSpringToken)
|
||||
private val inSpring = MotionSpring(context, inSpringToken)
|
||||
|
||||
fun jumpOut(view: View) {
|
||||
if (x == null) {
|
||||
view.translationX = 100000f
|
||||
}
|
||||
view.translationX = (x ?: view.width).toFloat()
|
||||
view.translationX = view.resolveSlideTarget(x)
|
||||
}
|
||||
|
||||
fun slideOut(view: View): Animator {
|
||||
val target = (x ?: view.width).toFloat()
|
||||
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
|
||||
}
|
||||
val animator = outConfig.genericFloat(view.translationX, target) { view.translationX = it }
|
||||
return animator
|
||||
return outSpring.create(
|
||||
view.translationX,
|
||||
target,
|
||||
SPATIAL_MIN_VISIBLE_CHANGE,
|
||||
update = { view.translationX = it },
|
||||
)
|
||||
}
|
||||
|
||||
fun slideIn(view: View): Animator {
|
||||
val animator = inConfig.genericFloat(view.translationX, 0f) { view.translationX = it }
|
||||
return animator
|
||||
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, AnimConfig.SHORT3, AnimConfig.MEDIUM1)
|
||||
|
||||
fun large(context: Context, x: Int?) =
|
||||
MaterialSlider(context, x, AnimConfig.MEDIUM3, AnimConfig.SHORT3)
|
||||
MaterialSlider(
|
||||
context,
|
||||
x,
|
||||
MotionSpringTokens.FAST_SPATIAL,
|
||||
MotionSpringTokens.DEFAULT_SPATIAL,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
class MaterialFadingSlider(private val slider: MaterialSlider) {
|
||||
fun jumpOut(view: View) {
|
||||
slider.jumpOut(view)
|
||||
view.alpha = 0f
|
||||
}
|
||||
|
||||
fun slideOut(view: View): Animator {
|
||||
val slideOut = slider.slideOut(view)
|
||||
val alphaOut =
|
||||
ValueAnimator.ofFloat(1f, 0f).apply {
|
||||
duration = slideOut.duration
|
||||
addUpdateListener { view.alpha = it.animatedValue as Float }
|
||||
}
|
||||
return AnimatorSet().apply { playTogether(slideOut, alphaOut) }
|
||||
}
|
||||
|
||||
fun slideIn(view: View): Animator {
|
||||
val slideIn = slider.slideIn(view)
|
||||
val alphaIn =
|
||||
ValueAnimator.ofFloat(0f, 1f).apply {
|
||||
duration = slideIn.duration
|
||||
addUpdateListener { view.alpha = it.animatedValue as Float }
|
||||
}
|
||||
return AnimatorSet().apply { playTogether(slideIn, alphaIn) }
|
||||
}
|
||||
}
|
||||
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
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
|
||||
package org.oxycblt.auxio.ui
|
||||
|
||||
import android.animation.Animator
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.widget.FrameLayout
|
||||
@@ -31,7 +30,7 @@ class MultiToolbar
|
||||
@JvmOverloads
|
||||
constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr: Int = 0) :
|
||||
FrameLayout(context, attrs, defStyleAttr) {
|
||||
private var animator: Animator? = null
|
||||
private var animator: MotionHandle? = null
|
||||
private var currentlyVisible = 0
|
||||
private val flipper = MaterialFlipper(context)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user