android Scroller的用法總結


 在android開發中有多種方式實現View的滑動,常見的有以下幾種:

  1.不斷的修改View的LayoutParams;

  2.通過動畫對View實現位移的效果;

  3.調用View的ScrollTo(),ScrllBy().

 

 這里要說的是第三種方式

  首先看看ScrollTo(),ScrllBy()的用法:

  scrollTo(int x, int y):

        scrollTo( )表示View相對於其初始位置滾動某段距離。

    x: 當x為正值時,表示View向左滾動了x距離

      當x為負值時,表示View向右移動了x距離

    y:當y為正值時,表示View向上滾動了y距離

      當y為負值時,表示View向下移動了y距離

  scrollBy(intx, int y):

     看過源碼就知道  

  public void scrollBy(int x, int y) {
   scrollTo(mScrollX + x, mScrollY + y);
  }
它是在原來mScrollX,mScrollY的基礎上接着滾動的
注意:這里並不是View自身在滾動,而是View里的內容在滾動。

通過上面兩個方法確實可以實現View的滾動,但我們要想讓View像我們常見的ViewPager那樣平滑的滾動,只用這兩方法是做不多的,因為它們產生的滾動是不連貫的,閃爍的,所以系統也為我們提供了

Scroller這個類來實現View的平滑滾動。

Scroller從源碼我們可以看到如何使用的:
  * <pre> private Scroller mScroller = new Scroller(context);
  * ...
  * public void zoomIn() {
  * // Revert any animation currently in progress
  * mScroller.forceFinished(true);
  * // Start scrolling by providing a starting point and
  * // the distance to travel
  * mScroller.startScroll(0, 0, 100, 0);
  * // Invalidate to request a redraw
  * invalidate();
  * }</pre>
  *
  * <p>To track the changing positions of the x/y coordinates, use
  * {@link #computeScrollOffset}. The method returns a boolean to indicate
  * whether the scroller is finished. If it isn't, it means that a fling or
  * programmatic pan operation is still in progress. You can use this method to
  * find the current offsets of the x and y coordinates, for example:</p>
  *
  * <pre>if (mScroller.computeScrollOffset()) {
  * // Get current x and y positions
  * int currX = mScroller.getCurrX();
  * int currY = mScroller.getCurrY();
  * ...
  * }</pre>

  根據上面文檔的描述可把Scroller的使用概括為以下五個主要步驟:

  1. 初始化Scroller
  2. 調用startScroll()開始滾動
  3. 執行invalidate()刷新界面
  4. 重寫View的computeScroll()並在其內部實現與滾動相關的業務邏輯
  5. 再次執行invalidate()刷新界面

  mScroller.startScroll(int startX,int startY,int dx,int dy)

  
startX:表示View X坐標滾動的開始位置;
  
   startY:表示View Y坐標滾動的開始位置 

   dx:表示View 從X坐標的開始位置需要滾動的距離(這里dx的正負是和ScrllTo() 的x一樣的)

  dy:表示View 從Y坐標開始位置需要滾動的距離(dy的正負和scrllTo的 y 一樣的)
  /**
   * Start scrolling by providing a starting point, the distance to travel,
   * and the duration of the scroll.
   *
   * @param startX Starting horizontal scroll offset in pixels. Positive
   * numbers will scroll the content to the left.
   * @param startY Starting vertical scroll offset in pixels. Positive numbers
   * will scroll the content up.
   * @param dx Horizontal distance to travel. Positive numbers will scroll the
   * content to the left.
   * @param dy Vertical distance to travel. Positive numbers will scroll the
   * content up.
   * @param duration Duration of the scroll in milliseconds.
   */
  public void startScroll(int startX, int startY, int dx, int dy, int duration) {
   mMode = SCROLL_MODE;
   mFinished = false;
   mDuration = duration;
   mStartTime = AnimationUtils.currentAnimationTimeMillis();
   mStartX = startX;
   mStartY = startY;
   mFinalX = startX + dx;
   mFinalY = startY + dy;
   mDeltaX = dx;
   mDeltaY = dy;
   mDurationReciprocal = 1.0f / (float) mDuration;
  }
  通過源碼可以看到,當調用startScroll()時,內部並沒有做什么滾動的操作,只是做了賦值的操作,所以當調用了這段代碼之后,
我們需要調用invalidate()方法來讓View重繪從而調用View里computeScroll()方法,在View源碼里computeScroll()方法是個
空實現,所以我們可以在這個方法里面做滾動的邏輯。

比如這樣:

·public void computeScroll(){
· if (mScroller.computeScrollOffset()) {
· scrollTo(0, mScroller.getCurrY());
· postInvalidate();
· }
· super.computeScroll();
·}
  另外在computeScroll()方法里,我們一般用mScroller.compuScrollOffset()來判斷View的滾動是否在繼續,返回true表示還在繼續,false
表示結束。
關於本人Scroller的理解差不多就這些了。
 
        




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM