一、手势交互过程:

  1)触屏时,触发MotionEvent事件。

  2)被OnTouchListener监听,在onTouch()中获得MotionEvent对象。

  3)GestureDetector转发MotionEvent对象至OnGestureListener。

  4)OnGestureListener获得该对象,根据该对象封装的信息做出合适的反馈。

二、需要用到的类和接口

  1、MotionEvent:

  1)用于封装手势、触摸笔,轨迹球等动作事件。

  2)内部封装用于记录横轴和纵轴坐标的属性X和Y。

  

  2、GestureDetector:

  识别各种手势:按下,移动,弹起。

  3、OnGestureListener接口

  1)手势交互的监听接口,其提供多个抽象方法。

    a) onDown(MotionEvent e);   //单击

    b) onSingleTapUp(MotionEvent e);  //抬起

    c) onShowPress(MotionEvent e);   //短按

    d) onLongPress(MotionEvent e);   //长按

    e) onScoll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY)  //滚动

    f) onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY)    //滑动

  2)根据GestureDetector的手势识别结果调用相对应的方法。

  4、OnDoubleTapListener接口:

    a) onDoubleTap(MotionEvent e);   //双击

    b) onDoubleTapEvent(MotionEvent e);  //双击按下和抬起各触发一次

    c) onSingleTapConfirmed(MotionEvent e);  //单击确认,很快的按下并弹起,但不点击第二下。

  5、SimpleOnGesttureListener类,实现了OnGestureListener和OnDoubleTapListener接口,如果我们需要实现手势,只需要继承这个类,实现对应的手势方法即可。

    

示例:根据左右拖拽切换图片,直接实现接口

main.xml,添加一个ImageView显示图片,默认显示第一张图片

<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="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <ImageView
android:id="@+id/img_girl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/girl1" /> </LinearLayout>

main.java

package com.example.gesturedetectordemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity implements OnTouchListener, OnGestureListener { //定义一个GestureDetector对象,用来处理各种手势,实现了OnTouchListener接口
GestureDetector myGestureDetector;
// 图片框
ImageView imggirl;
// 存放女孩图片的数组
int[] girls = { R.drawable.girl1, R.drawable.girl2, R.drawable.girl3, R.drawable.girl4, R.drawable.girl5 };
// 存放数组下标
private int index; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); myGestureDetector = new GestureDetector(this); imggirl = (ImageView) findViewById(R.id.img_girl); //添加OnTouchListener事件
imggirl.setOnTouchListener(this);
//下面两个要记得设哦,不然就没法处理轻触以外的事件了,例如抛掷动作。
imggirl.setLongClickable(true);
myGestureDetector.setIsLongpressEnabled(true);
} // 向后移动
public void goNext() {
index++;
index = Math.abs(index % girls.length);
imggirl.setImageResource(girls[index]);
} // 向前移动
public void goPrevious() {
index--;
index = Math.abs(index % girls.length);
imggirl.setImageResource(girls[index]);
} @Override //处理用户的触碰请求
public boolean onTouch(View v, MotionEvent event) {
//转交给GestureDetector来处理
myGestureDetector.onTouchEvent(event);
return false;
} @Override //在按下动作时被调用
public boolean onDown(MotionEvent e) {
//goNext();
return false;
} @Override //按下动作松开被调用
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub } @Override //在弹起时被调用
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
} @Override //在滚动时被调用
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// TODO Auto-generated method stub
return false;
} @Override //在长按时被调用
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub } @Override //在抛掷动作时被调用
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() - e2.getX() > 50) {
goNext();
Toast.makeText(MainActivity.this, "从右向左拖拽" + index, Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > 50) {
goPrevious();
Toast.makeText(MainActivity.this, "从左向右拖拽" + index, Toast.LENGTH_SHORT).show();
}
return false;
} }

  

