Android手势操作简单实例讲解

上一篇介绍的onTouch提供的事件还是相对较简单,如果需要处理一些复杂的手势,用这个接口就会很麻烦,因为我们要根据用户触摸的轨迹去判断是什么手势。幸好Android SDK给我们提供了GestureDetector类,通过这个类我们可以识别很多的手势,主要是通过他的onTouchEvent(event)方法完成了不同手势的识别。
GestureDetector这个类对外提供了两个接口和一个外部类:

 •接口:OnGestureListener,OnDoubleTapListener
 •外部类:SimpleOnGestureListener

这个外部类,其实是两个接口中所有函数的集成,它包含了这两个接口里所有必须要实现的函数而且都已经重写,但所有方法体都是空的;不同点在于:该类是static class,程序员可以在外部继承这个类,重写里面的手势处理方法。

OnGestureListener有下面的几个动作:
 •onDown(MotionEvent e):用户按下屏幕就会触发。
 •onShowPress(MotionEvent e):如果按下的时间超过瞬间,而且在按下的时候没有松开或者是拖动的,那么onShowPress就会执行,具体这个瞬间是多久,我也不清楚…
 •onLongPress(MotionEvent e):长按触摸屏,超过一定时长,就会触发这个事件。
触发顺序:onDown->onShowPress->onLongPress
 •onSingleTapUp(MotionEvent e):一次单独的轻击抬起操作,也就是轻击一下屏幕,立刻抬起来,才会有这个触发,当然,如果除了Down以外还有其它操作,那就不再是Single操作了,所以也就不会触发这个事件。
触发顺序:
点击一下非常快的(不滑动)Touchup:
onDown->onSingleTapUp->onSingleTapConfirmed
 点击一下稍微慢点的(不滑动)Touchup:
onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed
 •onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) :滑屏,用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发。
参数解释:
e1:第1个ACTION_DOWN MotionEvent
e2:最后一个ACTION_MOVE MotionEvent
velocityX:X轴上的移动速度,像素/秒
velocityY:Y轴上的移动速度,像素/秒
 •onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY):在屏幕上拖动事件。无论是用手拖动view,或者是以抛的动作滚动,都会多次触发,这个方法在ACTION_MOVE动作发生时就会触发。

OnDoubleTapListener有下面的几个动作:
 •onSingleTapConfirmed(MotionEvent e):单击事件。用来判定该次点击是SingleTap而不是DoubleTap,如果连续点击两次就是DoubleTap手势,如果只点击一次,系统等待一段时间后没有收到第二次点击则判定该次点击为SingleTap而不是DoubleTap,然后触发SingleTapConfirmed事件。
触发顺序是:OnDown->OnsingleTapUp->OnsingleTapConfirmed
 •onDoubleTap(MotionEvent e):双击事件。
 •onDoubleTapEvent(MotionEvent e):双击间隔中发生的动作。指触发onDoubleTap以后,在双击之间发生的其它动作,包含down、up和move事件。

关于onSingleTapConfirmed和onSingleTapUp的区别:onSingleTapUp,只要手抬起就会执行,而对于onSingleTapConfirmed来说,如果双击的话,则onSingleTapConfirmed不会执行。

使用Gesture
 •使用GestureDetector.OnGestureListener接口

要使用OnGestureListener接口,大致有几步要走:
1、创建OnGestureListener监听函数:

private class gestureListener implements GestureDetector.OnGestureListener{

}

2、创建GestureDetector实例:
构造函数有下面三个,根据需要选择:

GestureDetector gestureDetector=new GestureDetector(GestureDetector.OnGestureListener listener); 
GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.OnGestureListener listener); 
GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener);

注:GestureDetector现在已经是deprecation状态,现在推荐GestureDetectorCompat

GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener);
GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener, Handler handler);

 3、onTouch(View v, MotionEvent event)中拦截,在onTouch()方法中,我们调用GestureDetector的onTouchEvent()方法,将捕捉到的MotionEvent交给GestureDetector来分析是否有合适的callback函数来处理用户的手势

public boolean onTouch(View v, MotionEvent event) {
 return gestureDetectorCompat.onTouchEvent(event);
}

 4、控件绑定

TextView tv = (TextView)findViewById(R.id.tv);
tv.setOnTouchListener(this);

