mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-22 18:00:02 +01:00
Implement animated color lerp for material toolbars.
This commit is contained in:
committed by
Greyson Parrelli
parent
ef3c776b4b
commit
2eb933c2d4
@@ -1,10 +1,12 @@
|
||||
package org.thoughtcrime.securesms.util
|
||||
|
||||
import android.animation.ValueAnimator
|
||||
import android.app.Activity
|
||||
import android.os.Build
|
||||
import android.view.View
|
||||
import androidx.annotation.ColorInt
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.google.android.material.animation.ArgbEvaluatorCompat
|
||||
import org.thoughtcrime.securesms.R
|
||||
import org.thoughtcrime.securesms.util.views.Stub
|
||||
|
||||
@@ -13,20 +15,36 @@ import org.thoughtcrime.securesms.util.views.Stub
|
||||
* This can be used to appropriately tint toolbar backgrounds. Also can emit the state change
|
||||
* for other purposes.
|
||||
*/
|
||||
class Material3OnScrollHelper(
|
||||
open class Material3OnScrollHelper(
|
||||
private val activity: Activity,
|
||||
private val views: List<View>,
|
||||
private val viewStubs: List<Stub<out View>> = emptyList(),
|
||||
private val onActiveStateChanged: (Boolean) -> Unit
|
||||
) : RecyclerView.OnScrollListener() {
|
||||
private val viewStubs: List<Stub<out View>> = emptyList()
|
||||
) {
|
||||
|
||||
constructor(activity: Activity, views: List<View>, viewStubs: List<Stub<out View>>) : this(views, viewStubs, { updateStatusBarColor(activity, it) })
|
||||
open val activeColorRes: Int = R.color.signal_colorSurface2
|
||||
open val inactiveColorRes: Int = R.color.signal_colorBackground
|
||||
|
||||
constructor(activity: Activity, view: View) : this(listOf(view), emptyList(), { updateStatusBarColor(activity, it) })
|
||||
constructor(activity: Activity, view: View) : this(activity, listOf(view), emptyList())
|
||||
|
||||
private var animator: ValueAnimator? = null
|
||||
private var active: Boolean? = null
|
||||
private val scrollListener = OnScrollListener()
|
||||
|
||||
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
|
||||
updateActiveState(recyclerView.canScrollVertically(-1))
|
||||
fun attach(recyclerView: RecyclerView) {
|
||||
recyclerView.addOnScrollListener(scrollListener)
|
||||
scrollListener.onScrolled(recyclerView, 0, 0)
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels any currently running scroll animation and sets the color immediately.
|
||||
*/
|
||||
fun setColorImmediate() {
|
||||
if (active == null) {
|
||||
return
|
||||
}
|
||||
|
||||
animator?.cancel()
|
||||
setColor(ContextCompat.getColor(activity, if (active == true) activeColorRes else inactiveColorRes))
|
||||
}
|
||||
|
||||
private fun updateActiveState(isActive: Boolean) {
|
||||
@@ -34,23 +52,41 @@ class Material3OnScrollHelper(
|
||||
return
|
||||
}
|
||||
|
||||
val hadActiveState = active != null
|
||||
active = isActive
|
||||
|
||||
views.forEach { it.isActivated = isActive }
|
||||
viewStubs.filter { it.resolved() }.forEach { it.get().isActivated = isActive }
|
||||
|
||||
onActiveStateChanged(isActive)
|
||||
}
|
||||
if (animator?.isRunning == true) {
|
||||
animator?.reverse()
|
||||
} else {
|
||||
val startColor = ContextCompat.getColor(activity, if (isActive) inactiveColorRes else activeColorRes)
|
||||
val endColor = ContextCompat.getColor(activity, if (isActive) activeColorRes else inactiveColorRes)
|
||||
|
||||
companion object {
|
||||
fun updateStatusBarColor(activity: Activity, isActive: Boolean) {
|
||||
if (Build.VERSION.SDK_INT > 21) {
|
||||
if (isActive) {
|
||||
WindowUtil.setStatusBarColor(activity.window, ContextCompat.getColor(activity, R.color.signal_colorSurface2))
|
||||
} else {
|
||||
WindowUtil.setStatusBarColor(activity.window, ContextCompat.getColor(activity, R.color.signal_colorBackground))
|
||||
if (hadActiveState) {
|
||||
animator = ValueAnimator.ofObject(ArgbEvaluatorCompat(), startColor, endColor).apply {
|
||||
duration = 200
|
||||
addUpdateListener { animator ->
|
||||
setColor(animator.animatedValue as Int)
|
||||
}
|
||||
start()
|
||||
}
|
||||
} else {
|
||||
setColorImmediate()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setColor(@ColorInt color: Int) {
|
||||
WindowUtil.setStatusBarColor(activity.window, color)
|
||||
views.forEach { it.setBackgroundColor(color) }
|
||||
viewStubs.filter { it.resolved() }.forEach { it.get().setBackgroundColor(color) }
|
||||
}
|
||||
|
||||
private inner class OnScrollListener : RecyclerView.OnScrollListener() {
|
||||
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
|
||||
updateActiveState(recyclerView.canScrollVertically(-1))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user