ui: make multitoolbar transition m3
This commit is contained in:
parent
d909f2d98e
commit
2f21b12beb
1 changed files with 25 additions and 20 deletions
|
@ -27,8 +27,11 @@ import androidx.annotation.IdRes
|
||||||
import androidx.appcompat.widget.Toolbar
|
import androidx.appcompat.widget.Toolbar
|
||||||
import androidx.core.view.children
|
import androidx.core.view.children
|
||||||
import androidx.core.view.isInvisible
|
import androidx.core.view.isInvisible
|
||||||
import org.oxycblt.auxio.R
|
import androidx.interpolator.view.animation.FastOutSlowInInterpolator
|
||||||
import org.oxycblt.auxio.util.getInteger
|
import com.google.android.material.R as MR
|
||||||
|
import com.google.android.material.motion.MotionUtils
|
||||||
|
import kotlin.math.max
|
||||||
|
import kotlin.math.min
|
||||||
import org.oxycblt.auxio.util.logD
|
import org.oxycblt.auxio.util.logD
|
||||||
|
|
||||||
class MultiToolbar
|
class MultiToolbar
|
||||||
|
@ -37,6 +40,11 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
|
||||||
FrameLayout(context, attrs, defStyleAttr) {
|
FrameLayout(context, attrs, defStyleAttr) {
|
||||||
private var fadeThroughAnimator: ValueAnimator? = null
|
private var fadeThroughAnimator: ValueAnimator? = null
|
||||||
private var currentlyVisible = 0
|
private var currentlyVisible = 0
|
||||||
|
private val matInterpolator =
|
||||||
|
MotionUtils.resolveThemeInterpolator(
|
||||||
|
context, MR.attr.motionEasingStandardInterpolator, FastOutSlowInInterpolator())
|
||||||
|
private val matDuration =
|
||||||
|
MotionUtils.resolveThemeDuration(context, MR.attr.motionDurationMedium3, 300).toLong()
|
||||||
|
|
||||||
override fun onFinishInflate() {
|
override fun onFinishInflate() {
|
||||||
super.onFinishInflate()
|
super.onFinishInflate()
|
||||||
|
@ -61,16 +69,6 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
|
||||||
// Set up the target transitions for both the inner and selection toolbars.
|
// Set up the target transitions for both the inner and selection toolbars.
|
||||||
val targetFromAlpha = 0f
|
val targetFromAlpha = 0f
|
||||||
val targetToAlpha = 1f
|
val targetToAlpha = 1f
|
||||||
val targetDuration =
|
|
||||||
// Since this view starts with the lowest toolbar index,
|
|
||||||
if (from < to) {
|
|
||||||
logD("Moving higher, use an entrance animation")
|
|
||||||
context.getInteger(R.integer.anim_fade_enter_duration).toLong()
|
|
||||||
} else {
|
|
||||||
logD("Moving lower, use an exit animation")
|
|
||||||
context.getInteger(R.integer.anim_fade_exit_duration).toLong()
|
|
||||||
}
|
|
||||||
|
|
||||||
val fromView = getChildAt(from) as Toolbar
|
val fromView = getChildAt(from) as Toolbar
|
||||||
val toView = getChildAt(to) as Toolbar
|
val toView = getChildAt(to) as Toolbar
|
||||||
|
|
||||||
|
@ -91,7 +89,8 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
|
||||||
fadeThroughAnimator?.cancel()
|
fadeThroughAnimator?.cancel()
|
||||||
fadeThroughAnimator =
|
fadeThroughAnimator =
|
||||||
ValueAnimator.ofFloat(fromView.alpha, targetFromAlpha).apply {
|
ValueAnimator.ofFloat(fromView.alpha, targetFromAlpha).apply {
|
||||||
duration = targetDuration
|
duration = matDuration
|
||||||
|
interpolator = matInterpolator
|
||||||
addUpdateListener { setToolbarsAlpha(fromView, toView, it.animatedValue as Float) }
|
addUpdateListener { setToolbarsAlpha(fromView, toView, it.animatedValue as Float) }
|
||||||
start()
|
start()
|
||||||
}
|
}
|
||||||
|
@ -99,15 +98,21 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setToolbarsAlpha(from: Toolbar, to: Toolbar, innerAlpha: Float) {
|
private fun setToolbarsAlpha(from: Toolbar, to: Toolbar, ratio: Float) {
|
||||||
from.apply {
|
val outRatio = min(ratio * 2, 1f)
|
||||||
alpha = innerAlpha
|
to.apply {
|
||||||
isInvisible = innerAlpha == 0f
|
scaleX = 1 - 0.05f * outRatio
|
||||||
|
scaleY = 1 - 0.05f * outRatio
|
||||||
|
alpha = 1 - outRatio
|
||||||
|
isInvisible = alpha == 0f
|
||||||
}
|
}
|
||||||
|
|
||||||
to.apply {
|
val inRatio = max(ratio - 0.5f, 0f) * 2
|
||||||
alpha = 1 - innerAlpha
|
from.apply {
|
||||||
isInvisible = innerAlpha == 1f
|
scaleX = 1 - 0.05f * (1 - inRatio)
|
||||||
|
scaleY = 1 - 0.05f * (1 - inRatio)
|
||||||
|
alpha = inRatio
|
||||||
|
isInvisible = alpha == 0f
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue