Android GestureDetector实现手势滑动效果
作者:Vivinia_Vivinia 时间:2023-10-01 04:37:50
本文实例为大家分享了Android GestureDetector实现手势滑动的具体代码,供大家参考,具体内容如下
目标效果:
程序运行,手指在屏幕上从左往右或者从右往左滑动超过一定距离,就会吐司输出滑动方向和距离。
1.activity_main.xml页面放置一个ImageView控件。
activity_main.xml页面:
<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"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/ivShow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
2.MainActivity.java页面实现滑动方法。
MainActivity.java页面:
package com.example.gesturedetector;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.GestureDetector;
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.Toast;
public class MainActivity extends Activity {
private ImageView ivShow;
private GestureDetector gestureDetector;
class myGestureListener extends SimpleOnGestureListener{
@Override
/*识别滑动,第一个参数为刚开始事件,第二个参数为结束事件*/
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if(e1.getX()-e2.getX()>50){
Toast.makeText(MainActivity.this,"从右往左滑动"+(e1.getX()-e2.getX()),Toast.LENGTH_LONG).show();
}else if(e2.getX()-e1.getX()>50){
Toast.makeText(MainActivity.this,"从左往右滑动"+(e2.getX()-e1.getX()),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);
gestureDetector=new GestureDetector(MainActivity.this,new myGestureListener());
ivShow=(ImageView) findViewById(R.id.ivShow);
ivShow.setLongClickable(true); //view必须设置为true,否则手势识别无法正确工作
ivShow.setOnTouchListener(new OnTouchListener() {
/*可以捕获到触摸屏幕发生的Event事件*/
@Override
public boolean onTouch(View arg0, MotionEvent event) {
gestureDetector.onTouchEvent(event);//转发
return false;
}
});
}
}
3.程序较简单,运行就可以显示目标效果了。
来源:https://blog.csdn.net/hester_hester/article/details/51354728
标签:Android,GestureDetector,手势滑动
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Unity键盘WASD实现物体移动
2021-06-14 13:46:21
![](https://img.aspxhome.com/file/2023/3/71073_0s.jpg)
C#中常用的正则表达式实例
2021-05-27 04:39:12
![](https://img.aspxhome.com/file/2023/4/80034_0s.png)
Java 添加、替换、删除PDF中的图片的示例代码
2023-08-28 09:06:26
![](https://img.aspxhome.com/file/2023/7/71637_0s.jpg)
C#调用动态库
2021-07-30 03:46:25
![](https://img.aspxhome.com/file/2023/6/88336_0s.png)
Java 17的一些新特性介绍
2022-04-11 15:33:15
如何基于LoadingCache实现Java本地缓存
2023-04-02 00:14:55
Java 根据某个 key 加锁的实现方式
2023-10-27 07:54:20
详解如何在Spring Security中自定义权限表达式
2023-11-08 05:11:24
![](https://img.aspxhome.com/file/2023/6/58676_0s.jpg)
Java面试synchronized偏向锁后hashcode存址
2023-08-09 09:15:06
![](https://img.aspxhome.com/file/2023/0/57920_0s.png)
Android12 蓝牙适配的实现步骤
2021-08-25 08:36:34
![](https://img.aspxhome.com/file/2023/4/119094_0s.gif)
android 使用kotlin 实现点击更换全局语言(中日英切换)
2023-11-18 03:08:50
![](https://img.aspxhome.com/file/2023/3/138523_0s.png)
详解Java中的ThreadLocal
2022-08-19 17:48:43
![](https://img.aspxhome.com/file/2023/0/99890_0s.png)
c#模拟js escape方法的简单实例
2023-01-26 07:29:24
ubuntu用wifi连接android调试程序的步骤
2021-08-05 16:40:27
Android实现简易计步器功能隔天步数清零查看历史运动纪录
2021-07-24 00:37:02
![](https://img.aspxhome.com/file/2023/1/122241_0s.jpg)
Java实现选择排序
2021-06-30 16:53:55
![](https://img.aspxhome.com/file/2023/2/84602_0s.jpg)
Java编程实现A*算法完整代码
2022-06-18 12:28:57
![](https://img.aspxhome.com/file/2023/1/72591_0s.png)
Flutter集成高德地图并添加自定义Maker的实践
2022-07-11 21:32:49
![](https://img.aspxhome.com/file/2023/4/123234_0s.jpg)
Java找不到或无法加载主类及编码错误问题的解决方案
2021-08-01 03:43:10
JAVA面试题之Forward与Redirect的区别详解
2023-11-25 02:27:15
![](https://img.aspxhome.com/file/2023/2/59912_0s.png)