我正在扩展View并覆盖onTouchEvent 我想在第二根手指触摸屏幕时触发/发送MotionEvent.ACTION_CANCEL。有没有办法做到这一点?
答案 0 :(得分:0)
您可以调用或覆盖dispatchTouchEvent(MotionEvent事件)。
您想将ACTION_CANCEL发送给谁?例如,如果您的View是ViewGroup,则会在第二次按下时将其发送给所有子项:
class MyGroup extends ViewGroup {
...
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
// Is this the second finger down?
if (event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN) {
// Cancel all children
MotionEvent cancelEvent = MotionEvent.obtain(event);
cancelEvent.setAction(MotionEvent.ACTION_CANCEL);
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.dispatchTouchEvent(cancelEvent);
}
cancelEvent.recycle();
return false;
}
// Otherwise just do the normal behavior
return super.dispatchTouchEvent(event);
}
...
}