我写了这个代码用于放大/缩小。它工作,但即使用一根手指它放大/缩小,而它应该只用两个手指(多点触控)。谁知道为什么?
boolean surfaceTouchEvent(MotionEvent event) {
pointNum=event.getPointerCount();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
//User is pressing on finger
x0=event.getX(0);
y0=event.getY(0);
mode = false; //DRAG
break;
case MotionEvent.ACTION_POINTER_DOWN:
x1=event.getX(0);
x2=event.getX(1);
y1=event.getY(0);
y2=event.getY(1);
z4 = dist(x1, y1, x2, y2);
mode = true; // pinch
break;
case MotionEvent.ACTION_UP:
mode = false;
case MotionEvent.ACTION_POINTER_UP:
// User is released one of the fingers.
mode = false;
break;
case MotionEvent.ACTION_MOVE:
if (mode = false) {
// x1=event.getX(0)-x0;
//y1=event.getY(0)-y0;
}
if (mode = true) {
x1=event.getX(0);
x2=event.getX(1);
y1=event.getY(0);
y2=event.getY(1);
float z3 = dist(x1, y1, x2, y2);
if ( z3 > z4 ) {
zoom += 0.1;
}
else if ( z3 < z4) {
zoom -= 0.1;
}
}
break;
}
return super.surfaceTouchEvent(event);
}
我编辑代码如下,但这次它根本不起作用,至少第一个代码有效,但缩放反应很奇怪,请帮助
float x1,x2,y1,y2 = -1;
boolean surfaceTouchEvent(MotionEvent event) {
pointNum=event.getPointerCount();
if (pointNum >=2) {
x1=event.getX(0);
x2=event.getX(1);
y1=event.getY(0);
y2=event.getY(1);
z4 = dist(x1, y1, x2, y2);
if ( z3 > z4 ) {
zoom += z3/z4;
}
else if ( z3 < z4) {
zoom -= z3/z4;
}
}
else {
return false;
}
z3 = z4;
return super.surfaceTouchEvent(event);
}
答案 0 :(得分:0)
只有在有多个手指时才应执行此操作。 只需在代码之前添加:
boolean surfaceTouchEvent(MotionEvent event) {
pointNum=event.getPointerCount();
if(pointNum >= 2) {
// your code here
} else {
return false;
}
}