Android -- Scroller

來源:互聯網
上載者:User

標籤:

Android裡Scroller類是為了實現View平滑滾動的一個Helper類。通常在自訂的View時使用,在View中定義一個私人成員mScroller = new Scroller(context)。設定mScroller滾動的位置時,並不會導致View的滾動,通常是用mScroller記錄/計算View滾動的位置,再重寫View的computeScroll(),完成實際的滾動。

API
mScroller.getCurrX() //擷取mScroller當前水平滾動的位置  mScroller.getCurrY() //擷取mScroller當前豎直滾動的位置  mScroller.getFinalX() //擷取mScroller最終停止的水平位置  mScroller.getFinalY() //擷取mScroller最終停止的豎直位置  mScroller.setFinalX(int newX) //設定mScroller最終停留的水平位置,沒有動畫效果,直接跳到目標位置  mScroller.setFinalY(int newY) //設定mScroller最終停留的豎直位置,沒有動畫效果,直接跳到目標位置    //滾動,startX, startY為開始滾動的位置,dx,dy為滾動的位移量, duration為完成滾動的時間  mScroller.startScroll(int startX, int startY, int dx, int dy) //使用預設完成時間250ms  mScroller.startScroll(int startX, int startY, int dx, int dy, int duration)    mScroller.computeScrollOffset() //傳回值為boolean,true說明滾動尚未完成,false說明滾動已經完成。這是一個很重要的方法,通常放在View.computeScroll()中,用來判斷是否滾動是否結束。
Code
import android.content.Context;  import android.util.AttributeSet;  import android.util.Log;  import android.view.View;  import android.widget.LinearLayout;  import android.widget.Scroller;    public class CustomView extends LinearLayout {        private static final String TAG = "Scroller";        private Scroller mScroller;        public CustomView(Context context, AttributeSet attrs) {          super(context, attrs);          mScroller = new Scroller(context);      }        //調用此方法滾動到目標位置      public void smoothScrollTo(int fx, int fy) {          int dx = fx - mScroller.getFinalX();          int dy = fy - mScroller.getFinalY();          smoothScrollBy(dx, dy);      }        //調用此方法設定滾動的相對位移      public void smoothScrollBy(int dx, int dy) {            //設定mScroller的滾動位移量          mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy);          invalidate();//這裡必須調用invalidate()才能保證computeScroll()會被調用,否則不一定會重新整理介面,看不到滾動效果      }            @Override      public void computeScroll() {                //先判斷mScroller滾動是否完成          if (mScroller.computeScrollOffset()) {                        //這裡調用View的scrollTo()完成實際的滾動              scrollTo(mScroller.getCurrX(), mScroller.getCurrY());                            //必須調用該方法,否則不一定能看到滾動效果              postInvalidate();          }          super.computeScroll();      }  }

當startScroll執行過程中即在duration時間內,computeScrollOffset  方法會一直返回false,但當動畫執行完成後會返回返加true.

當我們執行ontouch或invalidate()或postInvalidate()都會導致computeScroll()這個方法的執行。所以postInvalidate執行後,會去調computeScroll 方法,而這個方法裡再去調postInvalidate,這樣就可以不斷地去調用scrollTo方法了,直到mScroller動畫結束,當然第一次時,我們需要手動去調用一次postInvalidate才會去調用。

方向

我是天王蓋地虎的分割線

Android -- Scroller

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.