android gesture检测

1、关于on<TouchEvent>的返回值

 a return value of true from the individual on<TouchEvent> methods indicates that you have handled the touch event. A return value of false passes events down through the view stack until the touch has been successfully handled.

true表示你已经处理了此touch事件,false则会将事件一直传到view栈,直到touch事件被处理。

2、关于onDown()事件

Whether or not you use GestureDetector.OnGestureListener, it's best practice to implement an onDown() method that returns true. This is because all gestures begin with an onDown() message. If you return false fromonDown(), as GestureDetector.SimpleOnGestureListener does by default, the system assumes that you want to ignore the rest of the gesture, and the other methods of GestureDetector.OnGestureListener never get called. 

 

 

 

关于多点触摸:

system generates the following touch events:

  • ACTION_DOWN—For the first pointer that touches the screen. This starts the gesture. The pointer data for this pointer is always at index 0 in the MotionEvent.
  • ACTION_POINTER_DOWN—For extra pointers that enter the screen beyond the first. The pointer data for this pointer is at the index returned by getActionIndex().
  • ACTION_MOVE—A change has happened during a press gesture.
  • ACTION_POINTER_UP—Sent when a non-primary pointer goes up.
  • ACTION_UP—Sent when the last pointer leaves the screen.

访问多点的touch数据:

You keep track of individual pointers within a MotionEvent via each pointer's index and ID:

  • Index: A MotionEvent effectively stores information about each pointer in an array. The index of a pointer is its position within this array. Most of the MotionEvent methods you use to interact with pointers take the pointer index as a parameter, not the pointer ID.
  • ID: Each pointer also has an ID mapping that stays persistent across touch events to allow tracking an individual pointer across the entire gesture.
 1 private int mActivePointerId;

 2  

 3 public boolean onTouchEvent(MotionEvent event) {

 4     ....

 5     // Get the pointer ID

 6     mActivePointerId = event.getPointerId(0);

 7 

 8     // ... Many touch events later...

 9 

10     // Use the pointer ID to find the index of the active pointer 

11     // and fetch its position

12     int pointerIndex = event.findPointerIndex(mActivePointerId);

13     // Get the pointer's current position

14     float x = event.getX(pointerIndex);

15     float y = event.getY(pointerIndex);

16 }
View Code

 

你可能感兴趣的:(android)