更改滚动条上的背景颜色

我有一个滚动视图,当用户滚动它时,我想慢慢地、逐渐地改变背景颜色。我怎样才能实现以下目标?



红颜莎娜
浏览 134回答 2
2回答

蝴蝶不菲

使用 aNestedScrollView并附加一个OnScrollChangeListener. 然后将其与ARGBEvaluator生成一种颜色,并将其设置为背景。我是凭记忆来的,但是类似:final ArgbEvaluator evaluator = new ArgbEvaluator();final colorStart = Color.GREEN;final colorEnd = Color.BLUE;nestedScrollView.setOnScrollChangedListener((view, scrollX, scrollY, oldX, oldY) -> {&nbsp; &nbsp; final float height = (float) v.getHeight();&nbsp; &nbsp; if(height <= 0) return;&nbsp; &nbsp; final float progress = (float)((float)scrollY/v.getHeight());&nbsp; &nbsp; background.setBackgroundColour((int)evaluator.evaluate(progress, startColor, endColor);});

浮云间

与已接受的答案类似,我在 Kotlin 中是这样做的:@RequiresApi(Build.VERSION_CODES.M)@SuppressLint("RestrictedApi")private fun setBackgroundAnimation(nestedScrollView: NestedScrollView) {&nbsp; &nbsp; val evaluator = ArgbEvaluator()&nbsp; &nbsp; val colorStart = context?.getColor(R.color.backgroundColor)&nbsp; &nbsp; val colorEnd = Color.BLACK&nbsp; &nbsp; var progress: Float&nbsp; &nbsp; val velocity = 3&nbsp; &nbsp; nestedScrollView.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->&nbsp; &nbsp; &nbsp; &nbsp; val scrollViewHeight = nestedScrollView.height&nbsp; &nbsp; &nbsp; &nbsp; if (scrollViewHeight > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; progress = (scrollY / scrollViewHeight.toFloat()) * velocity&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nestedScrollView.setBackgroundColor(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; evaluator.evaluate(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; progress,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; colorStart,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; colorEnd&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ) as Int&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java