user2366466
user2366466

Reputation: 21

How to find point on the image in a imageview when it is touched

I have a image in a imageview and I am creating a Monodroid application which needs to display swipe password pattern screen just like android lock screen.

I have 9 images placed in a 3x3 matrix and I have overridden the OnTouchEvent in the main activity. When I touch the screen I need to know whether the touched point is on the image or on the main layout other than images.

In iOS, we can get image bounds so that we can verify if the point is inside the bounds. I'm not able to find a suitable method to ensure the touched point is on the image.

Need help on this.

Upvotes: 2

Views: 943

Answers (1)

wlodek
wlodek

Reputation: 11

I think this is the right solution:

//any image view
//must be assigned before use by findViewByID(int id)
ImageView image_view = null;
@Override
public boolean onTouchEvent(MotionEvent event) {
    //boundary of your image view
    Rect rect = new Rect(image_view.getLeft(),image_view.getTop(),image_view.getRight(),image_view.getBottom());
    //if point is inside rect then test_collision = true, else test_collision = false
    boolean test_collision = rect.contains((int)event.getX(), (int)event.getY());
    return super.onTouchEvent(event);
}

Upvotes: 1

Related Questions