座標(X、Y)ピクセル(OnTouchEventメソッドとgetX()、getY )がわかっている場合、要素exを見つける方法. ボタンやテキストなど.... X、Yを使用
11075 次
5 に答える
23
getHitRect(outRect)
各子ビューを使用して、ポイントが結果の Rectangle にあるかどうかを確認できます。ここに簡単なサンプルがあります。
for(int _numChildren = getChildCount(); --_numChildren)
{
View _child = getChildAt(_numChildren);
Rect _bounds = new Rect();
_child.getHitRect(_bounds);
if (_bounds.contains(x, y)
// In View = true!!!
}
お役に立てれば、
ファジカルロジック
于 2012-06-09T08:16:27.107 に答える
8
ViewGroup
any を受け入れ、指定された x、y でビューを再帰的に検索する、もう少し完全な回答。
private View findViewAt(ViewGroup viewGroup, int x, int y) {
for(int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
if (child instanceof ViewGroup) {
View foundView = findViewAt((ViewGroup) child, x, y);
if (foundView != null && foundView.isShown()) {
return foundView;
}
} else {
int[] location = new int[2];
child.getLocationOnScreen(location);
Rect rect = new Rect(location[0], location[1], location[0] + child.getWidth(), location[1] + child.getHeight());
if (rect.contains(x, y)) {
return child;
}
}
}
return null;
}
于 2016-03-16T14:04:38.477 に答える
0
Android は、dispatchKeyEvent/dispatchTouchEvent を使用して、キー/タッチ イベントを処理する適切なビューを見つけます。これは複雑な手順です。多くのビューが (x, y) ポイントをカバーしている可能性があるためです。
ただし、(x, y) ポイントをカバーする最も上面のビューを見つけたいだけであれば簡単です。
1 getLocationOnScreen() を使用して絶対位置を取得します。
2 getWidth()、getHeight() を使用して、ビューが (x, y) ポイントをカバーしているかどうかを判断します。
3 ビューツリー全体でビューのレベルを計算します。( getParent() を再帰的に呼び出すか、検索メソッドを使用します)
4 ポイントをカバーし、最大のレベルを持つビューを見つけます。
于 2012-06-09T08:38:29.560 に答える