ScrollView下滑背景渐变

来源:互联网 发布:人工智能会议 编辑:程序博客网 时间:2024/04/30 06:04

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







0 0