Skip to content Skip to sidebar Skip to footer

How Can I Check The Image Is Touched Using Ontouch()

I am trying to get the x,y co-ordinates on Touch of Image and on that I want to perform some action. So, can anyone tell me how to get the x,y co-ordinates of Image when it is touc

Solution 1:

MoveX - It is the starting x co-ordinate of your view. MoveY - It is the starting y co-ordinate of your view. imgHeiht - It is the Height of the Image; imgWidth - It is the Width of the Image;

event.getX()- It is the actual position of your X co-ordinate.

event.getY()- It is the actual position of your Y co-ordinate.

if(MoveX <= event.getX() && event.getX()<= (MoveX+imgWidth) && (MoveY <= event.getY() && (event.getY() <= MoveY+imgHeight))){
    System.out.println("down...."+event.getX()+" "+event.getY());
    }

This may help you.

Solution 2:

I found another way and I think its a best way to detect touch for any view by using Rect

I just created a method that returns Rect instance including all the four co-ordinates of a view

private Rect getLocationOnScreen(View mView){
        Rect mRect = newRect();
        int[] location = newint[2];

        mView.getLocationOnScreen(location);

        mRect.left = location[0];
        mRect.top = location[1];
        mRect.right = location[0] + mView.getWidth();
        mRect.bottom = location[1] + mView.getHeight();

        return mRect;
    }

And then simply override onTouchEvent(MotionEvent event) in your Activity and check for the co-ordinates of any view that you want to detect is touched or not,

@Override
public boolean onTouchEvent(MotionEvent event) {
     int x = (int) event.getX();
     int y = (int) event.getY();

     if (event.getAction() == MotionEvent.ACTION_DOWN &&
                                      getLocationOnScreen(imageview).contains(x, y)) {
          Toast.makeText(getApplicationContext(), 
                                          "ImageView Touched", Toast.LENGTH_LONG).show();
     }
    return super.onTouchEvent(event);
}

Solution 3:

these are some of the methods you can call from an image view assuming you have this image in it. try experimenting and you me get what you need.

ImageView iv;
iv.setOnTouchListener(new OnTouchListener() {           
@Override
public boolean onTouch(View v, MotionEvent event) {
    event.getX();
    event.getX(pointerIndex)
    event.getXPrecision()
    returntrue;
}
});

Solution 4:

try this..

ImageView.setOnTouchListener(new View.OnTouchListener(){
@Override
    public boolean onTouch(View v, MotionEvent event)
    {
        if(event.getAction()==MotionEvent.ACTION_DOWN)
        {
            float x = event.getX();
            float y = event.getY();
        }
     returntrue;
    }
}

Solution 5:

you cannot direct get the image co-ordinates,when you draw the image ,you can get the image area in the screen,then you can use @CapDroid method to get the image co-ordinates

Post a Comment for "How Can I Check The Image Is Touched Using Ontouch()"