detect if views are overlapping

前端 未结 6 1390
名媛妹妹
名媛妹妹 2020-12-13 14:59

I have problem with drawing views on another size screens! I need method which has two parameters of View type. And return true if first view overlapping on second view, an

相关标签:
6条回答
  • 2020-12-13 15:20

    This is similar to the answer from Marcel Derks, but was written without the need for an additional import. It uses the basic code that forms Rect.intersect without creating the Rect objects.

    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];
    }
    

    You are not required to force a view measurement, but it is done for good measure ;)

    0 讨论(0)
  • 2020-12-13 15:21

    Seems like you are asking for code to your problem. I am posting the logic which I think may work:

    1. Create a funtion which will take two views as parameters, and returns a boolean.
    2. Now check the location of both views on the screen using this. It will give you the idea whether they are overlapping or not.
    3. Return true or false according to it.
    0 讨论(0)
  • 2020-12-13 15:23

    In Kotlin, you can use the View class extension. It turns out a more compact solution:

    // 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 ...
    }
    
    0 讨论(0)
  • 2020-12-13 15:27

    Berserk thanks you for help! After some experiments I wrote method which detect view is overlapped or not for my case!

    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);
        }
    
    0 讨论(0)
  • 2020-12-13 15:27

    To help you in a VERY simple way. I will give a logic code which will undoubtedly detect two colliding views, even if they are moving. This can work for Bitmaps, ImageViews, and just about anything you can think of. Here it is and this is all:

    //LET'S SAY WE DETECT IF PLAYER COLLIDES WITH ENEMY
    if((Math.abs(yourviewid.getY() - yoursecondviewid.getY()) < 75) 
         && (Math.abs(yourviewid.getX() - yoursecondviewid.getX()) < 75)){
        //DO YOUR STUFF HERE
    }
    
    0 讨论(0)
  • 2020-12-13 15:29

    You can also use Rect.intersect() to find overlapping views.

        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);
    
    0 讨论(0)
提交回复
热议问题