实现目标效果
分析
1、默认从上到下,是由黑到透明效果
2、滚动的时候,只是将透明部分变成黑色
xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="@color/black_000000_alpha0_change"
android:startColor="@color/black_000000" />
</shape>
动态代码
int[] colors = new int[2];
colors[0] = ResourcesUtils.getResourceColor(this, R.color.black_000000);
colors[1] = ResourcesUtils.getResourceColor(this, R.color.black_000000_alpha0_change);
@Override
public void onNestedScrollListener(int scrollY) {
if (scrollY == 0) {
colors[1] = ResourcesUtils.getResourceColor(this, R.color.black_000000_alpha0_change);
} else {
float alpha = scrollY / 220f;
if (alpha >= 1) alpha = 1;
int bgAlpha = (int) (alpha * 255);
colors[1] = Color.argb(bgAlpha, 0, 0, 0);
}
GradientDrawable gradientDrawable = (GradientDrawable) flTitleLayout.getBackground();
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setColors(colors); //添加颜色组
gradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);//设置线性渐变
flTitleLayout.setBackground(gradientDrawable);
}
关于onNestedScrollListener,请自行百度,NestedScrollView 设置滚动监听