我有一個按鈕,我在按鈕按動畫。當我拖出特定的閾值之後,我希望它恢復到「正常」狀態。如何讓我的視圖在ACTION_MOVE後停止監聽觸摸事件?
我在ACTION_DOWN
上創建了一個矩形的視圖邊界,並檢查它是否在ACTION_MOVE
的觸摸區域之外。我成功地發現了「越界」觸摸,但我無法停止觀看觸摸。這就像它忽略了我的animateToNormal()方法。
我試着改變布爾返回值爲true而不是false,這沒有幫助。我也嘗試在ACTION_MOVE
的情況下刪除觸摸偵聽器(設置爲空),但我需要重新連接以繼續偵聽觸摸。我想我可以在添加它之前添加一個任意延遲,但這似乎是一個可怕的黑客。
我在4.2設備(LG G2)上測試這個。
private static class AnimationOnTouchListener implements View.OnTouchListener {
private Rect rect;
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch(motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
rect = new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
animatePressed();
return false;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
// back to normal state
animateBackToNormal();
return false;
case MotionEvent.ACTION_MOVE:
if(!rect.contains(view.getLeft() + (int) motionEvent.getX(), view.getTop() + (int) motionEvent.getY())){
d(TAG, "out of bounds");
animateBackToNormal();
// STOP LISTENING TO MY TOUCH EVENTS!
} else {
d(TAG, "in bounds");
}
return false;
default:
return true;
}
}
好笑的是,我有相反的問題。我想繼續收到活動,但是我沒有。 http://stackoverflow.com/questions/34908569/android-continue-receiving-touch-events-in-a-view-after-the-touch-is-dragged –