我想在两个手指上实现画布的拖动和缩放。 我试过以下方法。
第1步: onTouchEvent我已经检查过getPointerCount == 2然后拖动画布类似于单指拖动。 代码:
if(ev.getPointerCount() == 2) {
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN: {
final float x = ev.getX(0);
final float y = ev.getY(0);
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_MOVE: {
final float x = ev.getX(0);
final float y = ev.getY(0);
// Only move if the ScaleGestureDetector isn't processing a gesture.
if (!mScaleDetector.isInProgress()) {
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
mPosX += dx;
mPosY += dy;
invalidate();
}
mLastTouchX = x;
mLastTouchY = y;
//invalidate();
break;
}
case MotionEvent.ACTION_UP: {
Log.i(TAG,"Double Touch : ACTION_UP SingleTouch:"+isSingleTouch );
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_CANCEL: {
Log.i(TAG,"Double Touch : ACTION_CANCEL SingleTouch:"+isSingleTouch );
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_POINTER_UP: {
Log.i(TAG,"Double Touch : ACTION_POINTER_UP SingleTouch:"+isSingleTouch );
// Extract the index of the pointer that left the touch sensor
final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK)
>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = ev.getPointerId(pointerIndex);
if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = ev.getX(newPointerIndex);
mLastTouchY = ev.getY(newPointerIndex);
mActivePointerId = ev.getPointerId(newPointerIndex);
}
break;
}
}
第2步:添加Scale Gesture Detector
代码:
public boolean onTouchEvent(MotionEvent ev) {
mScaleDetector.onTouchEvent(ev);
/*Some code */
}