android根据坐标获取相应控件,判断点是否在控件上
2015-03-16 11:29
357 查看
本文转自:http://www.2cto.com/kf/201501/370226.html
/**
* 根据坐标获取相对应的子控件<br>
* 在Activity使用
*
* @param x坐标
* @param y坐标
* @return 目标View
*/
public View getViewAtActivity(int x, int y) {
// 从Activity里获取容器
View root = getWindow().getDecorView();
return findViewByXY(root, x, y);
}
/**
* 根据坐标获取相对应的子控件<br>
* 在重写ViewGroup使用
*
* @param x坐标
* @param y坐标
* @return 目标View
*/
public View getViewAtViewGroup(int x, int y) {
return findViewByXY(this, x, y);
}
private View findViewByXY(View view, int x, int y) {
View targetView = null;
if (view instanceof ViewGroup) {
// 父容器,遍历子控件
ViewGroup v = (ViewGroup) view;
for (int i = 0; i < v.getChildCount(); i++) {
targetView = findViewByXY(v.getChildAt(i), x, y);
if (targetView != null) {
break;
}
}
} else {
targetView = getTouchTarget(view, x, y);
}
return targetView;
}
private View getTouchTarget(View view, int x, int y) {
View targetView = null;
// 判断view是否可以聚焦
ArrayList<view> TouchableViews = view.getTouchables();
for (View child : TouchableViews) {
if (isTouchPointInView(child, x, y)) {
targetView = child;
break;
}
}
return targetView;
}
private boolean isTouchPointInView(View view, int x, int y) {
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 (view.isClickable() && y >= top && y <= bottom && x >= left
&& x <= right) {
return true;
}
return false;
}
/**
* 根据坐标获取相对应的子控件<br>
* 在Activity使用
*
* @param x坐标
* @param y坐标
* @return 目标View
*/
public View getViewAtActivity(int x, int y) {
// 从Activity里获取容器
View root = getWindow().getDecorView();
return findViewByXY(root, x, y);
}
/**
* 根据坐标获取相对应的子控件<br>
* 在重写ViewGroup使用
*
* @param x坐标
* @param y坐标
* @return 目标View
*/
public View getViewAtViewGroup(int x, int y) {
return findViewByXY(this, x, y);
}
private View findViewByXY(View view, int x, int y) {
View targetView = null;
if (view instanceof ViewGroup) {
// 父容器,遍历子控件
ViewGroup v = (ViewGroup) view;
for (int i = 0; i < v.getChildCount(); i++) {
targetView = findViewByXY(v.getChildAt(i), x, y);
if (targetView != null) {
break;
}
}
} else {
targetView = getTouchTarget(view, x, y);
}
return targetView;
}
private View getTouchTarget(View view, int x, int y) {
View targetView = null;
// 判断view是否可以聚焦
ArrayList<view> TouchableViews = view.getTouchables();
for (View child : TouchableViews) {
if (isTouchPointInView(child, x, y)) {
targetView = child;
break;
}
}
return targetView;
}
private boolean isTouchPointInView(View view, int x, int y) {
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 (view.isClickable() && y >= top && y <= bottom && x >= left
&& x <= right) {
return true;
}
return false;
}
相关文章推荐
- Android根据坐标判断是否在控件内
- android 根据apk文件获取包名等信息 已经判断 指定 这个apk 是否安装过
- Android 根据坐标获取控件方法
- 精确获取屏幕尺寸 判断是否是平板 文字根据状态更改颜色 android:textColor 背景色根据状态更改颜色 android:backgroup 启动APK的默认Activit
- Android 根据坐标获取控件方法
- android 获取路径目录方法以及判断目录是否存在,创建目录
- Android高手进阶教程(十一)之----Android 通用获取Ip的方法(判断手机是否联网的方法)!!!
- Android获取SD卡上的APK文件,并且判断是否安装过、是否可以更新到新版本的工具类
- Android 之实用小代码-获取屏幕尺寸、判断是否是平板、文字颜色、点击效果、计算字宽等。
- 转:Android 通用获取Ip的方法(判断手机是否联网的方法)
- android 获取路径目录方法以及判断目录是否存在,创建目录
- android 获取控件位置坐标,屏幕尺寸,标题栏,状态栏高度
- 根据指定的控件ID获取相应的控件
- android 获取路径目录方法以及判断目录是否存在,创建目录 (转)
- Android获取SD卡上的APK文件,并且判断是否安装过、是否可以更新到新版本的工具类
- Android通过获取Ip的方法判断手机是否联网
- Android 通用获取Ip的方法(判断手机是否联网的方法)!!!
- Android获取SD卡上的APK文件,并且判断是否安装过、是否可以更新到新版本的工具类
- Android 通用获取Ip的方法(判断手机是否联网的方法)!!!
- 根据文件获取文件夹,判断文件夹是否存在,不存在则创建