3

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.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242

5 Answers5

6

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.

Community
  • 1
  • 1
Siten
  • 4,515
  • 9
  • 39
  • 64
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 = 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);
}
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
0

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;
}
});
Samuel
  • 9,883
  • 5
  • 45
  • 57
0

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;
    }
}
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • I had done this but I want to get the x,y co-ordinates when the Image is Touched. – Lalit Poptani Aug 04 '11 at 09:38
  • i get it, but as for me i don't know if there is any direct methods. but you sure can calculate the values relative to the images (top,left)(x,y) to the screens origin. sure its worth if we have a direct method. – Samuel Aug 04 '11 at 09:41
  • @CapDroid requirement is about to get the position of some view. this will give only xy co-ordinate of the screen. – Siten Aug 04 '11 at 10:46
  • it will given position of that view which had you applied onTouch Method – Niranj Patel Aug 04 '11 at 10:48
0

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

pengwang
  • 19,536
  • 34
  • 119
  • 168