android學習之VelocityTracker

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   color   ar   os   使用   

今天大概的學習了一下VelocityTracker ,用來監控手勢速度的,在View移動過程中,可以有個緩衝的移動。 我覺得初步的使用起來是很簡單的。
首先移動依靠的是Scroller類,Scroller中有個fling的方法,將MotionEvent的Up之後檢測的手勢速度提供給scroller之後,就可以自己完成滾動。1.初始化有些變數:  private void init(Context context){        mScroller = new Scroller(getContext());        final ViewConfiguration configuration = ViewConfiguration.get(context);          mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();          mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();     } configuration.getScaledTouchSlop()//獲得能夠進行手勢滑動的距離
configuration.getScaledMinimumFlingVelocity()//獲得允許執行一個fling手勢動作的最小速度值
configuration.getScaledMaximumFlingVelocity()//獲得允許執行一個fling手勢動作的最大速度值
2.在觸摸事件中處理: @Override      public boolean onTouchEvent(MotionEvent event) {                  obtainVelocityTracker(event);   //初始化mVelocityTracker                int action = event.getAction();          switch (action) {              case MotionEvent.ACTION_DOWN:                  startY = event.getY();                if (mScroller.isFinished()) {                    mScroller.abortAnimation();                }                break;              case MotionEvent.ACTION_MOVE:                  //求偽瞬時速度                  float moveY = event.getY();                scrollTo(0, (int) (getScrollY() + (startY - moveY)));                startY = event.getY();                break;              case MotionEvent.ACTION_UP:                  final VelocityTracker velocityTracker = mVelocityTracker;                  velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);                  int initialVelocity = (int) velocityTracker.getYVelocity();  //擷取Y軸的速度                if ((Math.abs(initialVelocity) > mMinimumVelocity) && getChildCount() > 0) {                          fling(-initialVelocity);  //開始滾動                }              case MotionEvent.ACTION_CANCEL:                  releaseVelocityTracker();  //要釋放掉                break;          }          return true;      }  
/** *釋放mVelocityTracker */ private void releaseVelocityTracker() {              if(null != mVelocityTracker) {                  mVelocityTracker.clear();                  mVelocityTracker.recycle();                  mVelocityTracker = null;              }        }  
/** *釋放初始化 */private void obtainVelocityTracker( MotionEvent event) {          if(null == mVelocityTracker) {              mVelocityTracker = VelocityTracker.obtain();          }          mVelocityTracker.addMovement(event);      }    
down和move事件中就是簡單的跟隨手勢移動了,主要的事件處理是在up中。滾動的事件處理在fling方法中:public void fling(int velocityY) {                 mScroller.fling(getScrollX(), getScrollY(), 0, velocityY, 0, 0, -1080, 0);  //就是scroller的一個方法而已,很簡單。                awakenScrollBars(mScroller.getDuration());                  invalidate();    }  
fling(getScrollX(), getScrollY(), 0, velocityY, 0, 0, -1080, 0);
                                                                                                                          最小Y      最大Y
我的Demo中只是簡單的Y軸上的滾動,所以設定了最小最大Y軸上的移動距離。最小Y  最大Y  都是說的ScrollY這個屬性。有Scroller這個類。就少不了computScroll方法。public void computeScroll() {          if (mScroller.computeScrollOffset()) {                  int x = mScroller.getCurrX();                  int y = mScroller.getCurrY();                  scrollTo(x, y);                  postInvalidate();          }  }  

我的github地址:https://github.com/flyme2012我的部落格地址:http://www.cnblogs.com/flyme2012/



來自為知筆記(Wiz)

android學習之VelocityTracker

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.