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;}