SimpleOnGestureListener//简单手势监听
方法:
Notified of a fling event when it occurs with the initial on down
MotionEvent
and the matching up MotionEvent
. The calculated velocity is supplied along the x and y axis in pixels per second.
//通知一个抛(扔,掷)事件,当它随着一个最初的向下的运动事件和匹配的运动事件发生时。计算的速率是通过x,y轴上每秒的像素值确定的。
Parameters//参数
e1:The first down motion event that started the fling.//开始这个"抛"的第一次向下的运动事件.
e2:The move motion event that triggered the current onFling.//引起当前的"抛"的运动事件。
velocityX:The velocity of this fling measured in pixels per second along the x axis.//以沿着x轴某秒多少像素数测量的"抛"的速率.
velocityY:The velocity of this fling measured in pixels per second along the y axis.//
以沿着y轴某秒多少像素数测量的"抛"的速率.
Returns
- true if the event is consumed, else false//如果事件被消耗,返回true,否则返回false.
(1)写一个类继承SimpleOngestureListener
import android.view.MotionEvent;
import android.view.GestureDetector.SimpleOnGestureListener;
public class DefaultListener extends SimpleOnGestureListener {
/**
* 两次轻敲时调用
*/
@Override
public boolean onDoubleTap(MotionEvent e) {
// TODO Auto-generated method stub
int action = e.getAction();
System.out.println("两次轻敲,onDoubleTap," + action);
return super.onDoubleTap(e);
}
@Override
// 按下时调用
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
System.out.println("按下,onDown==" + e.getAction());// onDown=0
return super.onDown(e);
}
// 滑动一段距离,up时触发
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
System.out.println("抛," + "e1===" + e1.getAction() + ",e2==="
+ e2.getAction());// 0
System.out.println("velocityX===" + velocityX + ",velocityY==="
+ velocityY);// 1
return super.onFling(e1, e2, velocityX, velocityY);
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
// TODO Auto-generated method stub
System.out.println("两次轻敲事件,onDoubleTapEven==" + e.getAction());
return super.onDoubleTapEvent(e);
}
// 长按后触发(Touch down之后一定时间(500ms))
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
System.out.println("长按,onLongPress==" + e.getAction());
super.onLongPress(e);
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
System.out.println("滑动时调用," + "e1===" + e1.getAction() + ",e2==="
+ e2.getAction());
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
System.out.println("短按,onShowPress==" + e.getAction());
super.onShowPress(e);
}
@Override
// 按下时调用
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
System.out.println("onSingleTapConfirmed,单击确认==" + e.getAction());// 0
return super.onSingleTapConfirmed(e);
}
@Override
// 按下时调用
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
System.out.println("onSingleTapUp==" + e.getAction());// 1
return super.onSingleTapUp(e);
}
}
(2)在一个Activity里面重写onTouchEvent(),并且将上面的那个类的对象作为参数传递给GestureDetector。如下所示:
import android.os.Bundle;
import android.app.Activity;
import android.view.GestureDetector;
import android.view.MotionEvent;
public class MainActivity extends Activity {
private GestureDetector gestureDetector;
private DefaultListener defaultListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
defaultListener = new DefaultListener();
gestureDetector = new GestureDetector(getApplicationContext(),
defaultListener);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
}