`

Android GestureDetector手势识别类

 
阅读更多

为了加强鼠标响应事件,Android提供了GestureDetector手势识别类。通过GestureDetector.OnGestureListener 来获取当前被触发的操作手势(Single Tap Up、Show Press、Long Press、Scroll、Down、Fling),具体包括以下几种:

boolean  onDoubleTap(MotionEvent e)
解释:双击的第二下Touch down时触发
boolean  onDoubleTapEvent(MotionEvent e)
解释:双击的第二下Touch down和up都会触发,可用e.getAction()区分。
boolean  onDown(MotionEvent e)
解释:Touch down时触发
boolean  onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
解释:Touch了滑动一点距离后,up时触发。
void  onLongPress(MotionEvent e)
解释:Touch了不移动一直Touch down时触发
boolean  onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
解释:Touch了滑动时触发。
void  onShowPress(MotionEvent e)
解释:Touch了还没有滑动时触发
(与onDown,onLongPress比较
onDown只要Touch down一定立刻触发。
而Touchdown后过一会没有滑动先触发onShowPress再是onLongPress。
所以Touchdown后一直不滑动,onDown->onShowPress->onLongPress这个顺序触发。


boolean  onSingleTapConfirmed(MotionEvent e)
boolean  onSingleTapUp(MotionEvent e)
解释:上面这两个函数都是在touch down后又没有滑动(onScroll),又没有长按(onLongPress),然后Touchup时触发。

点击一下非常快的(不滑动)Touchup:
onDown->onSingleTapUp->onSingleTapConfirmed
点击一下稍微慢点的(不滑动)Touchup:
onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed

使用GestureDetector需要在View中重写onTouchEvent事件,例如:

GestureDetector mGesture = null;
@Override
	public boolean onTouch(View v, MotionEvent event)
	{
		// TODO Auto-generated method stub
		return mGesture.onTouchEvent(event);
	}

  详细的测试例子一:

package com.jiubang.android.gesturetest;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnTouchListener;
import android.widget.Button;

public class GestureActivity extends Activity
			implements OnTouchListener
{
	private Button mButton = null;
	GestureDetector mGesture = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Log.i("TEST", "onCreate");
        mButton = (Button)findViewById(R.id.button1);
        mButton.setOnTouchListener(this);
        mGesture = new GestureDetector(this, new GestureListener());
    }

	@Override
	public boolean onTouch(View v, MotionEvent event)
	{
		// TODO Auto-generated method stub
		return mGesture.onTouchEvent(event);
	}
	
	class GestureListener extends SimpleOnGestureListener
	{

		@Override
		public boolean onDoubleTap(MotionEvent e)
		{
			// TODO Auto-generated method stub
			Log.i("TEST", "onDoubleTap");
			return super.onDoubleTap(e);
		}

		@Override
		public boolean onDown(MotionEvent e)
		{
			// TODO Auto-generated method stub
			Log.i("TEST", "onDown");
			return super.onDown(e);
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY)
		{
			// TODO Auto-generated method stub
			Log.i("TEST", "onFling:velocityX = " + velocityX + " velocityY" + velocityY);
			return super.onFling(e1, e2, velocityX, velocityY);
		}

		@Override
		public void onLongPress(MotionEvent e)
		{
			// TODO Auto-generated method stub
			Log.i("TEST", "onLongPress");
			super.onLongPress(e);
		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY)
		{
			// TODO Auto-generated method stub
			Log.i("TEST", "onScroll:distanceX = " + distanceX + " distanceY = " + distanceY);
			return super.onScroll(e1, e2, distanceX, distanceY);
		}

		@Override
		public boolean onSingleTapUp(MotionEvent e)
		{
			// TODO Auto-generated method stub
			Log.i("TEST", "onSingleTapUp");
			return super.onSingleTapUp(e);
		}
		
	}
}

测试例子二:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
import android.widget.Toast;

public class GestureActivity extends Activity implements OnTouchListener,
        OnGestureListener {

    GestureDetector detector;

    public GestureActivity() {
        detector = new GestureDetector(this);
    }
    
    public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.main);  
            TextView tv = (TextView) findViewById(R.id.TextView001);
            //设置tv的监听器  
            tv.setOnTouchListener(this);  
            tv.setFocusable(true);
            //必须,view才能够处理不同于Tap(轻触)的hold
            tv.setClickable(true);  
            tv.setLongClickable(true);  
            detector.setIsLongpressEnabled(true);  
    }  
    
    
    /* 
     * 在onTouch()方法中,我们调用GestureDetector的onTouchEvent()方法,将捕捉到的MotionEvent交给GestureDetector 
     * 来分析是否有合适的callback函数来处理用户的手势 
     */   
    public boolean onTouch(View v, MotionEvent event) {  
        return detector.onTouchEvent(event);  
    }  
  
    // 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发  
    public boolean onDown(MotionEvent arg0) {  
        Log.i("MyGesture", "onDown");  
        Toast.makeText(this, "onDown", Toast.LENGTH_SHORT).show();  
        return true;  
    }  
      
    /* 
     * 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发 
     * 注意和onDown()的区别,强调的是没有松开或者拖动的状态 
     */  
    public void onShowPress(MotionEvent e) {  
        Log.i("MyGesture", "onShowPress");  
        Toast.makeText(this, "onShowPress", Toast.LENGTH_SHORT).show();  
    }  
      
    // 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发  
    public boolean onSingleTapUp(MotionEvent e) {  
        Log.i("MyGesture", "onSingleTapUp");  
        Toast.makeText(this, "onSingleTapUp", Toast.LENGTH_SHORT).show();  
        return true;  
    }  
      
    // 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发  
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {  
        Log.i("MyGesture", "onFling");  
        
        // 参数解释:  
        // e1:第1个ACTION_DOWN MotionEvent  
        // e2:最后一个ACTION_MOVE MotionEvent  
        // velocityX:X轴上的移动速度,像素/秒  
        // velocityY:Y轴上的移动速度,像素/秒  
      
        // 触发条件 :  
        // X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒  
          
        final int FLING_MIN_DISTANCE = 100, FLING_MIN_VELOCITY = 200;  
        if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {  
            // Fling left  
            Log.i("MyGesture", "Fling left");  
            Toast.makeText(this, "Fling Left", Toast.LENGTH_SHORT).show();  
        } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {  
            // Fling right  
            Log.i("MyGesture", "Fling right");  
            Toast.makeText(this, "Fling Right", Toast.LENGTH_SHORT).show();  
        } else if(e2.getY()-e1.getY()>FLING_MIN_DISTANCE && Math.abs(velocityY)>FLING_MIN_VELOCITY) {
            // Fling down  
            Log.i("MyGesture", "Fling down");  
            Toast.makeText(this, "Fling down", Toast.LENGTH_SHORT).show();
        } else if(e1.getY()-e2.getY()>FLING_MIN_DISTANCE && Math.abs(velocityY)>FLING_MIN_VELOCITY) {
            // Fling up  
            Log.i("MyGesture", "Fling up");  
            Toast.makeText(this, "Fling up", Toast.LENGTH_SHORT).show();
        }  
        
        
        return false; 
         
    }  
      
    // 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发  
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {  
        Log.i("MyGesture", "onScroll");  
        Toast.makeText(this, "onScroll", Toast.LENGTH_LONG).show();  
        return true;  
    }  
      
    // 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发  
    public void onLongPress(MotionEvent e) {  
        Log.i("MyGesture", "onLongPress");  
        Toast.makeText(this, "onLongPress", Toast.LENGTH_LONG).show();  
    }  
    

}

 

 摘自:http://blog.csdn.net/aben_2005/article/details/6417423

        http://www.cnblogs.com/xiaobuild/archive/2011/09/02/2163866.html

GestureDetector API :http:www.cnblogs.com/over140/archive/2011/01/17/1937044.html


分享到:
评论

相关推荐

    Android手势识别GestureDetector分析

    在Android系统中,每一次手势交互都会依照以下顺序执行。 1. 接触接触屏一刹那,触发一个MotionEvent事件。 2. 该事件被OnTouchListener监听,在其...3. 通过GestureDetector(手势识别器)转发次MotionEvent对象。

    android GestureDetector类及其用法

    Android sdk给我们提供了GestureDetector(Gesture:手势Detector:识别)类,通过这个类我们可以识别很多的手势,主要是通过他的onTouchEvent(event)方法完成了不同手势的识别。虽然他能识别手势,但是不同的手势要...

    android使用gesturedetector手势识别示例分享

    代码如下:public class MyGestureLintener extends SimpleOnGestureListener {private Context context;public MyGestureLintener(Context context) { super(); this.context = context;} // 单击,触摸屏按下时...

    Android GestureDetector用户手势检测实例讲解

    主要为大家详细介绍了Android GestureDetector用户手势检测实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    Android手势识别器GestureDetector使用详解

    主要为大家详细介绍了Android手势识别器GestureDetector的使用方法解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    Android根据手势控制视频音量亮度快进

    Android根据手势控制视频音量亮度快进,android开发之GestureDetector手势识别(调节音量、亮度、快进和后退)

    Android GestureDetector实现手势滑动效果

    本文实例为大家分享了Android GestureDetector实现手势滑动的具体代码,供大家参考,具体内容如下 目标效果:   程序运行,手指在屏幕上从左往右或者从右往左滑动超过一定距离,就会吐司输出滑动方向和距离。 1....

    简述Android触摸屏手势识别

    很多时候,利用触摸屏的Fling、Scroll等Gesture(手势)操作来操作会...Android Developer讨论组里也有不少人有和我类似的问题,结合他们提到的方法和我所做的实验,我将给大家简单讲述一下Android中手势识别的实现。

    Android GestureDetector手势滑动使用实例讲解

    GestureDetector类可以让我们快速的处理手势事件,如点击,滑动等。 使用GestureDetector分三步: 1. 定义GestureDetector类 2. 初始化手势类,同时设置手势监听 3. 将touch事件交给gesture处理 先来了解一下...

    Android 手势识别的API简单使用

    对GestureOverlayView, GestureDetector,SimpleOnGestureListener的简单使用。

    Android编程使用GestureDetector实现简单手势监听与处理的方法

    添加手势识别监听步骤: 一、给相应的控件添加触摸监听事件, 二、利用GestureDetector转发这个触摸事件。 三、事先定义好一个实现simpleongestureListener这个监听的接口的类 四、在这个监听中处理各种事件。 具体...

    深入理解Android手势识别

    不过,为了提高我们的APP的用户体验,有时候我们需要识别用户的手势,Android给我们提供的手势识别工具GestureDetector就可以帮上大忙了。 基础 GestureDetector的工作原理是,当我们接收到用户触摸消息时,将这个...

    Android自定义GestureDetector实现手势ImageView

    Android手势ImageView三部曲(一) Android手势ImageView三部曲(二) Android手势ImageView三部曲(三) 前面我们讲到了ScaleGestureDetector这个工具类,我在疑惑,为什么搞出一个ScaleGestureDetector,不顺带...

    Android View进行手势识别详解

     很多网友发现Android中手势识别提供了两个类,由于Android 1.6以下的版本比如cupcake中无法使用android.view.GestureDetector,而android.gesture.Gesture是Android 1.6开始支持的,考虑到仍然有使用Android 1.5...

    理解Android的手势识别提高APP的用户体验

    不过,为了提高我们的APP的用户体验,有时候我们需要识别用户的手势,Android给我们提供的手势识别工具GestureDetector就可以帮上大忙了。 基础 GestureDetector的工作原理是,当我们接收到用户触摸消息时,将这个...

    Android自定义viewgroup可滚动布局 GestureDetector手势监听(5)

    这篇效果和上一篇://www.jb51.net/article/100638.htm的效果是一样的,但是不再在OnTouchEvent中写代码,而是使用系统自带的类GestureDetector来监听手势以及滑动事件等等,它内置了滑动,点击,长按等事件,而且有...

Global site tag (gtag.js) - Google Analytics