Lalit Poptani
Lalit Poptani

Reputation: 67286

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 touched. Thanks In Advance.

My Code -

public class MovableObject extends ImageView implements OnTouchListener{

    Bitmap myBmp;
    Paint myPaint = new Paint();
    int MoveX = 0;

    public MovableObject(Context context,int moveObject,Bitmap myBmp) {
        super(context);
        super.setClickable(true);
        this.myBmp = myBmp;
        myPaint.setColor(Color.WHITE);
        this.MoveX = moveObject;
        setOnTouchListener(this);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(myBmp, MoveX, 100, myPaint);
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction() & MotionEvent.ACTION_MASK) {

            case MotionEvent.ACTION_DOWN:
                System.out.println("down...."+event.getX()+" "+event.getY());
            case MotionEvent.ACTION_MOVE:
        }
        return true;
    }
}

By this I am getting the x,y co-ordinates where I click but I want to get the x,y when I click on my Image.

Upvotes: 3

Views: 3870

Answers (5)

Lalit Poptani
Lalit Poptani

Reputation: 67286

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 = new Rect();
        int[] location = new int[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);
}

Upvotes: 2

Siten
Siten

Reputation: 4533

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.

Upvotes: 6

pengwang
pengwang

Reputation: 19956

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

Upvotes: 0

Niranj Patel
Niranj Patel

Reputation: 33238

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();
        }
     return true;
    }
}

Upvotes: 0

Samuel
Samuel

Reputation: 9993

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()
    return true;
}
});

Upvotes: 0

Related Questions