特定のピクセル位置にビューがあります。ピクセルの座標があります。特定の座標でビューの ID を見つける方法は?
質問する
244 次
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
私はこのようなものがうまくいくはずだと思います:
ビュー階層をたどって、表示可能な子ビュー (つまり、サブビューを持たないビュー) を見つけます。
ビューで View.getLocationOnScreen() を使用して、ビューの位置 (上部/左側のウィンドウ座標) を取得します。
getMeasuredWidth() / getMeasuredHeight() を使用して、ビューの幅と高さを取得します
ピクセル座標がこの長方形内にあるかどうかを確認します
于 2012-07-12T14:38:23.917 に答える