1、对ScrollView设置滑动监听,重写onScrollChange()方法
2、获取当前透明度的数值,(注意:需要将255转换成double类型,不然结果一直会是0)
3、获取颜色的色值,调用Color.argb()方法。(代码中mycolor为自定义的颜色色值,int color = ContextCompat.getColor(this, R.color.red_trans);)
4、设置需要渐变的背景颜色
核心代码如下:
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
int boottom = layout_info.getBottom();
refreshLayout.setEnabled(scrollY<=1);
if(boottom>=scrollY) {
int alpha = (int) (255.0/boottom * scrollY);
int argb = Color.argb(alpha, Color.red(mycolor), Color.green(mycolor), Color.blue(mycolor));
layout.setBackgroundColor(argb);
}
}