注意
1.onDown 多指事件仍然响应成单指
2.onScroll滑动时触发,e1只能获得一个点 ,而e2却能获得多点。(搞不清楚为什么怎么设计)
3.想在view上加 GestureListener
可以使用下面的代码:
public class ViewerInnerTouchListener implements OnTouchListener {
private GestureDetector gestureDetector;
public ViewerInnerTouchListener(GAViewer viewer) {
gestureDetector = new GestureDetector(new GestureListener(viewer));
}
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true;
}
}
setOnTouchListener(new ViewerInnerTouchListener(this));
判断手势:
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.i("onFling", "onFling");
return true; // Right to left
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
return false; // Left to right
}
if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
return false; // Bottom to top
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
return false; // Top to bottom
}
return false;
}
GestureListener 的几个方法要理解
//Touch down时触发,不论是touch (包括long) ,scroll
public boolean onDown(MotionEvent e) {
return false;
}
//Touch了还没有滑动时触发
//(与onDown,onLongPress比较
//onDown只要Touch down一定立刻触发。
public void onShowPress(MotionEvent e) {
}
//Touch了不移动一直Touch down时触发
//Touchdown后过一会没有滑动先触发onShowPress再是onLongPress。
public void onLongPress(MotionEvent e) {
}
//上面这两个函数都是在touch down后又没有滑动(onScroll),又没有长按(onLongPress),然后Touchup时触发。
/**
* 点击一下非常快的(不滑动)Touchup: onDown->onSingleTapUp->onSingleTapConfirmed
*
点击一下稍微慢点的(不滑动)Touchup:onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed
* 点击longpress ,onScroll 时 不触发 onSingleTapUp
*/
public boolean onSingleTapUp(MotionEvent e) {
Log.i("onSingleTapUp", "onSingleTapUp");
return false;
}
//Touch了滑动时触发
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
return true;
}
//Touch了滑动一点距离后,up时触发
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return true;
}