Android 根据坐标获取控件方法

http://www.2cto.com/kf/201501/370226.html

http://www.2cto.com/kf/201501/370226.html

http://www.2cto.com/kf/201501/370226.html

http://www.2cto.com/kf/201501/370226.html


Android 根据坐标获取控件方法
2015-01-15       0  个评论    来源:陈英有  
收藏     我要投稿
id="iframeu2597680_0" src="http://pos.baidu.com/lcbm?sz=650x180&rdid=2597680&dc=2&di=u2597680&dri=0&dis=0&dai=4&ps=329x96&coa=at%3D3%26rsi0%3D650%26rsi1%3D180%26pat%3D1%26tn%3DbaiduCustNativeAD%26rss1%3D%2523F9F9F9%26conBW%3D0%26adp%3D1%26ptt%3D0%26titFF%3D%2525E5%2525BE%2525AE%2525E8%2525BD%2525AF%2525E9%25259B%252585%2525E9%2525BB%252591%26titFS%3D14%26rss2%3D%2523000000%26titSU%3D0%26tft%3D0%26tlt%3D1%26ptbg%3D90%26piw%3D140%26pih%3D90%26ptp%3D1&dcb=BAIDU_SSP_define&dtm=HTML_POST&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1475034420591&ti=Android%20%E6%A0%B9%E6%8D%AE%E5%9D%90%E6%A0%87%E8%8E%B7%E5%8F%96%E6%8E%A7%E4%BB%B6%E6%96%B9%E6%B3%95%20-%20Android%E7%A7%BB%E5%8A%A8%E5%BC%80%E5%8F%91%E6%8A%80%E6%9C%AF%E6%96%87%E7%AB%A0_%E6%89%8B%E6%9C%BA%E5%BC%80%E5%8F%91%20-%20%E7%BA%A2%E9%BB%91%E8%81%94%E7%9B%9F&ari=2&dbv=2&drs=1&pcs=1153x587&pss=1153x340&cfv=0&cpl=5&chi=1&cce=true&cec=GBK&tlm=1473529717&rw=587&ltu=http%3A%2F%2Fwww.2cto.com%2Fkf%2F201501%2F370226.html&ltr=http%3A%2F%2Fwww.2cto.com%2Fkf%2F201501%2F370226.html&ecd=1&psr=1280x800&par=1280x705&pis=-1x-1&ccd=24&cja=false&cmi=7&col=zh-CN&cdo=-1&tcn=1475034422&qn=b34d350ff5a95cdb&tt=1475034419816.1906.2300.2301" width="650" height="180" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="border-width: 0px; border-style: initial; vertical-align: bottom; margin: 0px;">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
      * 根据坐标获取相对应的子控件<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 ;
     }
</view>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值