我正在尝试使用以下代码进行捏合旋转图像:
public boolean onTouchEvent(MotionEvent e) {
if(e.getPointerCount() > 1) {
if (e.getActionMasked()== MotionEvent.ACTION_MOVE) {
previousX1 = curX1;
previousY1 = curY1;
currentX1 = e.getX(1);
currentY1 = e.getY(1);
previousX0 = curX0;
previousY0 = curY0;
currentX0 = e.getX(0);
currentY0 = e.getY(0);
double prevDist = Math.hypot(prevX1 - prevX2, prevY1 - prevY2);
double dist = Math.hypot(curX1 - curX2, curY1 - curY2);
angle += ((dist - prevDist) * 0.01);
}
}
return true;
}
现在,这在大多数情况下都可以正常工作,除了我想在捏开始时立即更新currentX1,currentX2等值,即第二个手指接触(或从中提起)的MotionEvent )第一根手指已被按下时的屏幕。但是,Android API相当混乱,我无法弄清楚如何检测这种情况。使用(e.getActionMasked() == MotionEvent.ACTION_DOWN)
或(e.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN)
作为条件似乎无效。
非常感谢帮助。谢谢!
答案 0 :(得分:0)
如果您只想在屏幕上使用 TWO 手指进行测试,可以使用此功能:
@Override
public boolean onTouch(final View view, MotionEvent event) {
final int action = event.getAction();
float xPosition = 0;
float yPosition = 0;
switch (action){
case MotionEvent.ACTION_DOWN :
xPosition = event.getX();
yPosition = event.getY();
break;
case MotionEvent.ACTION_POINTER_2_DOWN :
xPosition = event.getX(1);
yPosition = event.getY(1);
break;
default:
break;
}
}