Android dispatchTouchEvent检测多点触摸事件是否落入在某一个View区域内
Android dispatchTouchEvent检测多点触摸事件是否落入在某一个View区域内
检测发生在Android设备屏幕上到的点击事件是否发生在某一个View的区域内,重点方法是touchEventInView,该方法进行具体的检测,如果返回true,则表明该事件是发生在当前的参数view范围内,返回false则不是。
利用dispatchTouchEvent传出发生在全部区域上的点击事件,然后用touchEventInView计算判断。
dispatchTouchEvent返回true,则表明该事件消费掉了,不需要再次进行事件传递。换句话说,如果dispatchTouchEvent返回true,相当于对触摸事件进行了拦截处理。如果不想拦截不做任何处理,切忌不要简单粗暴的返回false,返回false导致意想不到的结果,而是要在dispatchTouchEvent里面利用super进行返回:
return super.dispatchTouchEvent(event);
例:
package zhangphil.test;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class ViewActivity extends AppCompatActivity {
private TextView text1;
private TextView text2;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_activity);
text1 = findViewById(R.id.text1);
text2 = findViewById(R.id.text2);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
/**
* 可能点击的事件不止一个点,多点触摸,比如两个以上手指同时触摸屏幕。
*
*/
for (int i = 0; i < event.getPointerCount(); i++) {
float x = event.getX(i);
float y = event.getY(i);
if (touchEventInView(text1, x, y)) {
Toast.makeText(this, "text1", Toast.LENGTH_SHORT).show();
}
if (touchEventInView(text2, x, y)) {
Toast.makeText(this, "text2", Toast.LENGTH_SHORT).show();
}
}
return super.dispatchTouchEvent(event);
}
/**
* 该方法检测一个点击事件是否落入在一个View内,换句话说,检测这个点击事件是否发生在该View上。
*
* @param view
* @param x
* @param y
* @return
*/
private boolean touchEventInView(View view, float x, float y) {
if (view == null) {
return false;
}
int[] location = new int[2];
view.getLocationOnScreen(location);
int left = location[0];
int top = location[1];
int right = left + view.getMeasuredWidth();
int bottom = top + view.getMeasuredHeight();
if (y >= top && y <= bottom && x >= left && x <= right) {
return true;
}
return false;
}
}
相应的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal">
<TextView
android:id="@+id/text1"
android:layout_width="60dp"
android:layout_height="50dp"
android:background="@android:color/holo_red_light"
android:text="1"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/text2"
android:layout_width="60dp"
android:layout_height="50dp"
android:background="@android:color/holo_orange_light"
android:text="2"
android:textColor="@android:color/white" />
</LinearLayout>
</RelativeLayout>

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
iOS开发之XLForm的使用
在iOS开发中,开发"表单"界面,字段稍微多一点的一般都用UITableView来做,而XLForm就是这样一个框架,它是创建动态表格视图最牛逼的iOS库, 用它实现表单功能,非常简单,省心省力。但是很可惜,搜索了很多文章都只是翻译官方文档,很多人在使用该库的时候可能都被官方文档带走远了,不知道如何具体使用。正好最近也要用到这个库,所以写个入门使用文章供大家参考。 一、 导入项目 使用CocoaPods或者手动导入库文件,本人选择直接导入项目源文件的方式。 导入项目.png 二、改造表单ViewController 让ViewController继承自XLFormViewController,并重写下面的两个方法 @interface OneViewController : XLFormViewController @end @implementation OneViewController - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self ...
-
下一篇
Android两条并排RecyclerView实时联动滑动增强
Android两条并排RecyclerView实时联动滑动增强 在附录1中,我初步实现了两条垂直摆放的RecyclerView的实时联动滚动,但是附录文章1的实现存在一定问题,比如当用户的手指同时在屏幕的两条RecyclerView区域内滑动,将发生异常。还有就是,比如在RecyclerView1正在滚动时候,用户又在RecyclerView2的区域内手指滑动,这样将触发循环嵌套滚动事件,引发错误,因此本文在附录1的基础上改进,通过拦截处理系统的触摸事件解决造成RecyclerView滚动引发的问题。 代码: package zhangphil.test; import android.graphics.Color; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.support.v7...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- MySQL8.0.19开启GTID主从同步CentOS8
- CentOS7,8上快速安装Gitea,搭建Git服务器
- MySQL数据库在高并发下的优化方案
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- CentOS8编译安装MySQL8.0.19
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- SpringBoot2整合Redis,开启缓存,提高访问速度
- Docker使用Oracle官方镜像安装(12C,18C,19C)