安卓自定义导航栏滚动渐变

1.自定义scrollview

public class ObservableScrollView :ScrollView
    {
        private OnScrollChangeListener scrollViewListener = null;  
        public ObservableScrollView(Context context):base(context)
        {

        }

        public ObservableScrollView(Context context,IAttributeSet attrs):base(context,attrs)
        {

        }
        public ObservableScrollView(Context context,IAttributeSet attrs,int defStyle):base(context,attrs,defStyle)
        {

        }
        public void SetScrollViewListener(OnScrollChangeListener onScrollChangeListener)
        {
            this.scrollViewListener = onScrollChangeListener;  
        }
        protected override void OnScrollChanged (int l, int t, int oldl, int oldt)
        {
            base.OnScrollChanged (l, t, oldl, oldt);
            if (scrollViewListener != null) {  
                scrollViewListener.onScrollChanged(this,l,t,oldl,oldt);  
            }  
        }
    }

2.滚动监听接口

public class MyScrollListener:OnScrollChangeListener
    {
        LinearLayout llNavBar;
        ImageView ivBg;
        public MyScrollListener(LinearLayout llNavBar,ImageView ivBg)
        {
            this.llNavBar = llNavBar;
            this.ivBg = ivBg;
        }

        public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy)
        {
            if(ivBg != null && ivBg.Height > 0 ){
                int lHeight = ivBg.Height;
                if(y < lHeight){
                    int progress =Math.Abs((int)((float)y/(float)lHeight* 255));
                    llNavBar.SetBackgroundColor(new Color(32, 36, 39, progress));
                }else{
                    llNavBar.SetBackgroundColor(new Color(32, 36, 39, 255));
                }   
            }
        }
    }

3.主界面关键代码

llNavBar.SetBackgroundColor(new Color(32, 36, 39, 0));
scrollView.SetScrollViewListener (new MyScrollListener (llNavBar, ivBg));

你可能感兴趣的:(Mono,For,android,渐变导航栏,安卓,scrollview)