检测视图是否重叠

sma*_*133 15 android view

我在另一个尺寸的屏幕上绘制视图时遇到问题!我需要有两个View类型参数的方法.如果第一个视图在第二个视图上重叠,则返回true,而在另一个视图中则返回false

在此输入图像描述

在此输入图像描述

小智 24

您还可以使用Rect.intersect()来查找重叠视图.

    int[] firstPosition = new int[2];
    int[] secondPosition = new int[2];

    firstView.getLocationOnScreen(firstPosition);
    secondView.getLocationOnScreen(secondPosition);

    // Rect constructor parameters: left, top, right, bottom
    Rect rectFirstView = new Rect(firstPosition[0], firstPosition[1],
            firstPosition[0] + firstView.getMeasuredWidth(), firstPosition[1] + firstView.getMeasuredHeight());
    Rect rectSecondView = new Rect(secondPosition[0], secondPosition[1],
            secondPosition[0] + secondView.getMeasuredWidth(), secondPosition[1] + secondView.getMeasuredHeight());
    return rectFirstView.intersect(rectSecondView);
Run Code Online (Sandbox Code Playgroud)


sma*_*133 22

Berserk谢谢你的帮助!经过一些实验,我写了一个方法,检测视图是否与我的情况重叠!

private boolean isViewOverlapping(View firstView, View secondView) {
        int[] firstPosition = new int[2];
        int[] secondPosition = new int[2];

        firstView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        firstView.getLocationOnScreen(firstPosition);
        secondView.getLocationOnScreen(secondPosition);

        int r = firstView.getMeasuredWidth() + firstPosition[0];
        int l = secondPosition[0];
        return r >= l && (r != 0 && l != 0);
    }
Run Code Online (Sandbox Code Playgroud)


Aba*_*art 6

这类似于Marcel Derks的答案,但无需额外导入即可编写。它使用Rect.intersect无需创建Rect对象即可形成的基本代码。

private boolean isViewOverlapping(View firstView, View secondView) {
    int[] firstPosition = new int[2];
    int[] secondPosition = new int[2];

    firstView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    firstView.getLocationOnScreen(firstPosition);
    secondView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    secondView.getLocationOnScreen(secondPosition);

    return firstPosition[0] < secondPosition[0] + secondView.getMeasuredWidth()
            && firstPosition[0] + firstView.getMeasuredWidth() > secondPosition[0]
            && firstPosition[1] < secondPosition[1] + secondView.getMeasuredHeight()
            && firstPosition[1] + firstView.getMeasuredHeight() > secondPosition[1];
}
Run Code Online (Sandbox Code Playgroud)

您不需要强制进行视图测量,但这样做是为了很好的测量;)


Gre*_*ory 6

在 Kotlin 中,您可以使用 View 类扩展。结果是一个更紧凑的解决方案:

// Returns true if the view overlaps another view.
// Additionally checks whether another view will overlap if it is shifted to delta values.
fun View.isOverlap(other: View, deltaX: Int = 0, deltaY: Int = 0): Boolean {
    val thisXY  = IntArray(2).apply { getLocationOnScreen(this) }
    val otherXY = IntArray(2).apply {
        other.getLocationOnScreen(this)
        this[0] += deltaX
        this[1] += deltaY
    }
    return thisXY.let { Rect(it[0], it[1], it[0] + width, it[1] + height) }
        .intersect(otherXY.let { 
            Rect(it[0], it[1], it[0] + other.width, it[1] + other.height)
         })
}

// usage example:
if (myView.isOverlap(otherView)) {
    // do something ...
}
Run Code Online (Sandbox Code Playgroud)