仿qq渐变式变化

  1.  添加一个类(重写)

    
    /**
     * 带滚动监听的scrollview
     *
     */
    public class GradationScrollView extends ScrollView {
    
    	public interface ScrollViewListener {
    
    		void onScrollChanged(GradationScrollView scrollView, int x, int y,
                                 int oldx, int oldy);
    
    	}
    
    	private ScrollViewListener scrollViewListener = null;
    
    	public GradationScrollView(Context context) {
    		super(context);
    	}
    
    	public GradationScrollView(Context context, AttributeSet attrs,
                                   int defStyle) {
    		super(context, attrs, defStyle);
    	}
    
    	public GradationScrollView(Context context, AttributeSet attrs) {
    		super(context, attrs);
    	}
    
    
    	public void setScrollViewListener(ScrollViewListener scrollViewListener) {
    		this.scrollViewListener = scrollViewListener;
    	}
    
    	@Override
    	protected void onScrollChanged(int x, int y, int oldx, int oldy) {
    		super.onScrollChanged(x, y, oldx, oldy);
    		if (scrollViewListener != null) {
    			scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
    		}
    	}
    
    }

    在需要的布局Xml里添加引用

        <com.juyou.calendar.manage.GradationScrollView
            android:id="@+id/gs_scrollview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="none">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
    
               
    
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
    
                    <Button
                        android:id="@+id/button"
                        android:layout_width="173dp"
                        android:layout_height="wrap_content"
                        android:text="展示数据"
                        android:visibility="gone" />
    
                    <RelativeLayout
                        android:id="@+id/parent_block"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
                </LinearLayout>
    
    
            </LinearLayout>
        </com.juyou.calendar.manage.GradationScrollView>
  2. Linelayout中就是要展示的数据
  3. 在需要的数据展示的类中 implements GradationScrollView.ScrollViewListener
    
    
    public class  A extends B implements GradationScrollView.ScrollViewListener {
    
    
      
    
        /**
         * 滑动最大区域-相对布局的高
         */
        private int height;
    
    
        @Override
        public void onScrollChanged(GradationScrollView scrollView, int x, int y, int oldx, int oldy) {
            if (y <= 0) {  
     //设置标题的背景颜色
                【初始化的控件】viewActionBarTitle.setBackgroundColor(Color.argb((int) 0, 255, 0, 0));
               【初始化的控件】 calendar.setVisibility(View.VISIBLE);
               【初始化的控件】 wkOut.setVisibility(View.VISIBLE);
            } else if (y > 0 && y <= height) { 
    //滑动距离小于banner图的高度时,设置背景和字体颜色颜色透明度渐变
                //滑动的距离:最大距离(相对布局高度) = 透明的改变 : 最大透明度
                //透明的改变 =  (滑动的距离/最大距离)*255
                //(滑动的距离/最大距离)
                float scale = (float) y / height;
                //透明度
                float alpha = (scale * 255);
               【初始化的控件】 calendar.setVisibility(View.VISIBLE);
               【初始化的控件】 wkOut.setVisibility(View.VISIBLE);
               【初始化的控件】 calendar.setBackgroundColor(Color.argb((int) alpha, 255,0,0));
            } else {   
     //滑动到banner下面设置普通颜色
                //y>height
                //透明度:0~255
               【初始化的控件】 calendar.setVisibility(View.GONE);
               【初始化的控件】 wkOut.setVisibility(View.GONE);
                //y>height
                //透明度:0~255
               【初始化的控件】 calendar.setBackgroundColor(Color.argb((int) 255, 255,0,0));
            }
    
        }
    
      
    
        @Override
        protected void initView(View view, Bundle savedInstanceState) {
            ButterKnife.bind(this, view);
            【布局展示】
            //视图初始化
            initListeners();
        }
    
        private void initListeners() {
            //视图树观察者
            ViewTreeObserver vto = llCalendOut.getViewTreeObserver();
    
            vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    //移除监听
                    llCalendOut.getViewTreeObserver().removeGlobalOnLayoutListener(
                            this);
    
                    //得到布局的高,(就是你要让其滑动的距离的之后要操作的)
                    height = llCalendOut.getHeight();
                    //设置ScrollView的滑动监听
                    gsScrollview.setScrollViewListener(CalendarFragment.this);
                }
            });
        }
    
     
    
    }
    

    完成

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值