Android之scrollview滑动使导航栏渐变背景色

一:效果图

第一二张效果图是用红线标注,第三张为黑线,三张为不同滑动距离的效果图,不想弄GIF的,凑合看吧、

 

二:实现步骤

1.xml实现,在你的布局加上scrollview,也可以自定义scrollview,都有注释,就不用解释太多了

<com.zjtd.bzcommunity.lib.myScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="none">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        </LinearLayout>
    </com.zjtd.bzcommunity.lib.myScrollView>

2.代码实现

private myScrollView mScrollView; // 整体ScrollView
private int fadingHeight = 600; // 当ScrollView滑动到什么位置时渐变消失(根据需要进行调整)
private Drawable drawable; // 顶部渐变布局需设置的Drawable
private RelativeLayout layout_top_search;//导航栏
private static final int START_ALPHA = 0;//scrollview滑动开始位置
private static final int END_ALPHA = 255;//scrollview滑动结束位置

mScrollView = (myScrollView) view.findViewById(R.id.scrollView);
drawable = getResources().getDrawable(R.color.dhlbg);
drawable.setAlpha(START_ALPHA);
layout_top_search.setBackgroundDrawable(drawable);
//调用方法
mScrollView.setOnScrollChangedListener(scrollChangedListener);


 /**
     * ScrollView的滚动监听
     */
    private myScrollView.OnScrollChangedListener scrollChangedListener = new myScrollView.OnScrollChangedListener() {
        @Override
        public void onScrollChanged(ScrollView who, int x, int y, int oldx,
                                    int oldy) {
            if (y > fadingHeight) {
                y = fadingHeight; // 当滑动到指定位置之后设置颜色为纯色,之前的话要渐变---实现下面的公式即可

//                relativela_id.setBackgroundColor(Color.WHITE);
            } else if (y < 0) {
                y = 0;
            } else {
//                relativela_id.setBackgroundColor(0x99FFFFFF);
            }
            drawable.setAlpha(y * (END_ALPHA - START_ALPHA) / fadingHeight
                    + START_ALPHA);

        }
    };


 

 

 

 

---------------------------差不多就是这样子了,不喜勿喷、

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶已初秋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值