实现代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"> 

 <TextView
  android:id="@+id/tv"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_margin="50dip"
  android:background="#76EE00"
  android:text="Gesture Detector" />
</RelativeLayout>
public class MainActivity extends Activity implements OnTouchListener{
 private GestureDetectorCompat mGestureDetectorCompat; 

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mGestureDetectorCompat = new GestureDetectorCompat(this, new gestureListener());
  TextView tv = (TextView)findViewById(R.id.tv);
  tv.setOnTouchListener(this);
  tv.setFocusable(true);
  tv.setClickable(true);
  tv.setLongClickable(true);
 } 

 public boolean onTouch(View v, MotionEvent event) {
  return mGestureDetectorCompat.onTouchEvent(event);
 } 

 private class gestureListener implements GestureDetector.OnGestureListener{
  public boolean onDown(MotionEvent e) {
   showlog("onDown");
   Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show();
   return false;
  } 

  public void onShowPress(MotionEvent e) {
   showlog("onShowPress");
   Toast.makeText(MainActivity.this, "onShowPress", Toast.LENGTH_SHORT).show();
  } 

  public boolean onSingleTapUp(MotionEvent e) {
   showlog("onSingleTapUp");
   Toast.makeText(MainActivity.this, "onSingleTapUp", Toast.LENGTH_SHORT).show();
   return true;
  } 

  public boolean onScroll(MotionEvent e1, MotionEvent e2,
    float distanceX, float distanceY) {
   showlog("onScroll:"+(e2.getX()-e1.getX()) +" "+distanceX);
   Toast.makeText(MainActivity.this, "onScroll", Toast.LENGTH_LONG).show();
   return true;
  } 

  public void onLongPress(MotionEvent e) {
    showlog("onLongPress");
    Toast.makeText(MainActivity.this, "onLongPress", Toast.LENGTH_LONG).show();
  } 

  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
    float velocityY) {
   showlog("onFling");
   Toast.makeText(MainActivity.this, "onFling", Toast.LENGTH_LONG).show();
   return true;
  }
 }; 

 public void showlog(String info) {
  System.out.print("GestureDetector "+info);
 }
}

•使用GestureDetector.OnDoubleTapListener接口
 实现OnDoubleTapListener接口的前提是先实现OnGestureListener接口,其实除了第1步,2、3、4步和上面完全一样,不再赘述,下面看下第一步:

同时创建OnGestureListener和OnDoubleTapListener监听函数:
方法一:新建一个类同时派生自OnGestureListener和OnDoubleTapListener:

代码如下:

private class gestureListener implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener{ 
    }

方法二:使用GestureDetector.setOnDoubleTapListener();函数设置监听:

/构建GestureDetector实例
mGestureDetector = new GestureDetector(new gestureListener());
private class gestureListener implements GestureDetector.OnGestureListener{
} 

//设置双击监听器
mGestureDetector.setOnDoubleTapListener(new doubleTapListener());
private class doubleTapListener implements GestureDetector.OnDoubleTapListener{
}

注:大家可以看到无论在方法一还是在方法二中,都需要派生自GestureDetector.OnGestureListener,前面我们说过GestureDetectorCompat 的构造函数,如下:

GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener);
GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener, Handler handler);

可以看到,它的两个构造函数参数都必须是OnGestureListener的实例。所以要想使用OnDoubleTapListener的几个函数,就必须先实现OnGestureListener。
实现代码:

