码迷,mamicode.com
首页 > 其他好文 > 详细

GestureDetector使用实例

时间:2016-05-07 08:47:45      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

GestureDetector使用实例


package com.example.test1;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.Toast;
/*双击时调用过程如下onDown(第一下按下)        onSingleTapUp(第一下抬起)      onDoubleTap,onDoubleTapEvent,onDown(第二下按下)    onDoubleTapEvent(第二下抬起)*/
class MyGestureLintener extends SimpleOnGestureListener {
private Context context;
public MyGestureLintener(Context context) {
    super();
    this.context = context;
}

// 单击,触摸屏按下时立刻触发
@Override
public boolean onDown(MotionEvent e) {
    // TODO Auto-generated method stub

    System.out.println("onDown");
    return true;
}
// 双击,手指在触摸屏上迅速点击第二下时触发
@Override
public boolean onDoubleTap(MotionEvent e) {
    // TODO Auto-generated method stub

     System.out.println("onDoubleTap");
    return super.onDoubleTap(e);
}

// 双击的按下跟抬起各触发一次
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
    // TODO Auto-generated method stub

     System.out.println("onDoubleTapEvent");
    return super.onDoubleTapEvent(e);
}



// 滑动,触摸屏按下后快速移动并抬起,会先触发滚动手势,跟着触发一个滑动手势
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    // TODO Auto-generated method stub

    System.out.println("onFling");
    return super.onFling(e1, e2, velocityX, velocityY);
}

// 长按,触摸屏按下后既不抬起也不移动,过一段时间后触发
@Override
public void onLongPress(MotionEvent e) {
    // TODO Auto-generated method stub


    System.out.println("onLongPress");
}

// 滚动,触摸屏按下后移动
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {

    System.out.println("onScroll");
    return true;
}

// 短按,触摸屏按下后片刻后抬起,会触发这个手势,如果迅速抬起则不会,长按也会在onLongPress之前调用此函数
@Override
public void onShowPress(MotionEvent e) {
    // TODO Auto-generated method stub

    System.out.println("onShowPress");

}

// 单击确认,即很快的按下并抬起,但并不连续点击第二下
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
    // TODO Auto-generated method stub

    System.out.println("onSingleTapConfirmed");
    return true;
}

// 抬起,手指离开触摸屏时触发(长按、滚动、滑动时,不会触发这个手势;单击和短按会出发这个手势)
@Override
public boolean onSingleTapUp(MotionEvent e) {
    // TODO Auto-generated method stub


    System.out.println("onSingleTapUp");
    return true;
}
}
public class MainActivity extends Activity {
private GestureDetector mGestureDetector;//手势对象
private MyGestureLintener myGestureLintener;//手势监听的接口对象

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main11);
    myGestureLintener = new MyGestureLintener(this);

    //手势对象的构造方法
    mGestureDetector = new GestureDetector(this,
            myGestureLintener);
}

/**GestureDetector类的onTouchEvent方法用来辨别不同的手势*/
@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean b = false;
    int i = event.getAction();
    int j = MotionEvent.ACTION_MOVE;
    System.out.println(i+"<----------------->"+j);
    b = mGestureDetector.onTouchEvent(event);
    return b;
}


}

GestureDetector使用实例

标签:

原文地址:http://blog.csdn.net/guangmomo/article/details/51330188

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!