您现在的位置是:首页 > 文章详情

Android dispatchTouchEvent检测多点触摸事件是否落入在某一个View区域内

日期:2018-04-23点击:358

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> 

原文链接:https://yq.aliyun.com/articles/615680
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章