コード例 #1
0
 @Override
 public boolean onTouchEvent(MotionEvent event) {
   boolean superOnTouchEvent = super.onTouchEvent(event);
   if (!isEnabled()) return superOnTouchEvent;
   boolean isEventInBounds = bounds.contains((int) event.getX(), (int) event.getY());
   if (isEventInBounds) {
     previousCoords.set(currentCoords.x, currentCoords.y);
     currentCoords.set((int) event.getX(), (int) event.getY());
   }
   int action = event.getActionMasked();
   switch (action) {
     case MotionEvent.ACTION_UP:
       if (pressed) {
         childView.setPressed(true);
         postDelayed(
             new Runnable() {
               @Override
               public void run() {
                 childView.setPressed(false);
               }
             },
             ViewConfiguration.getPressedStateDuration());
       }
       cancelPressedEvent();
       break;
     case MotionEvent.ACTION_CANCEL:
       //                currentCoords.set();
       childView.onTouchEvent(event);
       if (!pressed) startRipple();
       cancelPressedEvent();
       break;
     case MotionEvent.ACTION_DOWN:
       eventCancelled = false;
       pressEvent = new PressEvent(event);
       pressEvent.run();
       /** *这里注意当这个控件是在ScrollView内部的时候的问题 ** */
       break;
     case MotionEvent.ACTION_MOVE:
       if (isEventInBounds && !eventCancelled) invalidate();
       else if (!isEventInBounds) startRipple();
       if (!isEventInBounds) {
         cancelPressedEvent();
         if (hoverAnimator != null) hoverAnimator.cancel();
         childView.onTouchEvent(event);
         eventCancelled = true;
       }
       break;
   }
   return true;
 }
コード例 #2
0
 @Override
 public void onLongPress(MotionEvent e) {
   hasPerformedLongPress = childView.performLongClick();
   if (hasPerformedLongPress) {
     startRipple();
   }
   cancelPressedEvent();
 }