How to find intersect between two ImageView when drag and drop?

孤人 提交于 2019-12-05 09:25:37
interrupt

I assume that you need this functionality for Android. The easiest way is to use intersects method found in Rect class. Link to documentation.

import android.graphics.Rect;

...

Rect rc1 = new Rect();
imageView1.getDrawingRect(rc1);
Rect rc2 = new Rect();
imageView2.getDrawingRect(rc2);
if (Rect.intersects(rc1, rc2)) {
  // intersection is detected
  // here is your method call
}

*EDIT added missing right bracket

This is my Solution that works

private boolean isViewOverlapping(View firstView, View secondView) {

        final int[] location = new int[2];

        firstView.getLocationInWindow(location);
        Rect rect1 = new Rect(location[0], location[1],location[0] + firstView.getWidth(), location[1] + firstView.getHeight());

        secondView.getLocationInWindow(location);
        Rect rect2 = new Rect(location[0], location[1],location[0] + secondView.getWidth(), location[1] + secondView.getHeight());

        return rect1.intersect(rect2);
//        return  (rect1.contains(rect2)) || (rect2.contains(rect1));
    }

call the above function in

@Override
    public boolean onDrag(View v, DragEvent event) {
        switch (event.getAction()) {
case DragEvent.ACTION_DROP:

if(isViewOverlapping(view,child))
                                {Toast.makeText(this,"Overlapping with "+child.getTag() or text ,Toast.LENGTH_SHORT).show();
                                }

Hope this helps

 @override 
 onWindowFocusChanged(boolean focus) 

and put your code in there cz onCreate()/onStart() - this will not work

Kasra Rahjerdi

Do you already have the drag and drop functionality created for the second ImageView?

If so, all you need to do is call getLeft(), getRight(), so on to find the position of your second view after the drag (or on a Handler every few increments while it's being dragged), then this becomes a simple rectangle intersection problem, which already has an answer here.

Late reply, but I also had this problem and fixed it with the following code. The solution for Action drop is answered here, so I don´t post it again. But if You want to detect it WHILE DRAGGING , you have to do that stuff inside DragEvent.ACTION_DRAG_LOCATION.

  1. get x y of current drag event:

    int x_cord = (int) event.getX();
    int y_cord = (int) event.getY();
    
  2. now calculate the size of the rect. For that (in my case) if have to divide the size of the view because the drag position was in the center of it and then calculate the top/left/right/bottom:

    int left = (int) (x_cord - halfViewSize);
    int top = (int) (y_cord - halfViewSize);
    int right = (int) (x_cord + halfVaieSize);
    int bottom = (int) (y_cord + halfViewSize);
    

I casted the value because in my case halfViewSize was a float. If you have an integer, you don´t need to cast.

  1. Now you can build a rect with these values:

    Rect rect = new Rect(left, top, right, bottom);
    
  2. create a method which checks if the rect intersects with a rect of your childViews:

    private boolean collideWithOthers(Rect rect) {
    
        int count = yourParentLayout.getChildCount();
    
        boolean intersects = false;
        for (int i = 0; i < count; i++) {
    
            ImageView squareInside = (ImageView) yourParentLayout.getChildAt(i);
    
            Rect rectInside = squareInside.getCollisionRect();
            if (rectInside.intersect(rect)) {
                Log.d("TAG", "ATTENTION INTERSECT!!!");
                intersects = true;
                break;//stop the loop because an intersection is detected
    
            }
    
        }
    
        return intersects;
    }
    

Now you can do this stuff inside ACTION_DRAG_LOCATION:

    int x_cord = (int) event.getX();
    int y_cord = (int) event.getY();

    int left = (int) (x_cord - halfViewSize);
    int top = (int) (y_cord - halfViewSize);
    int right = (int) (x_cord + halfVaieSize);
    int bottom = (int) (y_cord + halfViewSize);

    Rect rect = new Rect(left, top, right, bottom);
    boolean collide = collideWithOthers(rect);

I hope this will fit to your needs, if not, maybe it´s helpful for others.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!