常見值有:java
case MotionEvent.ACTION_DOWN:
//拿到第0跟手指
trackID = event.getPointerId(0);
downX = event.getX();
downY = event.getY();
originalOffsetX = offsetX;
originalOffsetY = offsetY;
break;
case MotionEvent.ACTION_MOVE:
int index = event.findPointerIndex(trackID);
offsetX = originalOffsetX + event.getX(index) - downX;
offsetY = originalOffsetY + event.getY(index) - downY;
invalidate();
break;
case MotionEvent.ACTION_POINTER_DOWN:
int actionIndex = event.getActionIndex();
trackID = event.getPointerId(actionIndex);
downX = event.getX(actionIndex);
downY = event.getY(actionIndex);
originalOffsetX = offsetX;
originalOffsetY = offsetY;
break;
case MotionEvent.ACTION_POINTER_UP:
//擡起來的這根手指的index
actionIndex = event.getActionIndex();
int pointerId = event.getPointerId(actionIndex);
if (pointerId == trackID) {
//將當前的點分配到這根手指
int newIndex;
//若是擡起來的這根手指剛好是最後一根則
if (actionIndex == event.getPointerCount() - 1) {
newIndex = event.getPointerCount() - 2;
} else {
//若是是其餘手指則
newIndex = event.getPointerCount() - 1;
}
trackID = event.getPointerId(newIndex);
downX = event.getX(newIndex);
downY = event.getY(newIndex);
originalOffsetX = offsetX;
originalOffsetY = offsetY;
}
break;
複製代碼
float sumX = 0;
float sumY = 0;
int pointerCount = event.getPointerCount();
boolean isPointerUp = event.getActionMasked() == MotionEvent.ACTION_POINTER_UP;
for (int i = 0; i < pointerCount; i++) {
if (!(isPointerUp && i == event.getActionIndex())) {
sumX += event.getX(i);
sumY += event.getY(i);
}
}
if (isPointerUp) {
pointerCount -= 1;
}
float focusX = sumX / pointerCount;
float focusY = sumY / pointerCount;
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_POINTER_UP:
downX = focusX;
downY = focusY;
originalOffsetX = offsetX;
originalOffsetY = offsetY;
break;
case MotionEvent.ACTION_MOVE:
offsetX = originalOffsetX + focusX - downX;
offsetY = originalOffsetY + focusY - downY;
invalidate();
break;
}
複製代碼
case ACTION_DOWN:
case ACTION_POINTER_DOWN:
int actionIndex = event.getActionIndex();
int pointerId = event.getPointerId(actionIndex);
Path path = new Path();
path.moveTo(event.getX(actionIndex), event.getY(actionIndex));
paths.append(pointerId, path);
invalidate();
break;
case ACTION_MOVE:
for (int i = 0; i < event.getPointerCount(); i++) {
pointerId = event.getPointerId(i);
path = paths.get(pointerId);
path.lineTo(event.getX(i), event.getY(i));
}
invalidate();
break;
case ACTION_UP:
case ACTION_POINTER_UP:
pointerId = event.getPointerId(event.getActionIndex());
paths.remove(pointerId);
invalidate();
break;
複製代碼