Android 自定义控件之Scroller与computeScroll的调用关系

1.Scroller到底是什么?

答:Scroller只是个计算器,提供插值计算,让滚动过程具有动画属性,但它并不是UI,也不是辅助UI滑动,反而是单纯地为滑动提供计算。

无论从构造方法还是其他方法,以及Scroller的属性可知,其并不会持有View,辅助ViewGroup滑动

2.Scroller只是提供计算,那谁来调用computeScroll使得ViewGroup滑动

答:computeScroll也不是来让ViewGroup滑动的,真正让ViewGroup滑动的是scrollTo,scrollBy。computeScroll的作用是计算ViewGroup如何滑动。而computeScroll是通过draw来调用的。

3.computeScroll和Scroller都是计算,两者有啥关系?

答:文章开始已作答,没有直接的关系。computeScroll和Scroller要是飞得拉关系的话,那就是computeScroll可以参考Scroller计算结果来影响scrollTo,scrollBy,从而使得滑动发生改变。也就是Scroller不会调用computeScroll,反而是computeScroll调用Scroller。

4.滑动时连续的,如何让Scroller的计算也是连续的?

这个就问到了什么时候调用computeScroll了,如上所说computeScroll调用Scroller,只要computeScroll调用连续,Scroller也会连续,实质上computeScroll的连续性又invalidate方法控制,scrollTo,scrollBy都会调用invalidate,而invalidate回去触发draw,从而computeScroll被连续调用,综上,Scroller也会被连续调用,除非invalidate停止调用

5.computeScroll如何和Scroller的调用过程保持一致。

computeScroll参考Scroller影响scrollTo,scrollBy,实质上,为了不重复影响scrollTo,scrollBy,那么Scroller必须终止计算currX,currY。要知道计算有没有终止,需要通过mScroller.computeScrollOffset()

Android 自定义控件之Scroller与computeScroll的调用关系_第1张图片

6.如上问题应该说的很清楚了吧,如果不明白,请留言。

7.通过一个SlidePanel的例子,我们来深刻的了解一下

注意:在移动平台中,要明确知道“滑动”与“滚动”的不同,具体来说,滑动和滚动的方向总是相反的。

public class SlidingPanel extends RelativeLayout {
  private Context context;
  private FrameLayout leftMenu;
  private FrameLayout middleMenu;
  private FrameLayout rightMenu;
  private FrameLayout middleMask;
  private Scroller mScroller;
  public  final int LEFT_ID = 0xaabbcc;
  public  final int MIDEELE_ID = 0xaaccbb;
  public  final int RIGHT_ID = 0xccbbaa;
 
  private boolean isSlideCompete;
  private boolean isHorizontalScroll;
 
  private Point point = new Point();
  private static final int SLIDE_SLOP = 20;
 
  public SlidingPanel(Context context) {
      super(context);
      initView(context);
  }
 
  public SlidingPanel(Context context, AttributeSet attrs) {
      super(context, attrs);
      initView(context);
  }
 
  private void initView(Context context) {
 
      this.context = context;
      mScroller = new Scroller(context, new DecelerateInterpolator());
      leftMenu = new FrameLayout(context);
      middleMenu = new FrameLayout(context);
      rightMenu = new FrameLayout(context);
      middleMask = new FrameLayout(context);
      leftMenu.setBackgroundColor(Color.RED);
      middleMenu.setBackgroundColor(Color.GREEN);
      rightMenu.setBackgroundColor(Color.RED);
      middleMask.setBackgroundColor(0x88000000);
 
      addView(leftMenu);
      addView(middleMenu);
      addView(rightMenu);
      addView(middleMask);
 
      middleMask.setAlpha(0);
  }
  public float onMiddleMask(){
      return middleMask.getAlpha();
  }
    