Android学习(十六) 通过GestureDetector进行手势识别的更多相关文章

  1. 强化学习(十六) 深度确定性策略梯度(DDPG)

    在强化学习(十五) A3C中,我们讨论了使用多线程的方法来解决Actor-Critic难收敛的问题,今天我们不使用多线程,而是使用和DDQN类似的方法:即经验回放和双网络的方法来改进Actor-Cri ...

  2. 【Android】完善Android学习(六:API 4.0)

    备注:之前Android入门学习的书籍使用的是杨丰盛的<Android应用开发揭秘>,这本书是基于Android 2.2API的,目前Android已经到4.4了,更新了很多的API,也增 ...

  3. android 学习随笔六(网络要求及配置)

    android在4.0之后已经不允许在主线程执行http请求了. 主线程阻塞,应用会停止刷新界面,停止响应用户任何操作,耗时操作不要写在主线程   只有主线程才能修改UI ANR异常:Applicat ...

  4. android学习十二(android的Content Provider(内容提供器)的使用)

    文件存储和SharePreference存储以及数据存储一般为了安全,最好用于当前应用程序中訪问和存储数据.内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据共享的功能 ...

  5. Android学习十---Android Camera

    Android camera用来拍照和拍摄视频的先看一下最后实现的效果图             最后的效果图 一.准备 在你的应用程序上使用android拍照设备,需要考虑以下几个方面 1. 是否是 ...

  6. android学习十四(android的接收短信)

    收发短信是每一个手机主要的操作,android手机当然也能够接收短信了. android系统提供了一系列的API,使得我们能够在自己的应用程序里接收和发送短信. 事实上接收短信主要是利用我们前面学过的 ...

  7. Scala学习十六——XML处理

    一.本章要点 XML字面量<like>this</like>的类型为NodeSeq 可以在XML字面量中内嵌Scala代码 Node的child属性产出后代节点 Node的at ...

  8. Android学习十二:跑马灯程序实现(简单联系)

    package org.tonny; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; ...

  9. Android学习十二:自定义控件学习

    自定义控件: 1.定义控件的属性,atts.xml 2.代码实现自定义控件的控制 3.引用该控件 首先定义相关的属性 <?xml version="1.0" encoding ...

  10. Android学习十:appcompat_v7相关

    error: Error retrieving parent for item: No resource found that matches the given name 'android:Wind ...

随机推荐

  1. 百度之星初赛(A)——T5

    今夕何夕 Problem Description 今天是2017年8月6日,农历闰六月十五. 小度独自凭栏,望着一轮圆月,发出了“今夕何夕,见此良人”的寂寞感慨. 为了排遣郁结,它决定思考一个数学问题 ...

  2. 语音提示辅助类MySoundAlertUtil

    package com.jlb.scan.util; import android.content.Context; import android.media.AudioManager; import ...

  3. webdiyer aspnet pager最近又用这个。还是记录下。

    这个是页面里的代码需要在上面引入: <%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer&quo ...

  4. 转 C++中的static关键字

    C++中的static关键字 C++的static有两种用法:面向过程程序设计中的static和面向对象程序设计中的static.前者应用于普通变量和函数,不涉及类:后者主要说明static在类中的作 ...

  5. commons-lang3中DateUtils类方法介绍

    添加commons-lang3的Maven依赖 <dependency> <groupId>org.apache.commons</groupId> <art ...

  6. 使用函数方式生成UUID

    1.默认生成的UUID是有 “-” 分隔符的 例如: public static void main(String[] args){ String uuid = UUID.randomUUID().t ...

  7. RobotFramework自动化4-批量操作案例【转载】

    本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/robotframework/ 前言 有时候一个页面上有多个对象需要操作,如果一个个去定 ...

  8. Educational Codeforces Round 32

    http://codeforces.com/contest/888 A Local Extrema[水] [题意]:计算极值点个数 [分析]:除了第一个最后一个外,遇到极值点ans++,包括极大和极小 ...

  9. 【bzoj2160】【啦啦队排练】manacher(马拉车)+差分+快速幂

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=34562780 向大(hei)佬(e)势力学(di ...

  10. hdu 5206 Four Inages Strategy 计算几何

    题目链接:HDU - 5206 Young F found a secret record which inherited from ancient times in ancestral home b ...