单手指和双手指(手势探测器)
单手指:
- 如果想要监听单手指操作的全部操作监听(有两个接口,仅需要挑选自己需要的来实现):
public class SingleGesture implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener { @Override public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public void onShowPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onSingleTapUp(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub return false; } @Override public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // TODO Auto-generated method stub return false; } @Override public boolean onSingleTapConfirmed(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public boolean onDoubleTap(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public boolean onDoubleTapEvent(MotionEvent e) { // TODO Auto-generated method stub return false; } }
- 如果仅仅想要监听单手指操作的一部分简单操作的监听:
public class SingleGesture extends GestureDetector.SimpleOnGestureListener { }
因为SimpleOnGestureListener实现了onGestureListener接口和onDoubleTapListener接口,所以仅仅需要重写两个接口上的所需要的方法即可
/** * A convenience class to extend when you only want to listen for a subset * of all the gestures. This implements all methods in the * {@link OnGestureListener} and {@link OnDoubleTapListener} but does * nothing and return {@code false} for all applicable methods. */ public static class SimpleOnGestureListener implements OnGestureListener, OnDoubleTapListener { public boolean onSingleTapUp(MotionEvent e) { return false; } public void onLongPress(MotionEvent e) { } public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { return false; } public void onShowPress(MotionEvent e) { } public boolean onDown(MotionEvent e) { return false; } public boolean onDoubleTap(MotionEvent e) { return false; } public boolean onDoubleTapEvent(MotionEvent e) { return false; } public boolean onSingleTapConfirmed(MotionEvent e) { return false; } }
双手指:
- 如果想要监听单手指操作的全部操作监听
public class TwoFingerGesture implements ScaleGestureDetector.OnScaleGestureListener { @Override public boolean onScale(ScaleGestureDetector detector) { // TODO Auto-generated method stub return false; } @Override public boolean onScaleBegin(ScaleGestureDetector detector) { // TODO Auto-generated method stub return false; } @Override public void onScaleEnd(ScaleGestureDetector detector) { // TODO Auto-generated method stub } }
- 如果仅仅想要监听双手指操作的一部分简单操作的监听:
public class TwoFingerGesture extends ScaleGestureDetector.SimpleOnScaleGestureListener { }