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);
}
}