SimpleOnGestureListener//简单手势监听

来源:互联网 发布:sql 查看表约束 编辑:程序博客网 时间:2024/05/17 08:36

SimpleOnGestureListener//简单手势监听

方法:
(1)public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
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 {/** * 两次轻敲时调用 */@Overridepublic boolean onDoubleTap(MotionEvent e) {// TODO Auto-generated method stubint action = e.getAction();System.out.println("两次轻敲,onDoubleTap," + action);return super.onDoubleTap(e);}@Override// 按下时调用public boolean onDown(MotionEvent e) {// TODO Auto-generated method stubSystem.out.println("按下,onDown==" + e.getAction());// onDown=0return super.onDown(e);}// 滑动一段距离,up时触发@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {// TODO Auto-generated method stubSystem.out.println("抛," + "e1===" + e1.getAction() + ",e2==="+ e2.getAction());// 0System.out.println("velocityX===" + velocityX + ",velocityY==="+ velocityY);// 1return super.onFling(e1, e2, velocityX, velocityY);}@Overridepublic boolean onDoubleTapEvent(MotionEvent e) {// TODO Auto-generated method stubSystem.out.println("两次轻敲事件,onDoubleTapEven==" + e.getAction());return super.onDoubleTapEvent(e);}// 长按后触发(Touch down之后一定时间(500ms))@Overridepublic void onLongPress(MotionEvent e) {// TODO Auto-generated method stubSystem.out.println("长按,onLongPress==" + e.getAction());super.onLongPress(e);}@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {// TODO Auto-generated method stubSystem.out.println("滑动时调用," + "e1===" + e1.getAction() + ",e2==="+ e2.getAction());return super.onScroll(e1, e2, distanceX, distanceY);}@Overridepublic void onShowPress(MotionEvent e) {// TODO Auto-generated method stubSystem.out.println("短按,onShowPress==" + e.getAction());super.onShowPress(e);}@Override// 按下时调用public boolean onSingleTapConfirmed(MotionEvent e) {// TODO Auto-generated method stubSystem.out.println("onSingleTapConfirmed,单击确认==" + e.getAction());// 0return super.onSingleTapConfirmed(e);}@Override// 按下时调用public boolean onSingleTapUp(MotionEvent e) {// TODO Auto-generated method stubSystem.out.println("onSingleTapUp==" + e.getAction());// 1return 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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);defaultListener = new DefaultListener();gestureDetector = new GestureDetector(getApplicationContext(),defaultListener);}@Overridepublic boolean onTouchEvent(MotionEvent event) {return gestureDetector.onTouchEvent(event);}}





原创粉丝点击