`

转载:Android GestureDetector手势识别类

 
阅读更多
原文地址:http://blog.csdn.net/aben_2005/article/details/6417423

为了加强鼠标响应事件,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);
		}
		
	}
}

分享到:
评论

相关推荐

    GestureDetector手势识别.zip

    在这个"GestureDetector手势识别.zip"项目中,我们可以推测它可能包含了一个实现手势识别的示例或者库,便于开发者理解和应用手势识别技术。 首先,我们来讨论一下人体关键点检测。这是计算机视觉领域的一个子课题...

    android开发之GestureDetector手势识别调节音量亮度快进和后退源码.zip

    android开发之GestureDetector手势识别调节音量亮度快进和后退源码.zip【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能...

    android GestureDetector类及其用法

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

    Android手势识别GestureDetector分析

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

    android开发GestureDetector手势识别调节音量亮度快进和后退完整源码

    在Android开发中,手势识别是提升用户体验的重要一环。`GestureDetector`是Android SDK提供的一种用于处理常见触摸事件和手势的工具类,它可以帮助我们轻松地实现如滑动、点击等手势的检测。本教程将深入讲解如何...

    android手势库识别

    在Android开发中,手势识别是提升用户体验的重要一环。Android手势库识别主要涉及的是如何让应用能够理解并响应用户的触摸动作,从而实现更直观、更便捷的交互方式。本教程适用于初学者,将深入探讨如何在Android...

    android手势识别

    Android SDK提供了一个名为`GestureDetector`的类,它是手势识别的基础。这个类可以帮助开发者检测常见的滑动、轻击、长按等基础手势。同时,`ScaleGestureDetector`用于识别捏合缩放手势,`RotateGestureDetector`...

    Android触屏手势识别Demo源码.rar

    1. **基本手势识别**:Android SDK内置了`GestureDetector`类,用于识别常见的滑动(Swipe)、长按(LongPress)等手势。开发者可以创建`GestureDetector`实例,然后覆写其回调方法,如`onDown()`, `onFling()`, `...

    Android手势识别源码

    1. **手势识别基础**:Android SDK提供了GestureDetector类,它是手势识别的核心。GestureDetector能够处理基本的滑动(Swipe)、轻击(Tap)以及长按(Long Press)等手势。开发者可以通过覆写GestureDetector的...

    安卓 android 用户自定义手势 手势识别

    1. 基于View的手势识别: Android SDK提供了一些内置的GestureOverlayView类,可以方便地添加到布局中,让用户在指定区域内绘制手势。系统会自动处理手势的识别,并通过监听器回调手势事件。 2. 使用...

    android手势识别源码讲解

    首先,Android的手势识别主要依赖于`GestureDetector`和`ScaleGestureDetector`这两个内置类。`GestureDetector`用于处理基本的单指和双指触摸事件,如单击、长按、滑动等。而`ScaleGestureDetector`则专门处理双指...

    Android 触屏手势识别GestureTest.rar

    Android提供了GestureDetector、SimpleOnGestureListener等类来支持手势识别。GestureDetector用于检测滑动、双击等基本手势,而SimpleOnGestureListener则提供了处理这些手势的回调方法。 二、GestureDetector与...

    android 手势识别学习

    本教程将聚焦于Android手势识别的实现,主要关注`GestureDetector`类。 `GestureDetector`是Android SDK中的一个核心组件,用于处理基本的手势检测。它处理了滑动(swipe)、点击(tap)和长按(long press)等常见...

    android触屏手势识别

    总结起来,Android触屏手势识别涉及了触摸事件处理、`GestureDetector`、`ScaleGestureDetector`、`GestureOverlayView`以及自定义手势识别等多个方面。理解并熟练运用这些知识点,可以创建出更加直观、友好的用户...

    2011.10.11——— android GestureDetector 测试OnGestureListener

    在Android开发中,手势识别是实现用户交互的关键部分。`GestureDetector`类是Android SDK提供的一种强大工具,用于处理各种触摸事件,如滑动、点击等。这篇2011年的博客文章“2011.10.11——— android ...

    Android 带手势识别的音乐播放器

    在Android平台上,开发一款带有手势识别的音乐播放器是一项创新且具有挑战性的任务。这个项目融合了Android应用开发、媒体处理、用户界面设计以及手势识别技术。以下将详细阐述涉及的知识点: 1. **Android应用开发...

    GestureDetector和SimpleOnGestureListener的使用教程

    GestureDetector是Android SDK中的一种手势识别机制,它可以识别多种手势,例如down、up、scroll、filing等。通过GestureDetector的构造方法,可以将SimpleOnGestureListener对象传递进去,从而使GestureDetector...

    Android 触屏手势识别GestureTest.zip

    1. **手势识别库(Gesture Recognizer)**:Android SDK提供了一套名为`GestureDetector`的类,用于识别用户在屏幕上的一系列滑动、点击等基本手势。`GestureDetector`通过监听`MotionEvent`事件,解析出用户的手势...

    android 之GestureOverlayView手势识别

    `Gesture`类是Android手势识别的核心,它包含了手势的所有轨迹点信息。通过`Gesture`对象,我们可以使用`GestureLibrary`和`GestureDetector`进行手势的识别和比较。`GestureLibrary`用于存储预定义的手势,而`...

Global site tag (gtag.js) - Google Analytics