I have two ImageView
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
?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With