Android编程使用GestureDetector实现简单手势监听与处理的方法
作者:ITzhongzi 时间:2021-11-18 17:07:43
本文实例讲述了Android编程使用GestureDetector实现简单手势监听与处理的方法。分享给大家供大家参考,具体如下:
添加手势识别监听步骤:
一、给相应的控件添加触摸监听事件,
二、利用GestureDetector转发这个触摸事件。
三、事先定义好一个实现simpleongestureListener这个监听的接口的类
四、在这个监听中处理各种事件。
具体代码如下:
MainActivity代码如下:
package com.example.gesturedetector;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ImageView img;
GestureDetector myGestureDetector;
class myGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX()-e2.getX()>50)
{
Toast.makeText(MainActivity.this,"从右往左滑动",Toast.LENGTH_LONG).show();
}else if(e2.getX()-e1.getX()>50){
Toast.makeText(MainActivity.this,"从左往右滑动",Toast.LENGTH_LONG).show();
}
return super.onFling(e1, e2, velocityX, velocityY);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.img);
myGestureDetector = new GestureDetector(new myGestureListener());
img.setOnTouchListener(new View.OnTouchListener() {
//motionEvent可以捕捉我们触摸屏幕的event事件
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
myGestureDetector.onTouchEvent(motionEvent);
return true;
}
});
}
}
activity_main的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.gesturedetector.MainActivity">
<ImageView
android:id="@+id/img"
android:src="@mipmap/ic_launcher"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
希望本文所述对大家Android程序设计有所帮助。
来源:http://blog.csdn.net/itzhongzi/article/details/51852286
标签:Android,GestureDetector,手势
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
IntelliJ IDEA 好用插件之analyze inspect code详解
2021-09-26 22:16:36
![](https://img.aspxhome.com/file/2023/9/67649_0s.png)
Java访问Hadoop分布式文件系统HDFS的配置说明
2021-11-20 05:53:42
Idea运行单个main方法,不编译整个工程的问题
2021-09-06 09:57:22
![](https://img.aspxhome.com/file/2023/9/79849_0s.jpg)
Android Build类的详解及简单实例
2023-07-02 21:12:45
Java毕业设计实战之在线网盘系统的实现
2023-03-05 05:42:47
![](https://img.aspxhome.com/file/2023/3/72523_0s.jpg)
教你怎么用Idea打包jar包
2023-03-15 03:30:51
![](https://img.aspxhome.com/file/2023/0/68000_0s.png)
Java用 Rhino/Nashorn 代替第三方 JSON 转换库
2023-11-04 02:20:26
Java基于分治算法实现的棋盘覆盖问题示例
2021-07-17 14:05:16
![](https://img.aspxhome.com/file/2023/4/82034_0s.jpg)
Java类的定义以及执行顺序学习教程
2023-01-14 10:44:29
AQS(AbstractQueuedSynchronizer)抽象队列同步器及工作原理解析
2023-02-24 22:20:09
![](https://img.aspxhome.com/file/2023/6/84216_0s.png)
深入了解C#设计模式之订阅发布模式
2021-08-14 05:31:39
聊聊关于Java方法重写的反思
2022-10-26 00:36:12
android工程下不能运行java main程序的解决方法
2023-06-23 21:54:08
SpringMVC结构简介及常用注解汇总
2023-10-25 09:16:59
springboot aop里的@Pointcut()的配置方式
2021-11-18 20:55:37
Java原子操作CAS原理解析
2021-10-20 01:07:05
详解SpringBoot开发使用@ImportResource注解影响拦截器
2021-08-02 13:49:53
关于多线程常用方法以及对锁的控制(详解)
2022-02-02 08:16:42
关于C#连接SQL Server时提示用户登录失败的解决方法
2021-10-03 21:21:58
![](https://img.aspxhome.com/file/2023/1/79071_0s.png)
使用Jenkins来构建SVN+Maven项目的实现
2023-07-30 12:45:22
![](https://img.aspxhome.com/file/2023/0/61840_0s.jpg)