4

特定のピクセル位置にビューがあります。ピクセルの座標があります。特定の座標でビューの ID を見つける方法は?

4

2 に答える 2

3

あなたがの中にいる場合ViewGroup

int count = viewgroup.getChildCount();
for (int i = 0; i < count; i++)
{
   View view = viewgroup.getChildAt(i);
   if (view.getX() == theX && view.getY() == theY)
          return view.getId()
}

編集 (kcoppock): for ループ内では、次のようにします。

View view = viewgroup.getChildAt(i);
if(!view.getVisibility() == View.VISIBLE) continue;
int[] location = {0, 0};
view.getLocationOnScreen(location);
int right = location[0] + view.getWidth();
int bottom = location[1] + view.getHeight();
Rect bounds = new Rect(location[0], location[1], right, bottom);
if(bounds.contains(coordinatesX, coordinatesY)) return view.getId();
于 2012-07-12T14:47:20.837 に答える
1

私はこのようなものがうまくいくはずだと思います:

  1. ビュー階層をたどって、表示可能な子ビュー (つまり、サブビューを持たないビュー) を見つけます。

  2. ビューで View.getLocationOnScreen() を使用して、ビューの位置 (上部/左側のウィンドウ座標) を取得します。

  3. getMeasuredWidth() / getMeasuredHeight() を使用して、ビューの幅と高さを取得します

  4. ピクセル座標がこの長方形内にあるかどうかを確認します

于 2012-07-12T14:38:23.917 に答える