praveenb
praveenb

Reputation: 10659

how to know the coordinates of image when touch

How to get image coordinates at mouse hover position.

Please let me know

Thank you

Upvotes: 1

Views: 2204

Answers (4)

Naresh
Naresh

Reputation: 305

Try this

http://developer.android.com/reference/android/view/View.OnHoverListener.html

Available from api 14.

Upvotes: 0

atraudes
atraudes

Reputation: 2408

You set an onTouchListener for the image, and in the onTouch event, you can pull the x,y coordinates out of the MotionEvent. getX and getY will get you the x and y coordinates in relation to the image, and getRawX and getRawY will get the x,y coordinates of the screen.

public boolean onTouch(View arg0, MotionEvent arg1) {
   System.out.println("X: "+arg1.getX());
   System.out.println("Y: "+arg1.getY());
   System.out.println("Raw X: "+arg1.getRawX());
   System.out.println("Raw Y: "+arg1.getRawY());
   return true;
}

Upvotes: 2

AndrewKS
AndrewKS

Reputation: 3817

As stakx said, Android doesn't support the mouse. However, if you are referring to the image that currently has focus, try this:

Image focusedImg = getViewById(R.id.YourMainLayout).findFocus();

int[] relativeToParentPixels = { focusedImg.getLeft(), focusedImg.getTop(), 
                                 focusedImg.getRight(), focusedImg.getBottom() };

Now you'll have the boundary positions of the image in an array.

Upvotes: 0

Quoted from the "android-porting" mailing list (end of August 2010):

Android currently doesn't support mouse input, so has no concept of mouse hover.

Upvotes: 3

Related Questions