我想根据当前偏移量(从折叠到展开)为CollapsingToolbarLayout
中的后退按钮颜色(箭头)设置动画:
abstract class CollapsingToolbarListener : AppBarLayout.OnOffsetChangedListener {
/**
* @param appBarLayout the AppBarLayout which offset has changed
* @param verticalOffset the vertical offset for the parent AppBarLayout, in px
*/
override fun onOffsetChanged(appBarLayout: AppBarLayout, verticalOffset: Int) {
val offset = Math.abs(verticalOffset).toFloat()
val minOffset = 0.toFloat()
val maxOffset = appBarLayout.totalScrollRange.toFloat()
return when {
offset < minOffset -> onStateChanged(appBarLayout, 0f)
offset in minOffset..maxOffset -> onStateChanged(appBarLayout, offset / maxOffset)
offset > maxOffset -> onStateChanged(appBarLayout, 100f)
else -> throw IllegalStateException("bad offset: $offset")
}
}
/**
* @param appBarLayout the AppBarLayout which offset has changed
* @param offset the vertical offset for the parent AppBarLayout from 0 to 1
*/
abstract fun onStateChanged(appBarLayout: AppBarLayout, offset: Float)
}
在我的活动中:
appbar.addOnOffsetChangedListener(object : CollapsingToolbarListener() {
override fun onStateChanged(appBarLayout: AppBarLayout, offset: Float) {
val color = ArgbEvaluator().evaluate(offset, 0x00ff00, 0xff0000) as Int
val upArrow = resources.getDrawable(R.drawable.abc_ic_ab_back_material)
upArrow.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
supportActionBar?.setHomeAsUpIndicator(upArrow)
}
})
但是我的工具栏颜色完全没有改变。我想念什么?