2014-10-08 72 views
0

我创建了一个实现GestureDetector.OnGestureListener的类。但是,我不知道如何调用这些方法。是否有类似view.setOnTouchListener()的GestureListener允许您接收事件?先进的谢谢你。这里是我的类如何接收来自OnGestureListener的事件

公共类GestureHandler实现GestureDetector.OnGestureListener {

private static final int SWIPE_MIN_DISTANCE = 120; 
private static final int SWIPE_THRESHOLD_VELOCITY = 200; 
private int direction = -1; 

@Override 
public boolean onDown(MotionEvent e) { 
    return true; 
} 

@Override 
public void onShowPress(MotionEvent e) { 

} 

@Override 
public boolean onSingleTapUp(MotionEvent e) { 
    return false; 
} 

@Override 
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
    return false; 
} 

@Override 
public void onLongPress(MotionEvent e) { 

} 

@Override 
public boolean onFling(MotionEvent event1, MotionEvent event2, 
         float velocityX, float velocityY) { 
    //Left Swipe 
    if(event1.getX() - event2.getX() > SWIPE_MIN_DISTANCE && 
      Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
     direction = 270; 
     Log.d("Swiped: ", direction + ""); 
     return true; 
     //Right Swipe 
    } else if(event2.getX() - event1.getX() > SWIPE_MIN_DISTANCE && 
      Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
     direction = 90; 
     Log.d("Swiped: ", direction + ""); 
     return true; 
    } 
    //Up Swipe 
    if(event1.getY() - event2.getY() > SWIPE_MIN_DISTANCE && 
      Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) { 
     direction = 0; 
     Log.d("Swiped: ", direction + ""); 
     return true; 
     //Down Swipe 
    } else if(event2.getY() - event1.getY() > SWIPE_MIN_DISTANCE && 
      Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) { 
     direction = 180; 
     Log.d("Swiped: ", direction + ""); 
     return true; 
    } 
    direction = -1; 
    Log.d("Swiped: ", direction + ""); 
    return false; 
} 

}这里

回答

0

很简单的解决方案。 :)

下面是我这究竟是怎么在我的项目,与谷歌玻璃加工 -

我实际上并没有延伸GestureDetector。我有这样的事情:

public class EXAMPLE { 
    private GestureDetector gestureDetector; 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     gestureDetector = createGestureDetector(this); 

    } 
    private GestureDetector createGestureDetector(Context context) { 
     GestureDetector gestureDetectorTemp = new GestureDetector(context, new GestureDetector.OnGestureListener() { 
      @Override 
      public boolean onDown(MotionEvent motionEvent) { 
       return false; 
      } 

      @Override 
      public void onShowPress(MotionEvent motionEvent) { 
       return false; 
      } 

      @Override 
      public boolean onSingleTapUp(MotionEvent motionEvent) { 
       return false; 
      } 
      @Override 
      public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float distanceX, float distanceY) { 
       return false; 
      } 
      @Override 
      public void onLongPress(MotionEvent motionEvent) { 
      } 
      @Override 
      public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) { 
      return false; 
      } 
     }); 
     return gestureDetectorTemp; 
    } 

    @Override 
    public boolean onGenericMotionEvent(MotionEvent event) { 
     if (gestureDetector != null) { 
      return gestureDetector.onTouchEvent(event); 
     } 
     return false; 
    } 
} 

最后一部分是非常重要的。在任何通用运动事件中,如果gestureDetector不为null,则将通过gestureDetector发送事件进行处理。

还想你还需要了解什么是返回false;并返回true;事情的意思。如果你返回false,那就意味着这个事件没有被消耗掉。如果您返回true,则该事件将被消耗。换句话说,如果你返回true,那么没有别的东西会被激活,因为事件会被'吃掉',但是如果你返回false,这会把事件发送到其他功能上,这些功能可能会在采取行动时做些事情。

我希望这会有所帮助。祝你好运:)