Search code examples
androidimageviewdraggableregion

If ImageView is in Region, do something


At the moment I have Regions set up in my activity for touches, like this:

onTouch(...){
    int x = (int) ev.getX();
    int y = (int) ev.getY();

   if (x >= 370 && x <= 500 && y >= 250 && y <= 420) {
            //do something....
       }

The above code is in an onTouch method to detect if a touch passes through the Region.

Now I'm trying to get an image to follow the finger around the screen while still being able to interact with the Regions. I have an ImageView that I can drag around the screen, using the onDrag method, however when I drag it through the Region nothing happens.

I think something like this might work but I'm not sure how to actually code it:

    if ImageView (being dragged by finger) is in the area of 
         (x >= 370 && x <= 500 && y >= 250 && y <= 420) {
            //do something...
        }

Solution

  • Actually figured it out:

     ImageView j = (ImageView) findViewById(R.id.image);
     final int X = (int) event.getRawX();
     final int Y = (int) event.getRawY();
    
       if (X >= 370 && X <= 500 && Y >= 250 && Y <= 420) {
                j.//do whatever...
          }