java.lang.Object | |
↳ | android.view.GestureDetector |
Class Overview
Detects various gestures and events using the supplied MotionEvent
s. The GestureDetector.OnGestureListener
callback will notify users when a particular motion event has occurred. This class should only be used with MotionEvent
s reported via touch (don't use for trackball events). To use this class:
- Create an instance of the
GestureDetector
for yourView
- In the
onTouchEvent(MotionEvent)
method ensure you callonTouchEvent(MotionEvent)
. The methods defined in your callback will be executed when the events occur.
利用提供的MotionEvent来检测多种手势和事件。当一个特殊的动作事件发生,GestureDetector.OnGestureListener
这个回调接口就会通知用户。这个类只能用于由触摸所引起的事件(不用于由轨迹球所引起的事件)。
为你的view创建GestureDetector
实例
确保你调用了onTouchEvent(MotionEvent)。这个方法定义了当某一事件发生时回调方法会被执行。
public GestureDetector (Context context, GestureDetector.OnGestureListener listener)
Creates a GestureDetector with the supplied listener. You may only use this constructor from a UI thread (this is the usual situation).
Parameters
context | the application's context |
---|---|
listener | the listener invoked for all the callbacks, this must not be null. |
Throws
NullPointerException | if listener is null. |
---|
See Also
用存在的listener来创建GestureDetector。按照惯例来说,你只能在UI线程中调用这个构造方法。
listener不能为空。
public GestureDetector (Context context, GestureDetector.OnGestureListener listener, Handler handler, boolean ignoreMultitouch)
ignoreMultitouch whether events involving more than one pointer should be ignored.是否忽视多点触摸
public boolean isLongpressEnabled ()
Returns
- true if longpress is enabled, else false
- 当长按有效的时候会返回true,否则返回false
-
public boolean onTouchEvent (MotionEvent ev)
Since: API Level 1Analyzes the given motion event and if applicable triggers the appropriate callbacks on the
GestureDetector.OnGestureListener
supplied.Parameters
ev The current motion event. Returns
- true if the
GestureDetector.OnGestureListener
consumed the event, else false.
GestureDetector.OnGestureListener
中合适的回调方法 - true if the
- 如果
GestureDetector.OnGestureListener
处理了事件,就返回true,否则返回false
public void setIsLongpressEnabled (boolean isLongpressEnabled)
Since: API Level 1Set whether longpress is enabled, if this is enabled when a user presses and holds down you get a longpress event and nothing further. If it's disabled the user can press and hold down and then later moved their finger and you will get scroll events. By default longpress is enabled.
Parameters
isLongpressEnabled whether longpress should be enabled. -
public void setOnDoubleTapListener (GestureDetector.OnDoubleTapListener onDoubleTapListener)
Since: API Level 3Sets the listener which will be called for double-tap and related gestures.
Parameters
onDoubleTapListener the listener invoked for all the callbacks, or null to stop listening for double-tap gestures.