2014-12-03 57 views
0

我需要訪問ACTION_MOVE事件之後發生的ACTION_UP,並且SimpleOnGestureListener不直接支持該操作,所以我試圖在活動的onTouchEvent方法中實現自定義觸摸處理,以獲得更多熟悉它。Android中的自定義觸摸操作

該活動是一個鬧鐘的警報屏幕(當警報消失時出現)。 我需要處理5種手勢。它們是:

Single tap - Visual feedback 
Double tap - Snooze the alarm 
Swipe along the Y access - visual feedback based on distance swiped. 
ACTION_UP after an ACTION_MOVE with a distance of less than 50% of the screen's Y axis - visual feedback. 
ACTION_UP after an ACTION_MOVE with a distance of greater than 50% of the screen's Y axis - dismiss the alarm. 

我寫的不正常工作的功能,我敢肯定,這是因爲該變量「startEvent」正在在每次調用的onTouchEvent設置,當它應該是唯一的集在滑動的ACTION_DOWN事件或雙擊的第一個水龍頭上。

這裏是我的代碼:

private final int DOUBLE_TAP_TIMEOUT = 500; // ms 
private boolean isScrolling = false; // flag used to detect the ACTION_UP after a scroll 
private boolean possibleDoubleTap = false; // flag used to detect a double tap 
private MotionEvent startEvent; // the event that starts the gesture we are trying to detect 
private float yPercentScrolled; // the percent the along the (Y axis/2) that has been scrolled. 
@Override 
public boolean onTouchEvent(MotionEvent event){ 

    switch (event.getActionMasked()){ 

     case(MotionEvent.ACTION_DOWN): 
      Log.d("MOTION", "DOWN"); 
      // reset the startEvent given the proper conditions 
      if(
        (!isScrolling && !possibleDoubleTap) 
        || (possibleDoubleTap && event.getEventTime() - startEvent.getEventTime() <= DOUBLE_TAP_TIMEOUT) 
      ){ 
       startEvent = event; 
      } 
      return true; 

     case(MotionEvent.ACTION_UP): 
      Log.d("MOTION", "UP"); 
      if(isScrolling){ 
       isScrolling = false; 

       if(yPercentScrolled >= 1f){ 
        dismissAlarm(); 
       } 
       else { 
        layout.setBackgroundColor(bgColor); 
        startEvent = null; 
        possibleDoubleTap = false; 
        isScrolling = false; 
       } 
      } 
      else if(possibleDoubleTap){ 
       // if we have a double tap 
       if(event.getEventTime() - startEvent.getEventTime() <= DOUBLE_TAP_TIMEOUT){ 
        snoozeAlarm(); 
        return true; 
       } 
       // if we don't have a double tap, do nothing 
      } 
      else if(!possibleDoubleTap){ 
       possibleDoubleTap = true; 
       layout.setBackgroundColor(Color.parseColor("#000000")); 
      } 
      return true; 

     case(MotionEvent.ACTION_MOVE): 
      Log.d("MOTION", "MOVE"); 
      if(!isScrolling){ 
       isScrolling = true; 
      } 
      else { 
       yPercentScrolled = handleScroll(event, startEvent); 
      } 
      return true; 

     default: 
      Log.d("MOTION", "other:" + String.valueOf(event.getActionMasked())); 
      return super.onTouchEvent(event); 
    } 
} 

爲什麼「startEvent」任何想法正在被重置時,它不應該? logcat在復位時沒有「MOTION:DOWN」,所以這一切都在困擾着我。這也是將startEvent分配給非空值的唯一位置。

謝謝。

回答

0

看起來startEvent = event正在分配對象event的引用而不是對象本身。所以當對象event的值改變時。 startEvent的值也會更改。

嘗試取和只存儲您需要的值(在您的情況下是X, Y, and EventTime)而不是整個event對象。

+1

我實際上只是通過調試器,並意識到是這樣的。我使用了代碼startEvent = MotionEvent.obtain(event);克隆該對象,以便我可以傳遞MotionEvent,而不是一些變量。我認爲我的onTouchEvent現在工作正常,但我沒有測試每個輸入組合。謝謝。 – kemanuel 2014-12-04 04:22:59