栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

仿qq渐变式变化

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

仿qq渐变式变化

  1.  添加一个类(重写)

    
    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里添加引用

        
    
            
    
               
    
    
                
    
                    
  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);
                }
            });
        }
    
     
    
    }
    

    完成

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/343327.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号