Android 螢幕手勢滑動中onFling()函數的技巧分析

來源:互聯網
上載者:User

標籤:

關於如何處理手勢操作以及那四個基本固定的順序我就不講解了,這裡直接跳到我們獲得瞬間滑動後回調onFling()這個抽象函數時,應該如何根據參數比較準確的判斷滑動方向。如果你沒有前面的基礎知識,你可以去看看這篇文章:http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1020/448.html

我看到網上大部分資料,對這個抽象函數的實現都是相當簡單的:

123456 @Override  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  float velocityY) {     //dosomething returnfalse;  }

 

這些文章其實能解決的問題只有一個,那就是教你如何能在有手勢操作的時候,捕獲到這個動作,卻沒有去分析這個動作。

 

其實要真正能分析手勢,需要處理好這四個參數MotionEvent e1, MotionEvent e2, float velocityX, float velocityY

 

 先來看一個例子:
123456789101112131415161718192021 private int verticalMinDistance = 20;  private int minVelocity         = 0;      public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {         if(e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {             // 切換Activity          // Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);          // startActivity(intent);          Toast.makeText(this,"向左手勢", Toast.LENGTH_SHORT).show();      }elseif (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {             // 切換Activity          // Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);          // startActivity(intent);          Toast.makeText(this,"向右手勢", Toast.LENGTH_SHORT).show();              returnfalse}

 OnFling的四個參數意思分別為

e1: The first down motion event that started the fling.手勢起點的移動事件
e2: The move motion event that triggered the current onFling.當前手勢點的移動事件
velocityX: The velocity of this fling measured in pixels per second along the x axis.每秒x軸方向移動的像素
velocityY: The velocity of this fling measured in pixels per second along the y axis.每秒y軸方向移動的像素

說的更簡單點就是,滑鼠手勢相當於一個向量(當然有可能手勢是曲線),e1為向量的起點,e2為向量的終點,velocityX為向量水平方向的速度,velocityY為向量垂直方向的速度

 

1 if(e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity)

 

 則上面的語句能知道啥意思了吧,就是說向量的水平長度(滑了有多長)必須大於verticalMinDistance,並且水平方向速度大於minVelocity。

從而我們可以如此判斷手勢是否滿足一定的條件從而進行相應響應,也可以根據這個寫出更複雜的手勢判斷。

 

雖然我這篇文章不去探究手勢操作的基本步湊,但還是有必要談談我們的listenner在重載onTouch()這個函數的時候應該思考的問題:

 

123 public boolean onTouch(View v, MotionEvent event) {        returnmGestureDetector.onTouchEvent(event);   }

 

查看GestureDetector類的onTouchEvent的源碼就能知道,進入該函數後會進入case MotionEvent.ACTION_UP這個路徑,從而調用onFling函數。

我要說的就是這句話,因為在我看來GestureDetector未必能滿足處理所有的手勢需求,肯能有那麼一天,需要我們拋開GestureDetector   直接在onTouch()裡面完成任務。

Android 螢幕手勢滑動中onFling()函數的技巧分析

聯繫我們

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