Dmitry  Ushkevich
Dmitry Ushkevich

Reputation: 392

Get X/Y coordinates on ImageView touch

So, I need to get coordinates of my touch on ImageView.

I have already successfully tried to get coordinates in OnTouchListener event. But it is not what i really need.

The problem is that the resolution of the screen is much lower than the resolution of the picture in ImageView.

How to get actual coordinates (maybe certain pixels) of the image?

Upvotes: 1

Views: 2194

Answers (2)

Suhail Purkar
Suhail Purkar

Reputation: 104

Android: How do I get the x y coordinates within an image / ImageView?

imageView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            int[] values = new int[2]; 
            view.getLocationOnScreen(values);
            Log.d("X & Y",values[0]+" "+values[1]);
        }
    });

Something like this?

Upvotes: 2

Fatih Santalu
Fatih Santalu

Reputation: 4701

There's getLocationOnScreen() and getLocationInWindow() methods for the purpose.

imageView.setOnTouchListener(new OnTouchListener() {
            @Override public boolean onTouch(View view, MotionEvent motionEvent) {
                int[] locations = new int[2];
                view.getLocationOnScreen(locations);
                //view.getLocationInWindow(locations);
                return false;
            }
        });

Upvotes: 2

Related Questions