zoukankan      html  css  js  c++  java
  • 随着ScrollView的滑动,渐渐的运行动画View

    今天是实现了一个小功能的东西。看看效果图:
    123.gif

    实现方式:
    1.自己定义ScrollView   复写onScrollChange方法,来计算滑动的位置。
    2.自己定义接口,通过接口来在ScrollView中控制,滑动的高度的进度。
    3.在自己定义View中去运行动画。



    代码实现:
    1.ScrollView   最基本的代码仅仅有计算滑动位置的代码了,事实上也是非常easy的。获取子View的个数。每次都去for循环,去计算字View的位置,以及当前ScrollView的top bottom
    代码:

    @Override
        protected void onScrollChanged(int l, int t, int oldl, int oldt) {
            super.onScrollChanged(l, t, oldl, oldt);
            int currentBottom = t + height ;
            int currentTop = t ; 
            Log.e("Slide""onScrollChange") ;
            
            for (int i = 0; i < childCount; i++) {
                View childView = contentLayout.getChildAt(i )  ;
                if (!(childView  instanceof EasySlideInter)) {
                    continue ; 
                }
                int childTop = childView.getTop() ; 
                int childBottom = childView.getBottom() ;
                int childHeight = childView.getHeight() ; 
                EasySlideInter inter = (EasySlideInter) childView ; 
                if ( currentTop > childTop && currentTop < childBottom ) {
                    inter.contentSlide(countProgress(currentTop, childBottom, childHeight)); 
                }else if (currentBottom > childTop && currentBottom < childBottom ) {
                    inter.contentSlide(100 - countProgress(currentBottom, childBottom, childHeight)); 
                }else if(childTop >= currentTop && childBottom <= currentBottom){
                    inter.resetContent();
                }
            }
    }  


    通过childView的top位置与ScrollView的当前的top位置来推断是哪个子View正在慢慢的出现,计算出progress 传递给子View中去。

    事实上终于要的代码就是这么一段,动画的运行都在子View的接口方法中去做的。
    我贴上一个子View的实现:
        
        @Override
        public void contentSlide(int progress) {
            textAnimator.setCurrentPlayTime(progress);
            backAnimator.setCurrentPlayTime(progress);
        }
        @Override
        public void resetContent() {
            textAnimator.setCurrentPlayTime(100);
            backAnimator.setCurrentPlayTime(100);
        }
        
        
        private void initAnimation(){
            textAnimator = ObjectAnimator.ofInt(text"textColor", Color.BLUE , Color.RED); 
            textAnimator.setEvaluator(new ArgbEvaluator());
            textAnimator.setDuration(100)  ;
            textAnimator.setInterpolator(new LinearInterpolator()) ;
            
            backAnimator = ObjectAnimator.ofInt(this"backgroundColor", Color.BLACK , Color.BLUE , Color.BLACK); 
            backAnimator.setEvaluator(new ArgbEvaluator());
            backAnimator.setDuration(100)  ;
            backAnimator.setInterpolator(new LinearInterpolator()) ;
            
        }  
    实现两个接口的方法,在这两个方法中,去控制动画的进度。
    非常easy的,不再累赘叙述了。
    源代码下载:
    百度网盘:  http://pan.baidu.com/s/1dDtVzSt 


  • 相关阅读:
    I40E网卡BUG引起内核异常重启问题分析
    Linux rp_filter配置引起的组播断流问题
    【原创】Intel XL710网卡异常Reset问题复现
    【原创】qlogic网卡软中断不均衡问题分析
    【原创】控制perl和python脚本执行过程中脚本文件是否关闭的方法
    Linux内核d_path函数应用的经验总结
    Intel 82599网卡异常挂死原因
    用slub_track调试use after free问题
    如何获取内核指定线程的调用栈
    使用data breakpoint 追踪地址寄存器被修改的问题
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/6795638.html
Copyright © 2011-2022 走看看