6

I have two ImageView

  1. One is fixed at a position
  2. On Second I need to apply drag an drop functionality.

What I need:

Drag second ImageView to the another ImageView and when they intersect than I need to call a method.

EDIT 1:

Intersect b/w two imageView is DONE but on the drag n drop it's not happening.

Edit 2 :

How can I achieve this thing on Drag and drop?

Akarsh M
  • 1,629
  • 2
  • 24
  • 47

5 Answers5

10

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

tomassilny
  • 1,121
  • 1
  • 10
  • 14
interrupt
  • 2,030
  • 17
  • 14
  • doesn't get the intersect – Akarsh M Sep 30 '13 at 05:48
  • see the EDIT 1 :else part is printed – Akarsh M Sep 30 '13 at 05:55
  • Unfortunately your code does not help in assisting you. Since I'm pretty confident that Rect.intersects works as expected - you need to troubleshoot your code. I would start with printing myViewRect and otherViewRect1 to output log to validate rectangles data. – interrupt Sep 30 '13 at 08:08
  • in above edit two imageView is in layout both overlap each other and this code. Did I mistake somewhere – Akarsh M Sep 30 '13 at 08:58
5

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

2
 @override 
 onWindowFocusChanged(boolean focus) 

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

1

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.

Community
  • 1
  • 1
Kasra Rahjerdi
  • 2,483
  • 26
  • 42
0

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.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49