Search code examples
androidandroid-imageviewandroid-imageontouchlistenerandroid-gesture

How to know if two images are intersect while one image moving in android?


In my application, i am moving image around the screen by using onTouchListener.

I have another two images in the same view. My problem is that, when the moving image, touches any of the other images, i need to perform a certain action (that means if images are intersected, then do something).

How can this be achieved?.Please help me asap

Thanks in Advance.


Solution

  • You should be able to use Rect.intersects(Rect, Rect), like this example:

    Rect myViewRect = new Rect();
    myView.getHitRect(myViewRect);
    
    Rect otherViewRect1 = new Rect();
    otherView1.getHitRect(otherViewRect1);
    
    Rect otherViewRect2 = new Rect();
    otherView2.getHitRect(otherViewRect2);
    
    if (Rect.intersects(myViewRect, otherViewRect1)) {
      // Intersects otherView1
    }
    
    if (Rect.intersects(myViewRect, otherViewRect2)) {
      // Intersects otherView2
    } 
    

    Reference is here.