实现ScrollView头标题滑动渐变

ScrollView滑动监听:

Google并没有给我们提供ScrollView的滑动距离、是否滑动到布局底部、顶部的方法,但是提供了一个onScrollChanged方法:

@Override
  protected void onScrollChanged(int x, int y, int oldx, int oldy) {
    super.onScrollChanged(x, y, oldx, oldy);
    //todo:
    }
  }

我们可以知道这个方法的参数分别为: 
l:当前横向滑动距离 
t:当前纵向滑动距离 
oldl:之前横向滑动距离 
oldt:之前纵向滑动距离

但是这个方法我们不可以调用,我们可以重写接口或者重写ScrollView暴露该方法:

/**
 * Created by 张乔君 on 2017/10/12.
 */
public class GradaScrollView extends ScrollView {

 private ScrollViewListener scrollViewListener = null;

  public void setScrollViewListener(ScrollViewListener scrollViewListener) { this. scrollViewListener = scrollViewListener; } public interface ScrollViewListener { void onScrollChanged(GradaScrollView scrollView, int x, int y, int oldx, int oldy); } public GradaScrollView(Context context) { super(context); } public GradaScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public GradaScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onScrollChanged( int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); if( scrollViewListener!= null){ scrollViewListener.onScrollChanged( this, l, t, oldl, oldt); } }}

设置标题渐变

滚动监听暴露出来我们就该去设置标题栏随着ScrollView的滑动来改变标题栏的透明度实现渐变:

<com.zqj.jingdongdemo.view.GradaScrollView
    android:id="@+id/slv"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <include layout="@layout/f1_head" android:id="@+id/f1_head"></include>          
    </RelativeLayout>
</com.zqj.jingdongdemo.view.GradaScrollView>



主activity里面的方法

private void setListener() {
 //获取顶部图片高度后,设置滚动监听
    ViewTreeObserver vto = banner.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){

        @Override
        public void onGlobalLayout() {

         changehead.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            height=banner.getHeight();//获取xbanner的高度
            scrollview.setScrollViewListener(new GradaScrollView.ScrollViewListener() {
                @Override
                public void onScrollChanged(GradaScrollView scrollView, int x, int y, int oldx, int oldy) {
                    if(y<=0){//初始值
                        changehead.setBackgroundColor(Color.argb((int) 0, 144,151,166));
                    }else if(y>0 && y<=height){//开始渐变
                        float scale = (float) y / height;
                        float alpha = (255 * scale);
                       changehead.setBackgroundColor(Color.argb((int) alpha, 144,151,166));
                    }else{//超过banner的高度后,转换颜色
                        changehead.setBackgroundColor(Color.argb((int) 255, 144,151,166));
                    }
                }
            });

        }
    });

}


猜你喜欢

转载自blog.csdn.net/zqj861791241/article/details/78213082