ui: migrate motion to m3
This commit is contained in:
@@ -18,20 +18,18 @@
|
|||||||
|
|
||||||
package org.oxycblt.auxio.home
|
package org.oxycblt.auxio.home
|
||||||
|
|
||||||
import android.animation.Animator
|
import android.animation.ArgbEvaluator
|
||||||
import android.animation.AnimatorListenerAdapter
|
import android.graphics.Canvas
|
||||||
import android.animation.AnimatorSet
|
import android.graphics.ColorFilter
|
||||||
import android.animation.ObjectAnimator
|
import android.graphics.PixelFormat
|
||||||
|
import android.graphics.PorterDuff
|
||||||
|
import android.graphics.Rect
|
||||||
|
import android.graphics.drawable.Drawable
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.res.ColorStateList
|
import android.content.res.ColorStateList
|
||||||
import android.graphics.drawable.Drawable
|
|
||||||
import android.graphics.drawable.RotateDrawable
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Parcelable
|
import android.os.Parcelable
|
||||||
import android.util.AttributeSet
|
import android.util.AttributeSet
|
||||||
import android.util.Property
|
|
||||||
import android.view.View
|
|
||||||
import android.widget.ImageView
|
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
import androidx.annotation.AttrRes
|
import androidx.annotation.AttrRes
|
||||||
import androidx.annotation.FloatRange
|
import androidx.annotation.FloatRange
|
||||||
@@ -39,6 +37,11 @@ import androidx.core.os.BundleCompat
|
|||||||
import androidx.core.view.setMargins
|
import androidx.core.view.setMargins
|
||||||
import androidx.core.view.updateLayoutParams
|
import androidx.core.view.updateLayoutParams
|
||||||
import androidx.core.widget.TextViewCompat
|
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.google.android.material.shape.MaterialShapeDrawable
|
||||||
import com.leinardi.android.speeddial.FabWithLabelView
|
import com.leinardi.android.speeddial.FabWithLabelView
|
||||||
import com.leinardi.android.speeddial.SpeedDialActionItem
|
import com.leinardi.android.speeddial.SpeedDialActionItem
|
||||||
@@ -46,7 +49,6 @@ import com.leinardi.android.speeddial.SpeedDialView
|
|||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
import kotlinx.parcelize.Parcelize
|
import kotlinx.parcelize.Parcelize
|
||||||
import org.oxycblt.auxio.R
|
import org.oxycblt.auxio.R
|
||||||
import org.oxycblt.auxio.ui.AnimConfig
|
|
||||||
import org.oxycblt.auxio.util.getAttrColorCompat
|
import org.oxycblt.auxio.util.getAttrColorCompat
|
||||||
import org.oxycblt.auxio.util.getAttrResourceId
|
import org.oxycblt.auxio.util.getAttrResourceId
|
||||||
import org.oxycblt.auxio.util.getDimen
|
import org.oxycblt.auxio.util.getDimen
|
||||||
@@ -65,9 +67,23 @@ import org.oxycblt.auxio.util.getDimenPixels
|
|||||||
* @author Hai Zhang, Alexander Capehart (OxygenCobalt)
|
* @author Hai Zhang, Alexander Capehart (OxygenCobalt)
|
||||||
*/
|
*/
|
||||||
class ThemedSpeedDialView : SpeedDialView {
|
class ThemedSpeedDialView : SpeedDialView {
|
||||||
private var mainFabAnimator: Animator? = null
|
private var mainFabAnimation: MainFabAnimation? = null
|
||||||
private val spacingSmall = context.getDimenPixels(R.dimen.spacing_small)
|
private val spacingSmall = context.getDimenPixels(R.dimen.spacing_small)
|
||||||
private var innerChangeListener: ((Boolean) -> Unit)? = null
|
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)
|
constructor(context: Context) : super(context)
|
||||||
|
|
||||||
@@ -79,8 +95,6 @@ class ThemedSpeedDialView : SpeedDialView {
|
|||||||
@AttrRes defStyleAttr: Int,
|
@AttrRes defStyleAttr: Int,
|
||||||
) : super(context, attrs, defStyleAttr)
|
) : super(context, attrs, defStyleAttr)
|
||||||
|
|
||||||
private val stationaryConfig = AnimConfig.of(context, AnimConfig.STANDARD, AnimConfig.MEDIUM2)
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
// Work around ripple bug on Android 12 when useCompatPadding = true.
|
// Work around ripple bug on Android 12 when useCompatPadding = true.
|
||||||
// @see https://github.com/material-components/material-components-android/issues/2617
|
// @see https://github.com/material-components/material-components-android/issues/2617
|
||||||
@@ -107,18 +121,15 @@ class ThemedSpeedDialView : SpeedDialView {
|
|||||||
.defaultColor
|
.defaultColor
|
||||||
|
|
||||||
// Always use our own animation to fix the library issue that ripple is rotated as well.
|
// 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
|
mainFabAnimationRotateAngle = 0f
|
||||||
setMainFabClosedDrawable(mainFabDrawable)
|
setMainFabClosedDrawable(mainFabDrawable)
|
||||||
|
setMainFabOpenedDrawable(null)
|
||||||
setOnChangeListener(
|
setOnChangeListener(
|
||||||
object : OnChangeListener {
|
object : OnChangeListener {
|
||||||
override fun onMainActionSelected(): Boolean = false
|
override fun onMainActionSelected(): Boolean = false
|
||||||
|
|
||||||
override fun onToggleChanged(isOpen: Boolean) {
|
override fun onToggleChanged(isOpen: Boolean) {
|
||||||
|
mainFabAnimation?.cancel()
|
||||||
mainFab.backgroundTintList =
|
mainFab.backgroundTintList =
|
||||||
ColorStateList.valueOf(
|
ColorStateList.valueOf(
|
||||||
if (isOpen) mainFabClosedBackgroundColor
|
if (isOpen) mainFabClosedBackgroundColor
|
||||||
@@ -128,68 +139,84 @@ class ThemedSpeedDialView : SpeedDialView {
|
|||||||
ColorStateList.valueOf(
|
ColorStateList.valueOf(
|
||||||
if (isOpen) mainFabClosedIconColor else mainFabOpenedIconColor
|
if (isOpen) mainFabClosedIconColor else mainFabOpenedIconColor
|
||||||
)
|
)
|
||||||
mainFabAnimator?.cancel()
|
mainFabAnimation = createMainFabAnimation(isOpen).apply { start() }
|
||||||
mainFabAnimator =
|
|
||||||
createMainFabAnimator(isOpen).apply {
|
|
||||||
addListener(
|
|
||||||
object : AnimatorListenerAdapter() {
|
|
||||||
override fun onAnimationEnd(animation: Animator) {
|
|
||||||
mainFabAnimator = null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
start()
|
|
||||||
}
|
|
||||||
innerChangeListener?.invoke(isOpen)
|
innerChangeListener?.invoke(isOpen)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createMainFabAnimator(isOpen: Boolean): Animator {
|
private fun createMainFabAnimation(isOpen: Boolean): MainFabAnimation {
|
||||||
val totalDuration = stationaryConfig.duration
|
val targetBackgroundTint =
|
||||||
val partialDuration = totalDuration / 2 // This is half of the total duration
|
if (isOpen) mainFabOpenedBackgroundColor else mainFabClosedBackgroundColor
|
||||||
val delay = totalDuration / 4 // This is one fourth of the total duration
|
val targetImageTint = if (isOpen) mainFabOpenedIconColor else mainFabClosedIconColor
|
||||||
|
|
||||||
val backgroundTintAnimator =
|
val backgroundTintAnimation =
|
||||||
ObjectAnimator.ofArgb(
|
createColorSpringAnimation(
|
||||||
mainFab,
|
startColor =
|
||||||
VIEW_PROPERTY_BACKGROUND_TINT,
|
if (isOpen) mainFabClosedBackgroundColor else mainFabOpenedBackgroundColor,
|
||||||
if (isOpen) mainFabOpenedBackgroundColor else mainFabClosedBackgroundColor,
|
endColor = targetBackgroundTint,
|
||||||
)
|
) { mainFab.backgroundTintList = ColorStateList.valueOf(it) }
|
||||||
.apply {
|
val imageTintAnimation =
|
||||||
startDelay = delay
|
createColorSpringAnimation(
|
||||||
duration = partialDuration
|
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 =
|
return MainFabAnimation(
|
||||||
ObjectAnimator.ofArgb(
|
listOf(backgroundTintAnimation, imageTintAnimation, rotationAnimation)
|
||||||
mainFab,
|
) {
|
||||||
IMAGE_VIEW_PROPERTY_IMAGE_TINT,
|
mainFabAnimation = null
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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() {
|
override fun onAttachedToWindow() {
|
||||||
super.onAttachedToWindow()
|
super.onAttachedToWindow()
|
||||||
|
|
||||||
@@ -304,34 +331,140 @@ class ThemedSpeedDialView : SpeedDialView {
|
|||||||
innerChangeListener = listener
|
innerChangeListener = listener
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
@Parcelize
|
||||||
private val VIEW_PROPERTY_BACKGROUND_TINT =
|
private class State(val superState: Parcelable?, val isOpen: Boolean) : Parcelable
|
||||||
object : Property<View, Int>(Int::class.java, "backgroundTint") {
|
|
||||||
override fun get(view: View): Int = view.backgroundTintList!!.defaultColor
|
|
||||||
|
|
||||||
override fun set(view: View, value: Int?) {
|
private class MainFabAnimation(
|
||||||
view.backgroundTintList = ColorStateList.valueOf(value!!)
|
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 =
|
fun start() {
|
||||||
object : Property<ImageView, Int>(Int::class.java, "imageTint") {
|
animations.forEach { animation ->
|
||||||
override fun get(view: ImageView): Int = view.imageTintList!!.defaultColor
|
animation.animateToFinalPosition(animation.spring.finalPosition)
|
||||||
|
|
||||||
override fun set(view: ImageView, value: Int?) {
|
|
||||||
view.imageTintList = ColorStateList.valueOf(value!!)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private val DRAWABLE_PROPERTY_LEVEL =
|
fun cancel() {
|
||||||
object : Property<Drawable, Int>(Int::class.java, "level") {
|
if (finished) {
|
||||||
override fun get(drawable: Drawable): Int = drawable.level
|
return
|
||||||
|
|
||||||
override fun set(drawable: Drawable, value: Int?) {
|
|
||||||
drawable.level = value!!
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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
|
package org.oxycblt.auxio.image
|
||||||
|
|
||||||
import android.animation.Animator
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.graphics.Canvas
|
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.SquareCropTransformation
|
||||||
import org.oxycblt.auxio.image.coil.StackCoverCollection
|
import org.oxycblt.auxio.image.coil.StackCoverCollection
|
||||||
import org.oxycblt.auxio.ui.MaterialFader
|
import org.oxycblt.auxio.ui.MaterialFader
|
||||||
|
import org.oxycblt.auxio.ui.MotionHandle
|
||||||
import org.oxycblt.auxio.ui.UISettings
|
import org.oxycblt.auxio.ui.UISettings
|
||||||
import org.oxycblt.auxio.util.getAttrColorCompat
|
import org.oxycblt.auxio.util.getAttrColorCompat
|
||||||
import org.oxycblt.auxio.util.getColorCompat
|
import org.oxycblt.auxio.util.getColorCompat
|
||||||
@@ -104,8 +104,8 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
|
|||||||
private val selectionBadge: ImageView?
|
private val selectionBadge: ImageView?
|
||||||
private val iconSize: Int?
|
private val iconSize: Int?
|
||||||
|
|
||||||
private val fader = MaterialFader.quickLopsided(context)
|
private val fader = MaterialFader.new(context)
|
||||||
private var fadeAnimator: Animator? = null
|
private var fadeAnimator: MotionHandle? = null
|
||||||
private val indicatorMatrix = Matrix()
|
private val indicatorMatrix = Matrix()
|
||||||
private val indicatorMatrixSrc = RectF()
|
private val indicatorMatrixSrc = RectF()
|
||||||
private val indicatorMatrixDst = RectF()
|
private val indicatorMatrixDst = RectF()
|
||||||
|
|||||||
@@ -18,7 +18,6 @@
|
|||||||
|
|
||||||
package org.oxycblt.auxio.list.recycler
|
package org.oxycblt.auxio.list.recycler
|
||||||
|
|
||||||
import android.animation.Animator
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.graphics.Canvas
|
import android.graphics.Canvas
|
||||||
@@ -63,6 +62,7 @@ import kotlin.math.roundToInt
|
|||||||
import org.oxycblt.auxio.R
|
import org.oxycblt.auxio.R
|
||||||
import org.oxycblt.auxio.ui.ExpressiveShapes
|
import org.oxycblt.auxio.ui.ExpressiveShapes
|
||||||
import org.oxycblt.auxio.ui.MaterialSlider
|
import org.oxycblt.auxio.ui.MaterialSlider
|
||||||
|
import org.oxycblt.auxio.ui.MotionHandle
|
||||||
import org.oxycblt.auxio.util.getAttrColorCompat
|
import org.oxycblt.auxio.util.getAttrColorCompat
|
||||||
import org.oxycblt.auxio.util.getAttrResourceId
|
import org.oxycblt.auxio.util.getAttrResourceId
|
||||||
import org.oxycblt.auxio.util.getDimenPixels
|
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 thumbWidth = context.getDimenPixels(R.dimen.spacing_mid_medium)
|
||||||
private val thumbHeight = context.getDimenPixels(R.dimen.size_touchable_medium)
|
private val thumbHeight = context.getDimenPixels(R.dimen.size_touchable_medium)
|
||||||
private val thumbSlider = MaterialSlider.small(context, thumbWidth)
|
private val thumbSlider = MaterialSlider.small(context, thumbWidth)
|
||||||
private var thumbAnimator: Animator? = null
|
private var thumbAnimator: MotionHandle? = null
|
||||||
|
|
||||||
@SuppressLint("InflateParams")
|
@SuppressLint("InflateParams")
|
||||||
private val thumbView =
|
private val thumbView =
|
||||||
|
|||||||
@@ -18,19 +18,18 @@
|
|||||||
|
|
||||||
package org.oxycblt.auxio.ui
|
package org.oxycblt.auxio.ui
|
||||||
|
|
||||||
import android.animation.Animator
|
|
||||||
import android.animation.AnimatorSet
|
|
||||||
import android.animation.TimeInterpolator
|
import android.animation.TimeInterpolator
|
||||||
import android.animation.ValueAnimator
|
import android.animation.ValueAnimator
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.graphics.Rect
|
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import androidx.annotation.AttrRes
|
import androidx.annotation.AttrRes
|
||||||
import androidx.core.graphics.toRectF
|
import androidx.annotation.StyleRes
|
||||||
import androidx.core.view.isInvisible
|
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 androidx.interpolator.view.animation.FastOutSlowInInterpolator
|
||||||
import com.google.android.material.R as MR
|
import com.google.android.material.R as MR
|
||||||
import com.google.android.material.button.MaterialButton
|
|
||||||
import com.google.android.material.motion.MotionUtils
|
import com.google.android.material.motion.MotionUtils
|
||||||
|
|
||||||
class AnimConfig(
|
class AnimConfig(
|
||||||
@@ -46,26 +45,7 @@ class AnimConfig(
|
|||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val STANDARD = MR.attr.motionEasingStandardInterpolator
|
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 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>) =
|
fun of(context: Context, @AttrRes interpolator: Int, duration: Pair<Int, Int>) =
|
||||||
AnimConfig(context, interpolator, duration.first, duration.second)
|
AnimConfig(context, interpolator, duration.first, duration.second)
|
||||||
@@ -85,34 +65,162 @@ class AnimConfig(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class MaterialCornerAnim(context: Context) {
|
interface MotionHandle {
|
||||||
private val config = AnimConfig.of(context, AnimConfig.STANDARD, AnimConfig.MEDIUM2)
|
fun start()
|
||||||
|
|
||||||
fun animate(button: MaterialButton, sizeDp: Float): Animator {
|
fun cancel()
|
||||||
val shapeModel = button.shapeAppearanceModel
|
}
|
||||||
val bounds = Rect(0, 0, button.width, button.height)
|
|
||||||
val start = shapeModel.topRightCornerSize.getCornerSize(bounds.toRectF())
|
private object NoOpMotionHandle : MotionHandle {
|
||||||
return config.genericFloat(start, sizeDp) { size ->
|
override fun start() = Unit
|
||||||
button.shapeAppearanceModel = shapeModel.withCornerSize { size }
|
|
||||||
|
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
|
class MaterialFader
|
||||||
private constructor(
|
private constructor(
|
||||||
context: Context,
|
context: Context,
|
||||||
private val scale: Float,
|
private val scale: Float,
|
||||||
@AttrRes outInterpolator: Int,
|
alphaOutToken: SpringToken,
|
||||||
alphaOutDuration: Pair<Int, Int>,
|
scaleOutToken: SpringToken,
|
||||||
scaleOutDuration: Pair<Int, Int>,
|
alphaInToken: SpringToken,
|
||||||
inInterpolator: Int,
|
scaleInToken: SpringToken,
|
||||||
alphaInDuration: Pair<Int, Int>,
|
scaleOutDampingRatioOverride: Float? = null,
|
||||||
scaleInDuration: Pair<Int, Int>,
|
scaleInDampingRatioOverride: Float? = null,
|
||||||
) {
|
) {
|
||||||
private val alphaOutConfig = AnimConfig.of(context, outInterpolator, alphaOutDuration)
|
private val alphaOutSpring = MotionSpring(context, alphaOutToken)
|
||||||
private val scaleOutConfig = AnimConfig.of(context, outInterpolator, scaleOutDuration)
|
private val scaleOutSpring =
|
||||||
private val alphaInConfig = AnimConfig.of(context, inInterpolator, alphaInDuration)
|
MotionSpring(
|
||||||
private val scaleInConfig = AnimConfig.of(context, inInterpolator, scaleInDuration)
|
context,
|
||||||
|
scaleOutToken,
|
||||||
|
dampingRatioOverride = scaleOutDampingRatioOverride,
|
||||||
|
)
|
||||||
|
private val alphaInSpring = MotionSpring(context, alphaInToken)
|
||||||
|
private val scaleInSpring =
|
||||||
|
MotionSpring(
|
||||||
|
context,
|
||||||
|
scaleInToken,
|
||||||
|
dampingRatioOverride = scaleInDampingRatioOverride,
|
||||||
|
)
|
||||||
|
|
||||||
fun jumpToFadeOut(view: View) {
|
fun jumpToFadeOut(view: View) {
|
||||||
view.apply {
|
view.apply {
|
||||||
@@ -132,75 +240,132 @@ private constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun fadeOut(view: View): Animator {
|
fun fadeOut(view: View, onEnd: (() -> Unit)? = null): MotionHandle {
|
||||||
if (!view.isLaidOut) {
|
if (!view.isLaidOut) {
|
||||||
jumpToFadeOut(view)
|
return SnapMotionHandle {
|
||||||
return AnimatorSet()
|
jumpToFadeOut(view)
|
||||||
|
onEnd?.invoke()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val alphaAnimator =
|
val alphaMotion =
|
||||||
alphaOutConfig.genericFloat(view.alpha, 0f) {
|
alphaOutSpring.create(
|
||||||
view.alpha = it
|
view.alpha,
|
||||||
view.isInvisible = view.alpha == 0f
|
0f,
|
||||||
}
|
ALPHA_MIN_VISIBLE_CHANGE,
|
||||||
val scaleXAnimator = scaleOutConfig.genericFloat(view.scaleX, scale) { view.scaleX = it }
|
update = { view.alpha = it },
|
||||||
val scaleYAnimator = scaleOutConfig.genericFloat(view.scaleY, scale) { view.scaleY = it }
|
onEnd = { canceled ->
|
||||||
return AnimatorSet().apply { playTogether(alphaAnimator, scaleXAnimator, scaleYAnimator) }
|
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) {
|
if (!view.isLaidOut) {
|
||||||
jumpToFadeIn(view)
|
return SnapMotionHandle {
|
||||||
return AnimatorSet()
|
jumpToFadeIn(view)
|
||||||
}
|
onEnd?.invoke()
|
||||||
val alphaAnimator =
|
|
||||||
alphaInConfig.genericFloat(view.alpha, 1f) {
|
|
||||||
view.alpha = it
|
|
||||||
view.isInvisible = view.alpha == 0f
|
|
||||||
}
|
}
|
||||||
val scaleXAnimator = scaleInConfig.genericFloat(view.scaleX, 1.0f) { view.scaleX = it }
|
}
|
||||||
val scaleYAnimator = scaleInConfig.genericFloat(view.scaleY, 1.0f) { view.scaleY = it }
|
val alphaMotion =
|
||||||
return AnimatorSet().apply { playTogether(alphaAnimator, scaleXAnimator, scaleYAnimator) }
|
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 {
|
companion object {
|
||||||
fun quickLopsided(context: Context) =
|
fun new(context: Context) =
|
||||||
MaterialFader(
|
|
||||||
context,
|
|
||||||
0.6f,
|
|
||||||
AnimConfig.EMPHASIZED_ACCELERATE,
|
|
||||||
AnimConfig.SHORT1,
|
|
||||||
AnimConfig.SHORT3,
|
|
||||||
AnimConfig.EMPHASIZED_DECELERATE,
|
|
||||||
AnimConfig.SHORT1,
|
|
||||||
AnimConfig.MEDIUM3,
|
|
||||||
)
|
|
||||||
|
|
||||||
fun symmetric(context: Context) =
|
|
||||||
MaterialFader(
|
MaterialFader(
|
||||||
context,
|
context,
|
||||||
0.9f,
|
0.9f,
|
||||||
AnimConfig.EMPHASIZED_ACCELERATE,
|
MotionSpringTokens.FAST_EFFECTS,
|
||||||
AnimConfig.SHORT3,
|
MotionSpringTokens.FAST_SPATIAL,
|
||||||
AnimConfig.MEDIUM1,
|
MotionSpringTokens.FAST_EFFECTS,
|
||||||
AnimConfig.EMPHASIZED_DECELERATE,
|
MotionSpringTokens.FAST_SPATIAL,
|
||||||
AnimConfig.SHORT3,
|
// Material's standard spatial springs are only slightly underdamped (0.9),
|
||||||
AnimConfig.MEDIUM1,
|
// which is too restrained for the toolbar swap to read as springy.
|
||||||
|
scaleOutDampingRatioOverride = 0.6f,
|
||||||
|
scaleInDampingRatioOverride = 0.6f,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class MaterialFlipper(context: Context) {
|
class MaterialFlipper(context: Context) {
|
||||||
private val fader = MaterialFader.symmetric(context)
|
private val fader = MaterialFader.new(context)
|
||||||
|
|
||||||
fun jump(from: View) {
|
fun jump(from: View) {
|
||||||
fader.jumpToFadeOut(from)
|
fader.jumpToFadeOut(from)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun flip(from: View, to: View): Animator {
|
fun flip(from: View, to: View): MotionHandle {
|
||||||
val outAnimator = fader.fadeOut(from)
|
var canceled = false
|
||||||
val inAnimator = fader.fadeIn(to).apply { startDelay = outAnimator.totalDuration }
|
var inMotion: MotionHandle = NoOpMotionHandle
|
||||||
return AnimatorSet().apply { playTogether(outAnimator, inAnimator) }
|
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(
|
private constructor(
|
||||||
context: Context,
|
context: Context,
|
||||||
private val x: Int?,
|
private val x: Int?,
|
||||||
inDuration: Pair<Int, Int>,
|
inSpringToken: SpringToken,
|
||||||
outDuration: Pair<Int, Int>,
|
outSpringToken: SpringToken,
|
||||||
) {
|
) {
|
||||||
private val outConfig = AnimConfig.of(context, AnimConfig.EMPHASIZED_ACCELERATE, outDuration)
|
private val outSpring = MotionSpring(context, outSpringToken)
|
||||||
private val inConfig = AnimConfig.of(context, AnimConfig.EMPHASIZED_DECELERATE, inDuration)
|
private val inSpring = MotionSpring(context, inSpringToken)
|
||||||
|
|
||||||
fun jumpOut(view: View) {
|
fun jumpOut(view: View) {
|
||||||
if (x == null) {
|
view.translationX = view.resolveSlideTarget(x)
|
||||||
view.translationX = 100000f
|
|
||||||
}
|
|
||||||
view.translationX = (x ?: view.width).toFloat()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun slideOut(view: View): Animator {
|
fun slideOut(view: View): MotionHandle {
|
||||||
val target = (x ?: view.width).toFloat()
|
val target = view.resolveSlideTarget(x)
|
||||||
|
if (!view.isLaidOut) {
|
||||||
|
return SnapMotionHandle { view.translationX = target }
|
||||||
|
}
|
||||||
if (view.translationX > target) {
|
if (view.translationX > target) {
|
||||||
view.translationX = target
|
view.translationX = target
|
||||||
}
|
}
|
||||||
val animator = outConfig.genericFloat(view.translationX, target) { view.translationX = it }
|
return outSpring.create(
|
||||||
return animator
|
view.translationX,
|
||||||
|
target,
|
||||||
|
SPATIAL_MIN_VISIBLE_CHANGE,
|
||||||
|
update = { view.translationX = it },
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun slideIn(view: View): Animator {
|
fun slideIn(view: View): MotionHandle {
|
||||||
val animator = inConfig.genericFloat(view.translationX, 0f) { view.translationX = it }
|
if (!view.isLaidOut) {
|
||||||
return animator
|
return SnapMotionHandle { view.translationX = 0f }
|
||||||
|
}
|
||||||
|
return inSpring.create(
|
||||||
|
view.translationX,
|
||||||
|
0f,
|
||||||
|
SPATIAL_MIN_VISIBLE_CHANGE,
|
||||||
|
update = { view.translationX = it },
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun small(context: Context, x: Int?) =
|
fun small(context: Context, x: Int?) =
|
||||||
MaterialSlider(context, x, AnimConfig.SHORT3, AnimConfig.MEDIUM1)
|
MaterialSlider(
|
||||||
|
context,
|
||||||
fun large(context: Context, x: Int?) =
|
x,
|
||||||
MaterialSlider(context, x, AnimConfig.MEDIUM3, AnimConfig.SHORT3)
|
MotionSpringTokens.FAST_SPATIAL,
|
||||||
|
MotionSpringTokens.DEFAULT_SPATIAL,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class MaterialFadingSlider(private val slider: MaterialSlider) {
|
private const val ALPHA_MIN_VISIBLE_CHANGE = 1f / 512f
|
||||||
fun jumpOut(view: View) {
|
private const val SCALE_MIN_VISIBLE_CHANGE = 0.0001f
|
||||||
slider.jumpOut(view)
|
private const val SPATIAL_MIN_VISIBLE_CHANGE = 0.01f
|
||||||
view.alpha = 0f
|
private const val OFFSCREEN_TRANSLATION_PX = 100000f
|
||||||
}
|
|
||||||
|
|
||||||
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) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -18,7 +18,6 @@
|
|||||||
|
|
||||||
package org.oxycblt.auxio.ui
|
package org.oxycblt.auxio.ui
|
||||||
|
|
||||||
import android.animation.Animator
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.util.AttributeSet
|
import android.util.AttributeSet
|
||||||
import android.widget.FrameLayout
|
import android.widget.FrameLayout
|
||||||
@@ -31,7 +30,7 @@ class MultiToolbar
|
|||||||
@JvmOverloads
|
@JvmOverloads
|
||||||
constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr: Int = 0) :
|
constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr: Int = 0) :
|
||||||
FrameLayout(context, attrs, defStyleAttr) {
|
FrameLayout(context, attrs, defStyleAttr) {
|
||||||
private var animator: Animator? = null
|
private var animator: MotionHandle? = null
|
||||||
private var currentlyVisible = 0
|
private var currentlyVisible = 0
|
||||||
private val flipper = MaterialFlipper(context)
|
private val flipper = MaterialFlipper(context)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user