public class MainActivity extends Activity implements OnTouchListener{
 private GestureDetectorCompat mGestureDetectorCompat; 

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mGestureDetectorCompat = new GestureDetectorCompat(this, new gestureListener());
  TextView tv = (TextView)findViewById(R.id.tv);
  tv.setOnTouchListener(this);
  tv.setFocusable(true);
  tv.setClickable(true);
  tv.setLongClickable(true);
 } 

 public boolean onTouch(View v, MotionEvent event) {
  return mGestureDetectorCompat.onTouchEvent(event);
 } 

 private class gestureListener implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener{
  public boolean onDown(MotionEvent e) {
   showlog("onDown");
   Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show();
   return false;
  } 

  public void onShowPress(MotionEvent e) {
   showlog("onShowPress");
   Toast.makeText(MainActivity.this, "onShowPress", Toast.LENGTH_SHORT).show();
  } 

  public boolean onSingleTapUp(MotionEvent e) {
   showlog("onSingleTapUp");
   Toast.makeText(MainActivity.this, "onSingleTapUp", Toast.LENGTH_SHORT).show();
   return true;
  } 

  public boolean onScroll(MotionEvent e1, MotionEvent e2,
    float distanceX, float distanceY) {
   showlog("onScroll:"+(e2.getX()-e1.getX()) +" "+distanceX);
   Toast.makeText(MainActivity.this, "onScroll", Toast.LENGTH_LONG).show();
   return true;
  } 

  public void onLongPress(MotionEvent e) {
    showlog("onLongPress");
    Toast.makeText(MainActivity.this, "onLongPress", Toast.LENGTH_LONG).show();
  } 

  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
    float velocityY) {
   showlog("onFling");
   Toast.makeText(MainActivity.this, "onFling", Toast.LENGTH_LONG).show();
   return true;
  }

  public boolean onSingleTapConfirmed(MotionEvent e) {
   showlog("onSingleTapConfirmed");
   Toast.makeText(MainActivity.this, "onSingleTapConfirmed",Toast.LENGTH_LONG).show();
   return true;
  } 

  public boolean onDoubleTap(MotionEvent e) {
   showlog("onDoubleTap");
   Toast.makeText(MainActivity.this, "onDoubleTap", Toast.LENGTH_LONG).show();
   return true;
  }

  public boolean onDoubleTapEvent(MotionEvent e) {
   showlog("onDoubleTapEvent");
   Toast.makeText(MainActivity.this, "onDoubleTapEvent", Toast.LENGTH_LONG).show();
   return true;
  }

 public void showlog(String info) {
  System.out.print("GestureDetector "+info);
 }
}

•使用GestureDetector.SimpleOnGestureListener类

使用OnGestureListener和OnDoubleTapListener接口,这样需要重载接口所有的方法,适合监听所有的手势,如果我们只想监听某个手势或某几个手势呢,这时候就可以使用SimpleOnGestureListener类了。
它与前两个不同的是:
1、这是一个类,在它基础上新建类的话,要用extends派生而不是用implements继承!
2、OnGestureListener和OnDoubleTapListener接口里的函数都是强制必须重写的,即使用不到也要重写出来一个空函数但在SimpleOnGestureListener类的实例或派生类中不必如此,可以根据情况,用到哪个函数就重写哪个函数,因为SimpleOnGestureListener类本身已经实现了这两个接口的所有函数,只是里面全是空的而已。

public class MainActivity extends Activity implements OnTouchListener {
 private GestureDetector mGestureDetector; 

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mGestureDetector = new GestureDetector(new simpleGestureListener());
  TextView tv = (TextView)findViewById(R.id.tv);
  tv.setOnTouchListener(this);
  tv.setFocusable(true);
  tv.setClickable(true);
  tv.setLongClickable(true);
 } 

 public boolean onTouch(View v, MotionEvent event) {
  return mGestureDetector.onTouchEvent(event);
 } 

 private class simpleGestureListener extends GestureDetector.SimpleOnGestureListener { 

  /*****OnGestureListener的函数*****/
  public boolean onDown(MotionEvent e) {
   Log.i("MyGesture", "onDown");
   Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show();
   return false;
  } 

  public void onShowPress(MotionEvent e) {
   Log.i("MyGesture", "onShowPress");
   Toast.makeText(MainActivity.this, "onShowPress", Toast.LENGTH_SHORT).show();
  } 

  public boolean onSingleTapUp(MotionEvent e) {
   Log.i("MyGesture", "onSingleTapUp");
   Toast.makeText(MainActivity.this, "onSingleTapUp",
     Toast.LENGTH_SHORT).show();
   return true;
  } 

  /*****OnDoubleTapListener的函数*****/
  public boolean onSingleTapConfirmed(MotionEvent e) {
   Log.i("MyGesture", "onSingleTapConfirmed");
   Toast.makeText(MainActivity.this, "onSingleTapConfirmed",Toast.LENGTH_LONG).show();
   return true;
  } 

  public boolean onDoubleTap(MotionEvent e) {
   Log.i("MyGesture", "onDoubleTap");
   Toast.makeText(MainActivity.this, "onDoubleTap", Toast.LENGTH_LONG).show();
   return true;
  }
 }
}

