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 theMotionEvent
.ACTION_POINTER_DOWN
—For extra pointers that enter the screen beyond the first. The pointer data for this pointer is at the index returned bygetActionIndex()
.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 theMotionEvent
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.
private int mActivePointerId; public boolean onTouchEvent(MotionEvent event) {
....
// Get the pointer ID
mActivePointerId = event.getPointerId(0); // ... Many touch events later... // Use the pointer ID to find the index of the active pointer
// and fetch its position
int pointerIndex = event.findPointerIndex(mActivePointerId);
// Get the pointer's current position
float x = event.getX(pointerIndex);
float y = event.getY(pointerIndex);
}