Camus Camus - 1 year ago 82
Android Question

Android: How to determine if a touch event is in a circle?

I want to play a media when i touch a circular area, but how can I could determine that my touch position is in the circle?

So far I extend a

and implement the
, and I need the algorithm for deciding if the position is inside or outside the circle.

Answer Source

You should take position of the view with View.getX() and View.getY() to get x and y of the upper left corner and also assuming You know the radius (or able to obtain width/height of the view to determine radius). After that, obtain xTouch and yTouch using MotionEvent.getX() and MotionEvent.getY() and check if:

(xTouch - (x + radius)) * (xTouch - (x + radius)) + (yTouch - (y + radius)) * (yTouch - (y + radius)) <= radius * radius

The formula is just interpretation of schools geometry for determining if dot is inside circle area or not. Refer to circle equation for Cartesian coordinates for more details.

Values explanation is:

(x + radius) and (y + radius) is the center of circle.

(xTouch - (x + radius)) is distance from touch point to center by X.

(yTouch - (y + radius)) is distance from touch point to center by Y.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download