2017-01-22 23:13:07 +01:00
|
|
|
package eu.kanade.tachiyomi.widget
|
|
|
|
|
|
|
|
import android.animation.ObjectAnimator
|
|
|
|
import android.animation.StateListAnimator
|
|
|
|
import android.content.Context
|
2021-01-18 23:24:23 +01:00
|
|
|
import android.os.Build
|
2020-01-29 04:47:57 +01:00
|
|
|
import android.util.AttributeSet
|
2020-01-05 17:29:27 +01:00
|
|
|
import com.google.android.material.R
|
|
|
|
import com.google.android.material.appbar.AppBarLayout
|
2017-01-22 23:13:07 +01:00
|
|
|
|
|
|
|
class ElevationAppBarLayout @JvmOverloads constructor(
|
2020-02-27 00:03:34 +01:00
|
|
|
context: Context,
|
|
|
|
attrs: AttributeSet? = null
|
2017-01-22 23:13:07 +01:00
|
|
|
) : AppBarLayout(context, attrs) {
|
|
|
|
|
|
|
|
private var origStateAnimator: StateListAnimator? = null
|
|
|
|
|
|
|
|
init {
|
2020-01-08 00:46:31 +01:00
|
|
|
origStateAnimator = stateListAnimator
|
2017-01-22 23:13:07 +01:00
|
|
|
}
|
|
|
|
|
2020-08-10 18:04:51 +02:00
|
|
|
fun enableElevation(liftOnScroll: Boolean) {
|
2021-01-18 23:24:23 +01:00
|
|
|
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
// Delay to avoid crash
|
|
|
|
post { setElevation(liftOnScroll) }
|
|
|
|
} else {
|
|
|
|
setElevation(liftOnScroll)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun setElevation(liftOnScroll: Boolean) {
|
2020-01-08 00:46:31 +01:00
|
|
|
stateListAnimator = origStateAnimator
|
2020-08-10 18:04:51 +02:00
|
|
|
isLiftOnScroll = liftOnScroll
|
2017-01-22 23:13:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fun disableElevation() {
|
2020-01-08 00:46:31 +01:00
|
|
|
stateListAnimator = StateListAnimator().apply {
|
|
|
|
val objAnimator = ObjectAnimator.ofFloat(this, "elevation", 0f)
|
2017-01-22 23:13:07 +01:00
|
|
|
|
2020-01-08 00:46:31 +01:00
|
|
|
// Enabled and collapsible, but not collapsed means not elevated
|
2020-04-25 20:24:45 +02:00
|
|
|
addState(
|
|
|
|
intArrayOf(android.R.attr.enabled, R.attr.state_collapsible, -R.attr.state_collapsed),
|
|
|
|
objAnimator
|
|
|
|
)
|
2017-01-22 23:13:07 +01:00
|
|
|
|
2020-01-08 00:46:31 +01:00
|
|
|
// Default enabled state
|
|
|
|
addState(intArrayOf(android.R.attr.enabled), objAnimator)
|
2017-01-22 23:13:07 +01:00
|
|
|
|
2020-01-08 00:46:31 +01:00
|
|
|
// Disabled state
|
|
|
|
addState(IntArray(0), objAnimator)
|
2017-01-22 23:13:07 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-05 17:29:27 +01:00
|
|
|
}
|