//ScrollView 滑动变色
scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
int height = rlTitle.getMeasuredHeight(); //title 高
Log.e(TAG,"----------mHeight " + height);
if (scrollY <= 0) {
rlTitle.setBackgroundColor(Color.argb((int) 0, 0, 0, 0));
tvTitle.setTextColor(Color.argb((int) 0, 0, 0, 0));
} else if (scrollY <= height) {
//获取渐变率
float scale = (float) scrollY / height;
float alpha = (255 * scale);
rlTitle.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));
tvTitle.setTextColor(Color.argb((int) alpha, 0, 0, 0));
} else {
rlTitle.setBackgroundColor(Color.argb((int) 255, 255, 255, 255));
tvTitle.setTextColor(Color.argb((int) 255, 0, 0, 0));
}
}
});
//recyclerView的滑动,,判断向上滑动的距离是否大于y
recyclerView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View view, int i, int i1, int i2, int i3) {
//获取滑动距离,,通过布局管理器
//1.获得视图的第一条木的下标
//2.根据下标获得view条目,,,在获得条目的高度
//3.下标*条目高度-可见视图距离顶部的高度
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
int position = layoutManager.findFirstVisibleItemPosition();
View firstVisiableChildView = layoutManager.findViewByPosition(position);
int itemHeight = firstVisiableChildView.getHeight();
int y = (position) * itemHeight - firstVisiableChildView.getTop();
if (y <= 0) {
//设置标题的背景颜色
rlTitle.setBackgroundColor(Color.argb((int) 0, 0, 0, 0));
tvTitle.setTextColor(Color.argb((int) 0, 0, 0, 0));
Log.e(TAG, "----------------y <= 0----" + y);
} else if (y > 0 && y <= 150) {
//滑动距离小于banner图的高度时,设置背景和字体颜色颜色透明度渐变
float scale = (float) y / 150;
float alpha = (255 * scale);
rlTitle.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));
tvTitle.setTextColor(Color.argb((int) alpha, 0, 0, 0));
Log.e(TAG, "---------------y > 0 && y <= 150---" + y);
} else {
//滑动到banner下面设置普通颜色
rlTitle.setBackgroundColor(Color.argb((int) 255, 255, 255, 255));
tvTitle.setTextColor(Color.argb((int) 255, 0, 0, 0));
Log.e(TAG, "------------------" + y);
}
}
});
https://www.sioe.cn/yingyong/yanse-rgb-16/ (设置颜色转换网址)
源码:https://github.com/zhudaihao/TitleBackGround