Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 301
Akarsh M Avatar asked Sep 30 '13 05:09

Akarsh M


2 Answers

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

like image 151
interrupt Avatar answered Oct 21 '22 04:10

interrupt


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

like image 44
Lakhwinder Singh Dhillon Avatar answered Oct 21 '22 04:10

Lakhwinder Singh Dhillon