美文网首页简化开发
GradientDrawable 滚动修改渐变

GradientDrawable 滚动修改渐变

作者: 一个冬季 | 来源:发表于2021-04-25 11:54 被阅读0次
实现目标效果
效果图.jpg
分析

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 设置滚动监听

相关文章

网友评论

    本文标题:GradientDrawable 滚动修改渐变

    本文链接:https://www.haomeiwen.com/subject/ldrdrltx.html