判断当前Event触点是否在指定区域之内的两种方法
2013-08-16 19:31
465 查看
1.根据屏幕绝对坐标
使用示例(对于一个popupwindow,如果触点在其外,则关闭自己:
2.通过一个view对应的长方形最为媒介。
使用示例:
需要记住的是,代码中的childView必须是fatherView的子控件,或者是father背景drawable中一个需要绘画的bitmap元素等这种情况。 fatherView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float x=event.getX();
float y=event.getY();
Rect r = new Rect();
childView.getGlobalVisibleRect(r);
if(isXYInRec(x,y,r){
//TODO
}
return false;
}
});
public static boolean checkDownPointerInView(View view, float x, float y) { int[] location2 = new int[2]; view.getLocationOnScreen(location2); if (x >= location2[0] && x <= location2[0] + view.getWidth() && y >= location2[1] && y <= location2[1] + view.getHeight()) { return true; } return false; }
使用示例(对于一个popupwindow,如果触点在其外,则关闭自己:
public boolean onTouch(View v, MotionEvent event) { int action=event.getAction(); switch(action){ case MotionEvent.ACTION_DOWN: float x=event.getRawX(); float y=event.getRawY(); if(!PositionUtils.checkDownPointerInView(viewMain, x, y)){ mPopupWindow.dismiss(); return true; } } return false; }
2.通过一个view对应的长方形最为媒介。
public boolean isXYInRec(float x,float y,Rect r) { if(r.left <= x&& r.right >= x&&r.top <= y&& r.bottom >= y) { return true; } return false; }
使用示例:
需要记住的是,代码中的childView必须是fatherView的子控件,或者是father背景drawable中一个需要绘画的bitmap元素等这种情况。 fatherView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float x=event.getX();
float y=event.getY();
Rect r = new Rect();
childView.getGlobalVisibleRect(r);
if(isXYInRec(x,y,r){
//TODO
}
return false;
}
});
相关文章推荐
- 判断当前window对象是否为顶级窗口的两种方法
- 两种不同方法判断Excel表格中是否存在指定数值
- mongodb 判断坐标是否在指定多边形区域内的方法
- Android 判断当前线程是否是主线程的两种方法
- Android 判断当前线程是否是主线程的两种方法
- jQuery判断当前点击位置是否非指定元素区域
- Android 判断当前线程是否是主线程的两种方法
- Android编程判断当前指定App是否在前台的方法
- Android 判断当前线程是否是主线程的两种方法
- mongodb 判断坐标是否在指定多边形区域内的方法
- Android判断当前应用程序处于前台还是后台的两种方法
- JAVA 中两种判断输入的是否是数字的方法
- [shell]判断一个字符串是否为数字字符串的两种方法(是否为进程号(PID))
- containsKey方法——判断是否包含指定的键名
- indexOf() 如何判断一个元素在指定数组中是否存在? 找出指定元素出现的所有位置? indexOf()方法 是正序查找,lastIndexOf()是倒叙查找
- python实现判断数组是否包含指定元素的方法
- JavaScript判断数组是否包含指定元素的方法
- 简单的判断当前的浏览器是否在微信中打开的方法
- 两种Android判断view是否部分被遮挡(或者移出屏幕)的方法
- 判断链表是否有环的两种方式和创建有无环的方法