Android 滑動手勢簡單使用方法

來源:互聯網
上載者:User

Android 滑動手勢簡單使用方法

用法:首先讓指定的Activity實現Android.view.OnGestureListener,然後在你需要調用GestureDetector的View上,添加onTouchListener,之後setLongClickable(true)即可.

如:

customView.setOnTouchListner(xxxListener);

customView.setLongClickable(true);

 

之後就可以在onFling方法中設定你想要的手勢.

 

onFling方法參數解釋:

onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY)

官方解釋:

 

Notified of a fling event when it occurs with the initial on down MotionEvent and the matching up MotionEvent. The calculated velocity is supplied along the x and y axis in pixels per second.

Specified by: onFling(...) in OnGestureListener Parameters: 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. velocityY The velocity of this fling measured in pixels per second along the y axis.Returns:true if the event is consumed, else false 大致意思:

e1代表手指按下的瞬間觸發的事件,

e2代表手指未彈起,移動時觸發的事件

剩下兩個參數大致意思是每秒鐘在x,y方向上移動的像素點數.

所以,通過e1和e2即可創造出手勢的判斷條件.

簡單左右滑動手勢代碼:

@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {// TODO Auto-generated method stubtoif (e1.getX() > e2.getX()) {Log.i(msg, <<<<<<<<<);} else {Log.i(msg, >>>>>>>>>);}Toast.makeText(this, Fling, 0).show();return false;}

 

 

 

聯繫我們

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