2 回答
TA贡献1810条经验 获得超4个赞
使用 aNestedScrollView并附加一个OnScrollChangeListener. 然后将其与ARGBEvaluator生成一种颜色,并将其设置为背景。我是凭记忆来的,但是类似:
final ArgbEvaluator evaluator = new ArgbEvaluator();
final colorStart = Color.GREEN;
final colorEnd = Color.BLUE;
nestedScrollView.setOnScrollChangedListener((view, scrollX, scrollY, oldX, oldY) -> {
final float height = (float) v.getHeight();
if(height <= 0) return;
final float progress = (float)((float)scrollY/v.getHeight());
background.setBackgroundColour((int)evaluator.evaluate(progress, startColor, endColor);
});
TA贡献1829条经验 获得超4个赞
与已接受的答案类似,我在 Kotlin 中是这样做的:
@RequiresApi(Build.VERSION_CODES.M)
@SuppressLint("RestrictedApi")
private fun setBackgroundAnimation(nestedScrollView: NestedScrollView) {
val evaluator = ArgbEvaluator()
val colorStart = context?.getColor(R.color.backgroundColor)
val colorEnd = Color.BLACK
var progress: Float
val velocity = 3
nestedScrollView.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
val scrollViewHeight = nestedScrollView.height
if (scrollViewHeight > 0) {
progress = (scrollY / scrollViewHeight.toFloat()) * velocity
nestedScrollView.setBackgroundColor(
evaluator.evaluate(
progress,
colorStart,
colorEnd
) as Int
)
}
}
}
添加回答
举报