Android 根据坐标获取控件方法

来源:互联网 发布:管理wifi软件 编辑:程序博客网 时间:2024/05/14 22:40

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 个评论    来源:陈英有  
收藏    我要投稿
<iframe id="iframeu2597680_0" src="http://pos.baidu.com/lcbm?sz=650x180&amp;rdid=2597680&amp;dc=2&amp;di=u2597680&amp;dri=0&amp;dis=0&amp;dai=4&amp;ps=329x96&amp;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&amp;dcb=BAIDU_SSP_define&amp;dtm=HTML_POST&amp;dvi=0.0&amp;dci=-1&amp;dpt=none&amp;tsr=0&amp;tpr=1475034420591&amp;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&amp;ari=2&amp;dbv=2&amp;drs=1&amp;pcs=1153x587&amp;pss=1153x340&amp;cfv=0&amp;cpl=5&amp;chi=1&amp;cce=true&amp;cec=GBK&amp;tlm=1473529717&amp;rw=587&amp;ltu=http%3A%2F%2Fwww.2cto.com%2Fkf%2F201501%2F370226.html&amp;ltr=http%3A%2F%2Fwww.2cto.com%2Fkf%2F201501%2F370226.html&amp;ecd=1&amp;psr=1280x800&amp;par=1280x705&amp;pis=-1x-1&amp;ccd=24&amp;cja=false&amp;cmi=7&amp;col=zh-CN&amp;cdo=-1&amp;tcn=1475034422&amp;qn=b34d350ff5a95cdb&amp;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;"></iframe>
?
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
     */
    publicView getViewAtActivity(intx, inty) {
        // 从Activity里获取容器
        View root = getWindow().getDecorView();
        returnfindViewByXY(root, x, y);
    }
 
    /**
     * 根据坐标获取相对应的子控件<br>
     * 在重写ViewGroup使用
     *
     * @param x坐标
     * @param y坐标
     * @return 目标View
     */
    publicView getViewAtViewGroup(intx, inty) {
        returnfindViewByXY(this, x, y);
    }
 
    privateView findViewByXY(View view, intx, inty) {
        View targetView = null;
        if(view instanceofViewGroup) {
            // 父容器,遍历子控件
            ViewGroup v = (ViewGroup) view;
            for(inti = 0; i < v.getChildCount(); i++) {
                targetView = findViewByXY(v.getChildAt(i), x, y);
                if(targetView != null) {
                    break;
                }
            }
        }else{
            targetView = getTouchTarget(view, x, y);
        }
        returntargetView;
 
    }
 
    privateView getTouchTarget(View view, intx, inty) {
        View targetView = null;
        // 判断view是否可以聚焦
        ArrayList<view> TouchableViews = view.getTouchables();
        for(View child : TouchableViews) {
            if(isTouchPointInView(child, x, y)) {
                targetView = child;
                break;
            }
        }
        returntargetView;
    }
 
    privateboolean isTouchPointInView(View view, intx, inty) {
        int[] location = newint[2];
        view.getLocationOnScreen(location);
        intleft = location[0];
        inttop = location[1];
        intright = left + view.getMeasuredWidth();
        intbottom = top + view.getMeasuredHeight();
        if(view.isClickable() && y >= top && y <= bottom && x >= left
                && x <= right) {
            returntrue;
        }
        returnfalse;
    }
</view>


0 0