  @Override
  public void scrollTo(int x, int y) {
      super.scrollTo(x, y);
      onMiddleMask();
  // Log.e("getScrollX","getScrollX="+getScrollX());//可以是负值
      int curX = Math.abs(getScrollX());
      float scale = curX/(float)leftMenu.getMeasuredWidth();
      middleMask.setAlpha(scale);
      
  }
 
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
      middleMenu.measure(widthMeasureSpec, heightMeasureSpec);
      middleMask.measure(widthMeasureSpec, heightMeasureSpec);
      int realWidth = MeasureSpec.getSize(widthMeasureSpec);
      int tempWidthMeasure = MeasureSpec.makeMeasureSpec(
            (int) (realWidth * 0.8f), MeasureSpec.EXACTLY);
      leftMenu.measure(tempWidthMeasure, heightMeasureSpec);
      rightMenu.measure(tempWidthMeasure, heightMeasureSpec);
  }
 
  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
      super.onLayout(changed, l, t, r, b);
      middleMenu.layout(l, t, r, b);
      middleMask.layout(l, t, r, b);
      leftMenu.layout(l - leftMenu.getMeasuredWidth(), t, r, b);
      rightMenu.layout(
            l + middleMenu.getMeasuredWidth(),
            t,
            l + middleMenu.getMeasuredWidth()
                  + rightMenu.getMeasuredWidth(), b);
  }
 
 
 
  @Override
  public boolean dispatchTouchEvent(MotionEvent ev) {
      if (!isSlideCompete) {
        handleSlideEvent(ev);
        return true;
      }
      if (isHorizontalScroll) {
        switch (ev.getActionMasked()) {
        case MotionEvent.ACTION_MOVE:
            int curScrollX = getScrollX();
            int dis_x = (int) (ev.getX() - point.x);
            //滑动方向和滚动滚动条方向相反,因此dis_x必须取负值
            int expectX = -dis_x + curScrollX;
 
            if(dis_x>0)
            {
              Log.d("I","Right-Slide,Left-Scroll");//向右滑动,向左滚动
            }else{
              Log.d("I","Left-Slide,Right-Scroll");
            }
 
            Log.e("I","ScrollX="+curScrollX+" , X="+ev.getX()+" , dis_x="+dis_x);
            //规定expectX的变化范围
            int    finalX = Math.max(-leftMenu.getMeasuredWidth(),Math.min(expectX, rightMenu.getMeasuredWidth()));
 
            scrollTo(finalX, 0);
            point.x = (int) ev.getX();//更新,保证滑动平滑
            break;
 
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            curScrollX = getScrollX();
            if (Math.abs(curScrollX) > leftMenu.getMeasuredWidth() >> 1) {
              if (curScrollX < 0) {
                  mScroller.startScroll(curScrollX, 0,
                        -leftMenu.getMeasuredWidth() - curScrollX, 0,
                        200);
              } else {
                  mScroller.startScroll(curScrollX, 0,
                        leftMenu.getMeasuredWidth() - curScrollX, 0,
                        200);
              }
 
            } else {
              mScroller.startScroll(curScrollX, 0, -curScrollX, 0, 200);
            }
            invalidate();
            isHorizontalScroll = false;
            isSlideCompete = false;
            break;
        }
      } else {
        switch (ev.getActionMasked()) {
        case MotionEvent.ACTION_UP:
            isHorizontalScroll = false;
            isSlideCompete = false;
            break;
 
        default:
            break;
        }
      }
 
      return super.dispatchTouchEvent(ev);
  }
 
  /**
    * 通过invalidate操纵,此方法通过draw方法调用
    */
  @Override
  public void computeScroll() {
      super.computeScroll();
      if (!mScroller.computeScrollOffset()) {
        //计算currX,currY,并检测是否已完成“滚动”
        return;
      }
      int tempX = mScroller.getCurrX();
      scrollTo(tempX, 0); //会重复调用invalidate
  }
 
 
  private void handleSlideEvent(MotionEvent ev) {
      switch (ev.getAction()&MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
        point.x = (int) ev.getX();
        point.y = (int) ev.getY();
        super.dispatchTouchEvent(ev);
        break;
 
      case MotionEvent.ACTION_MOVE:
        int dX = Math.abs((int) ev.getX() - point.x);
        int dY = Math.abs((int) ev.getY() - point.y);
        if (dX >= SLIDE_SLOP && dX > dY) { // 左右滑动
            isHorizontalScroll = true;
            isSlideCompete = true;
            point.x = (int) ev.getX();
            point.y = (int) ev.getY();
        } else if (dY >= SLIDE_SLOP && dY > dX) { // 上下滑动
            isHorizontalScroll = false;
            isSlideCompete = true;
            point.x = (int) ev.getX();
            point.y = (int) ev.getY();
        }
        break;
      case MotionEvent.ACTION_UP:
      case MotionEvent.ACTION_OUTSIDE:
      case MotionEvent.ACTION_CANCEL:
        super.dispatchTouchEvent(ev);
        isHorizontalScroll = false;
        isSlideCompete = false;
        break;
      }
  }
 
}
本文转载自:http://www.linuxidc.com/Linux/2016-01/127276.htm

你可能感兴趣的:(android应用,自定义控件,computeScroll,控件,ui,android)