View绘制总结--View结合Scroller实现平滑移动(computeScroll())

适用范围:

       当View或者ViewGroup中的内容超过一个屏幕时,我们必须要通过滑动的方式使得用户可以查看那些超过屏幕的内容,如果

直接调用ScrollTo()或者ScrollBy()的方式来移动的话会让用户觉得太突然而且效果不好看,这时候就可以使用Scroller来实现平滑移动。

Scroller的本质及作用:

       Scroller本质就是一个Helper类,里面保存了目标对象要移动的距离,时间等属性!     

Scroller的具体API分析:

       实例化:

            Scroller mScroller = new Scroller(Context mContext){}; :采用默认插值器

            Scroller mScroller = new Scroller(Context mContext,Interpolator interpolator){};采用指定的插值器

       调用过程:

       public void startScroll( int startX, int startY, int dx,int dy){};

                这方法并不是真正意义上的开始Scroll,它的作用是为Scroller做一些准备工作

                比如设置移动的初始位置,滑动的位移以及持续时间等。

       public boolean computeScrollOffset(){}

                这方法用于判断移动过程是否完成

       getCurrX、getCurrY、getFinalX、getFinalY、这些方法用于获取scroll的一些位置信息

Scroller与View结合使用:

       首先需要在自定义的View中定义一个私有成员 mScroller,用于记录view滚动的位置,然后再重写View的computeScroll()方法来实现具体移动

注意:Scroller的作用只是保存一些信息,以及判断是否移动完成,所以我们得知道computeScroll()这个方法的调用流程,在查看Android源码时发现

View.java中的computeScroll()方法是一个空函数,所以我们需要在自定义的View中重写这个方法来实现我们想要的功能,那么computeScroll()是怎么样被

调用的呢?

调用invalidate()(UI线程)或者postInvalidate()使View(Viewgroup)树重绘

重绘分成两种情况:

1、Viewgroup的重绘

ViewGroup的绘制流程:onDraw()-->dispatchDraw()-->drawChild()-->child.computeScroll()

2、View的重绘:

View没有子view所以在View的源码中看到dispatchDraw()方法是一个空方法,那么其调用流程就和ViewGroup是不一样的,那么View是怎样调用computeScroll()的呢?

我们注意到invalidate是重绘整个View树或者ViewGroup树,所以当View重绘时其所在父容器也会重绘,so,父容器就会经历onDraw()-->dispatchDraw()-->drawChild()

-->child.computeScroll()流程,这时候自定义View中重写的computeScroll()方法就会被调用。

 ViewGroup.java:

  1. @Override  
  2. protected void dispatchDraw(Canvas canvas){ 


  1.   
  2.             for (int i = 0; i < count; i++) {  
  3.             final View child = children[i];  
  4.             if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null)  
  5.   
  6.             {  
  7.                 more |= drawChild(canvas, child, drawingTime);  
  8.             }  

drawChild函数:

protected boolean drawChild(Canvas canvas, View child, long drawingTime) {

                  ----------

child.computeScroll();

              -----------------

 if ((child.mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
        if (ViewDebug.TRACE_HIERARCHY) {
            ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
        }
        child.dispatchDraw(canvas);
    } else {
        child.draw(canvas);
    }




         自定义View:

  1. public class CustomView extends View {  
  2.   
  3.     private Scroller mScroller;  
  4.   
  5.     public CustomView(Context context, AttributeSet attrs) {  
  6.         super(context, attrs);  
  7.         mScroller = new Scroller(context);  
  8.     }  
  9.   
  10.     //调用此方法滚动到目标位置  
  11.     public void smoothScrollTo(int fx, int fy) {  
  12.         int dx = fx - mScroller.getFinalX();  
  13.         int dy = fy - mScroller.getFinalY();  
  14.         smoothScrollBy(dx, dy);  
  15.     }  
  16.   
  17.     //调用此方法设置滚动的相对偏移  
  18.     public void smoothScrollBy(int dx, int dy) {  
  19.         //设置mScroller的滚动偏移量  
  20.         mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy);  
  21.         invalidate();//这里必须调用invalidate()才能保证computeScroll()会被调用,否则不一定会刷新界面,看不到滚动效果  
  22.     }  
  23.       
  24.     @Override  
  25.     public void computeScroll() {  
  26.         //先判断mScroller滚动是否完成  
  27.         if (mScroller.computeScrollOffset()) {  
  28.             //这里调用View的scrollTo()完成实际的滚动  
  29.             scrollTo(mScroller.getCurrX(), mScroller.getCurrY());  
  30.               
  31.             //必须调用该方法,否则不一定能看到滚动效果  
  32.             postInvalidate();  
  33.         }  
  34.         super.computeScroll();  
  35.     }  
  36. }  

你可能感兴趣的:(View绘制总结--View结合Scroller实现平滑移动(computeScroll()))