简体   繁体   English

如何通过坐标 x,y Android 在 View 中查找元素

[英]How to find element in View by coordinates x,y Android

If i know coordinates(X,Y) pixel (by OnTouchEvent method and getX(),getY) how i can find element ex.如果我知道坐标(X,Y)像素(通过 OnTouchEvent 方法和 getX(),getY)我如何找到元素 ex。 button or text etc.... by use X,Y按钮或文本等.... 使用X,Y

You could use getHitRect(outRect) of each child View and check if the point is in the resulting Rectangle.您可以使用每个子视图的getHitRect(outRect)并检查该点是否在结果矩形中。 Here is a quick sample.这是一个快速示例。

for(int _numChildren = getChildCount(); --_numChildren)
{
    View _child = getChildAt(_numChildren);
    Rect _bounds = new Rect();
    _child.getHitRect(_bounds);
    if (_bounds.contains(x, y)
        // In View = true!!!
}

Hope this helps,希望这可以帮助,

FuzzicalLogic模糊逻辑

A slightly more complete answer that accepts any ViewGroup and will recursively search for the view at the given x,y.一个稍微更完整的答案,它接受任何ViewGroup并将递归搜索给定 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;
}

The same solution as https://stackoverflow.com/a/10959466/2557258 but in kotlin:https://stackoverflow.com/a/10959466/2557258相同的解决方案,但在 kotlin 中:

fun ViewGroup.getViewByCoordinates(x: Float, y: Float) : View? {
    (childCount - 1 downTo 0)
        .map { this.getChildAt(it) }
        .forEach {
            val bounds = Rect()
            it.getHitRect(bounds)
            if (bounds.contains(x.toInt(), y.toInt())) {
                return it
            }
        }
    return null
}

Modification of of the answer @Luke provided.修改@Luke 提供的答案。 Difference being using getHitRect rather than getLocationOnScreen .不同之处在于使用getHitRect而不是getLocationOnScreen I found getLocationOnScreen to be inaccurate on what view was being selected.我发现getLocationOnScreen对选择的视图不准确。 Also converted the code to Kotlin and made it an extension on ViewGroup :还将代码转换为 Kotlin 并使其成为ViewGroup的扩展:

/**
 * Find the [View] at the provided [x] and [y] coordinates within the [ViewGroup].
 */
fun ViewGroup.findViewAt(x: Int, y: Int): View? {
    for (i in 0 until childCount) {
        val child = getChildAt(i)

        if (child is ViewGroup) {
            val foundView = child.findViewAt(x, y)
            if (foundView != null && foundView.isShown) {
                return foundView
            }
        } else {
            val rect = Rect()

            child.getHitRect(rect)

            if (rect.contains(x, y)) {
                return child
            }
        }
    }
    return null
}

Android use dispatchKeyEvent/dispatchTouchEvent to find the right view to handle key/touch event, it's a complex procedure. Android 使用 dispatchKeyEvent/dispatchTouchEvent 找到正确的视图来处理按键/触摸事件,这是一个复杂的过程。 Since there could be many views cover the (x, y) point.因为可能有很多视图覆盖 (x, y) 点。

But it's simple if you just want to find the most topside view that cover the (x, y) point.但是,如果您只想找到覆盖 (x, y) 点的最上方视图,这很简单。

1 Use getLocationOnScreen() to get the absoulte position. 1 使用 getLocationOnScreen() 获取绝对位置。

2 Use getWidth(), getHeight() to figure out whether the view cover the (x, y) point. 2 使用 getWidth()、getHeight() 判断视图是否覆盖 (x, y) 点。

3 Cacluate the level of view in whole view tree. 3 计算整个视图树中的视图级别。 (Call getParent() recursively or use some search method) (递归调用 getParent() 或使用某种搜索方法)

4 Find the view that both cover the point and have the biggest level. 4 找到既覆盖点又具有最大层的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM