在android中查找具有给定x/y坐标的视图

在android中查找具有给定x/y坐标的视图,android,Android,是否有可能找到以给定绝对x/y像素坐标显示的视图 编辑:我找到了一个非常有效的解决方案: private View findViewByCoord(float x, float y){ TextView textView = null; int[] location = new int[2]; int width = 0; int height = 0; for(int reference : cardReference){ textVie

是否有可能找到以给定绝对x/y像素坐标显示的视图

编辑:我找到了一个非常有效的解决方案:

private View findViewByCoord(float x, float y){
    TextView textView = null;
    int[] location = new int[2];
    int width = 0;
    int height = 0;
    for(int reference : cardReference){
        textView = (TextView) findViewById(reference);
        textView.getLocationOnScreen(location);
        width = textView.getWidth();
        height = textView.getHeight();
        if(location[0] <= x && x <= (location[0] + width) && location[1] <= y && y <= (location[1] + height)){
            Log.i("Test", "Card " + textView.getText() + " is pointed");
            return textView;
        }
    }
    return null;
}

<> >为了加快性能,我会考虑使用一个文本视图数组,然后在每个循环中调用FordView ById()。

< P>一个解决方案是循环遍历父视图的子项并检查<代码> GETFLUTE()/<代码>和<代码> GETOPER()/<代码>坐标对您所选择的X和Y坐标。如果有匹配,你有你的观点

不过我想听听其他的选择

编辑:您还必须计算出视图的高度/宽度,以及与给定的左坐标和顶坐标的关系,以查看您的坐标是否在该范围内

int[] cardReference = new int[]{R.id.card1_1, R.id.card1_2, R.id.card1_3, R.id.card1_4,
                              R.id.card2_1, R.id.card2_2, R.id.card2_3, R.id.card2_4,
                              R.id.card3_1, R.id.card3_2, R.id.card3_3, R.id.card3_4,
                              R.id.card4_1, R.id.card4_2, R.id.card4_3, R.id.card4_4,
                              R.id.card5_1, R.id.card5_2, R.id.card5_3, R.id.card5_4};