Android activity和view判断滑动
作者:lqh 时间:2021-09-18 17:25:17
Android activity和view判断滑动
实例代码:
//手指按下的点为(x1, y1)手指离开屏幕的点为(x2, y2)
float x1 = 0;
float x2 = 0;
float y1 = 0;
float y2 = 0;
@Override
public boolean onTouchEvent(MotionEvent event) {
//继承了Activity的onTouchEvent方法,直接监听点击事件
if(event.getAction() == MotionEvent.ACTION_DOWN) {
//当手指按下的时候
x1 = event.getX();
y1 = event.getY();
}
if(event.getAction() == MotionEvent.ACTION_UP) {
//当手指离开的时候
x2 = event.getX();
y2 = event.getY();
if(y1 - y2 > 50) {
Toast.makeText(MainActivity.this, "向上滑", Toast.LENGTH_SHORT).show();
} else if(y2 - y1 > 50) {
Toast.makeText(MainActivity.this, "向下滑", Toast.LENGTH_SHORT).show();
} else if(x1 - x2 > 50) {
Toast.makeText(MainActivity.this, "向左滑", Toast.LENGTH_SHORT).show();
} else if(x2 - x1 > 50) {
Toast.makeText(MainActivity.this, "向右滑", Toast.LENGTH_SHORT).show();
}
}
return super.onTouchEvent(event);
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
标签:Android,activity,view,滑动
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android操作系统的架构设计分析
2022-07-12 11:02:26
![](https://img.aspxhome.com/file/2023/9/96799_0s.jpg)
C#操作 JSON方法汇总
2023-01-23 06:49:59
Mybatis配置之<typeAliases>别名配置元素解析
2023-08-02 03:09:54
![](https://img.aspxhome.com/file/2023/7/60447_0s.png)
javaWeb项目部署到阿里云服务器步骤详解
2023-11-07 05:21:36
![](https://img.aspxhome.com/file/2023/9/58909_0s.png)
Java的split方法使用详解
2021-10-03 06:09:57
![](https://img.aspxhome.com/file/2023/3/64523_0s.png)
Andriod arcgis保存Mapview为图片的实例代码
2022-08-15 01:23:29
C#数据类型实现背包、队列和栈
2022-10-07 06:42:09
![](https://img.aspxhome.com/file/2023/6/113726_0s.jpg)
自定义spring mvc的json视图实现思路解析
2023-03-12 12:04:44
c#获取两个特定字符之间的内容并输出的方法
2021-12-02 19:47:11
SpringMVC之AbstractAnnotationConfigDispatcherSer解读
2023-06-21 21:42:32
![](https://img.aspxhome.com/file/2023/4/122254_0s.png)
android开发仿ios的UIScrollView实例代码
2023-08-07 01:55:20
C#/VB.NET中从 PDF 文档中提取所有表格
2023-04-07 09:11:03
![](https://img.aspxhome.com/file/2023/7/94557_0s.png)
hutool实战:IoUtil 流操作工具类(将内容写到流中)
2022-11-16 09:17:47
![](https://img.aspxhome.com/file/2023/6/83086_0s.png)
Maven管理SpringBoot Profile详解
2022-03-20 09:57:45
java中的文件操作总结(干货)
2023-11-08 22:24:29
初步编写IDEA\\AndroidStudio翻译插件的方法
2022-12-14 19:30:30
![](https://img.aspxhome.com/file/2023/9/96029_0s.png)
C# Windows API应用之基于FlashWindowEx实现窗口闪烁的方法
2023-06-30 12:55:22
springboot远程debug调试全过程
2023-11-25 07:05:56
![](https://img.aspxhome.com/file/2023/7/60247_0s.png)
详解如何在SpringBoot中自定义参数解析器
2023-07-24 16:06:51
Java经典面试题最全汇总208道(五)
2023-11-10 07:06:46