源码下载:http://xiazai.jb51.net/201609/yuanma/GestureDetector(jb51.net).rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 直接拿来用的Android刮奖控件

    直接上效果图 功能特色:  1.可以设置刮开后显示文字或图片  2.可以统计已刮开区域所占百分比 Demo下载地址:RubberDemo.rar 下面是源码: @SuppressLint("HandlerLeak") public class RubberView extends TextView { private static final int W = 480; private static final int H = 800; private static final int

  • android中图片加载到内存的实例代码

    本文演示android中图片加载到内存 首先设计界面: 代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="matc

  • Android Service总结及详细介绍

    1.Service的种类 按运行地点分类: 类别 区别  优点 缺点   应用 本地服务(Local) 该服务依附在主进程上,  服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外Local服务因为是在同一进程因此不需要IPC,也不需要AIDL.相应bindService会方便很多.  主进程被Kill后,服务便会终止.  非常常见的应用如:HTC的音乐播放服务,天天动听音乐播放服务. 远程服务(Remote) 该服务是独立的进程,  服务为独立的进程,对应进程名格式为所在包名

  • Android自定义View实现弹性小球效果

    照例先看效果图 自定义代码示例 public class BezierView extends View { Paint paint;//画笔 Path path;//路径 int radius = 50;//圆的半径 int time = 100;//计数时长 int index; int offsetIndex; float viewX, viewY;//图形中心点坐标 float width;//屏幕宽度 float partWidth;//屏幕宽度的1/4 int paddingLeft

  • Android 传感器--光照传感器详解及使用

    Android 设备中有许多传感器,其中有一个传感器控制着你屏幕亮度的变化.当你在很暗的地方使用手机,你设备的屏幕会自动调暗,从而保护你眼睛. 起着这样作用,Android是通过一款光照传感器来获取你周围环境亮度的变化.光照传感器一般在手机的顶部的位置. 要在程序中使用这款传感器 (1)首先要获取SensorManager传感器管理器服务:SensorManager sensorManager=(SensorManager)getSystemService(Context.SENSOR_SERV

  • Android 面试题汇总

        Android 70道面试题汇总不再愁面试 本文为开发者奉献了70道经典Android面试题加答案--重要知识点几乎都涉及到了,你还等啥,赶紧收藏吧!! 1. 下列哪些语句关于内存回收的说明是正确的? (b) A. 程序员必须创建一个线程来释放内存 B. 内存回收程序负责释放无用内存 C. 内存回收程序允许程序员直接释放内存 D. 内存回收程序可以在指定的时间释放内存对象 2. 下面异常是属于Runtime Exception 的是(abcd)(多选) A.ArithmeticExcep

  • Android 五大布局方式详解

    Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件从屏幕左上方布局组件. 表格布局(TableLayout):按照行列方式布局组件. 相对布局(RelativeLayout):相对其它组件的布局方式.  绝对布局(AbsoluteLayout):按照绝对坐标来布局组件.  1. 线性布局 线性布局是Android开发中最常见的一种布局方式,它是按照垂直或者水平方向来布局,通过"androi

  • Android手势操作简单实例讲解

    上一篇介绍的onTouch提供的事件还是相对较简单,如果需要处理一些复杂的手势,用这个接口就会很麻烦,因为我们要根据用户触摸的轨迹去判断是什么手势.幸好Android SDK给我们提供了GestureDetector类,通过这个类我们可以识别很多的手势,主要是通过他的onTouchEvent(event)方法完成了不同手势的识别. GestureDetector这个类对外提供了两个接口和一个外部类:  •接口:OnGestureListener,OnDoubleTapListener  •外部类

  • Android 自定义ContentProvider简单实例

    Android 自定义ContentProvider简单实例 Android允许我们定义自己的的ContentProvider对象来共享数据,练练手,简单来实现一下. 要使用ContentProvider来操作数据,必须要有保存数据的场所.可以使用文件或SQLite数据库的方式来保存数据,通常使用SQLite数据库. 1,创建一个数据库帮助类,归根结底都是它在操作数据库.代码如下: package com.njue; import android.content.Context; import

  • Android 对话框 Dialog使用实例讲解

    对话框 Dialog 什么是对话框 对话框是在当前的页面之上弹出的小窗口, 用于显示一些重要的提示信息, 提示用户的输入,确认信息,或显示某种状态.如 : 显示进度条对话框, 退出提示. 对话框的特点: 1, 当前界面弹出的小窗口. 2, 用户要与它进行交互, 可以接收用户输入的信息, 也可以反馈信息给用户. 常用对话框: 1, 普通对话框 AlertDialog 2, 进度条对话框 ProgressDialog 3, 日期对话框 DatePickerDialog 4, 时间对话框 TimePi

  • Android 登录处理简单实例(源码下载)

    Android 登录处理简单实例 今天整理一下之前在项目中写的关于某些界面需要登录判断处理.这里整理了一个简易的 Demo 模拟一下 登录情况 和 未登录情况 下的界面跳转处理, 效果如图: 以上分别模拟了,未登录和已登录 情况下的 界面跳转和当前界面事件处理.接下来我们来看一下实现方式. 先从核心代码开始看: public class GoLoginUtil { public static final String CLASSNAME = "className"; public st

  • Android 手势操作编程详解

    手势操作在我们使用智能设备的过程中奉献了不一样的体验.Android开发中必然会进行手势操作方面的编程.那么它的原理是怎样的呢?我们如何进行手势操作编程呢?        手势操作原理 首先,在Android系统中,每一次手势交互都会依照以下顺序执行. 1. 接触接触屏一刹那,触发一个MotionEvent事件. 2. 该事件被OnTouchListener监听,在其onTouch()方法里获得该MotionEvent对象. 3. 通过GestureDetector(手势识别器)转发次Motio

  • 基于spring中的aop简单实例讲解

    aop,即面向切面编程,面向切面编程的目标就是分离关注点,比如:一个骑士只需要关注守护安全,或者远征,而骑士辉煌一生的事迹由谁来记录和歌颂呢,当然不会是自己了,这个完全可以由诗人去歌颂,比如当骑士出征的时候诗人可以去欢送,当骑士英勇牺牲的时候,诗人可以写诗歌颂骑士的一生.那么骑士只需要关注怎么打仗就好了.而诗人也只需要关注写诗歌颂和欢送就好了,那么这样就把功能分离了.所以可以把诗人当成一个切面,当骑士出征的前后诗人分别负责欢送和写诗歌颂(记录).而且,这个切面可以对多个骑士或者明人使用,并不只局

  • python3 os进行嵌套操作的实例讲解

    在生活之中,我们想要去一个很远的地方,可能先走到坐车的地方,再从乘车去目的地.那么,我们是不是可以理解成函数嵌套也是这样,需要不同函数的组合才能最终实现某种运行的结果.在python3 os中同样有这样的嵌套操作存在,小伙伴跟小编一起看看和以前的学习有何不同. 如果想批量一个嵌套的文件,即在一个新建的文件里面,在建文件夹. os.makedirs() import os try: name = "python3" count = 1 for i in range(3): os.make

  • python shutil操作文件实例讲解

    1.使用函数shutil.make_archive()创建归档文件,并返回归档后的名称. import shutil path_1 = r'D:\PycharmProjects\Hello' path_2 = r'D:\PycharmProjects\Hello\shutil-test' new_path = shutil.make_archive(path_2,'zip',path_1) print(new_path) 2.使用函数shutil.unpack_archive(filename[

  • Android 单例模式 Singleton 简单实例设计模式解析

    单例模式 Singleton 简单实例设计模式解析 前言 今天我来全面总结一下Android开发中最常用的设计模式 - 单例模式. 关于设计模式的介绍,可以看下我之前写的:1分钟全面了解"设计模式" 目录 1. 引入 1.1 解决的是什么问题 之前说过,设计模式 = 某类特定问题的解决方案,那么单例模式是解决什么问题的解决方案呢? 含义:单例 =一个实例: 解决的问题:降低对象之间的耦合度 解决方法:单例模式,即实现一个类只有一个实例化对象,并提供一个全局访问点 1.2 实例引入 接下

  • Android手势操作示例(上/下/左/右的判断)

    本文实例讲述了Android手势操作方法.分享给大家供大家参考,具体如下: Android中提供了判断手势的接口,所以我们可以根据提供的API来实现各种各样的手势功能来提高手机应用的用户体验. 下面是我写的一段小Demo: GestureActivity.Java public class GestureActivity extends Activity { private GestureDetector gestureDetector; private Screen screen; @Over

随机推荐