滑动title变色加背景高斯模糊效果实现

95 篇文章 0 订阅
//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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值