这是我的代码:
public boolean onTouch(View v, MotionEvent event) {
switch(event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
Log.d("getButtonCode", "catch ACTION_DOWN");
break;
case MotionEvent.ACTION_POINTER_DOWN:
Log.d("getButtonCode", "catch ACTION_POINTER_DOWN");
break;
case MotionEvent.ACTION_POINTER_UP:
Log.d("getButtonCode", "catch ACTION_POINTER_UP");
break;
case MotionEvent.ACTION_UP:
Log.d("getButtonCode", "catch ACTION_UP");
case MotionEvent.ACTION_CANCEL:
Log.d("getButtonCode", "catch ACTION_CANCEL");
case MotionEvent.ACTION_MOVE:
break;
default:
break;
}
return true;
}
我从未捕获过ACTION_POINTER_DOWN和ACTION_POINTER_UP。如果我从ACTION_DOWN中删除break
,我总是在ACTION_DOWN之后捕获ACTION_POINTER_DOWN,即使它是第一个指针。我认为应该如此:第一个指针只捕获ACTION_DOWN,每个下一个指针只有ACTION_POINTER_DOWN,如果非主指针已经上升,我应该捕获ACTION_POINTER_UP。
但它不起作用。我的代码出了什么问题?
PS:我看到了其他类似的问题,但没有回答帮助我。答案 0 :(得分:0)
我没有使用switch语句,而是使用了一系列if语句。这让我可以对event.getAction()或event.getActionMasked()进行测试,它允许同时执行多个可能的操作。
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
startingX = (int) event.getX();
startingY = (int) event.getY();
}
if(event.getAction() == MotionEvent.ACTION_MOVE) {
int scaleX = 1;
int scaleY = 1;
if(touchStateScale == true){
scaleX = (int) (event.getX(0) - event.getX(1));
scaleY = (int) (event.getY(0) - event.getY(1));
}
int endingX = (int) event.getX();
int endingY = (int) event.getY();
int newX = endingX-startingX;
int newY = endingY-startingY;
((ImageView) v).setImageBitmap(drawNewImageScaled(baseImage, newImageResourceId, newX, newY, scaleX, scaleY));
}
if(event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN){
Log.i(TAG, "second pointer detected");
touchStateScale = true;
}
if (event.getAction() == MotionEvent.ACTION_UP) {
touchStateScale = false;
return false;
}
return true;
}
};