浮云间
与已接受的答案类似,我在 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 ) } }}