1

So I am making an android application where I have a set of circles. When the user touches a point in one of the circles I need to calculate a point according to where the user touched.
Somewhat similar to an archer's target.
how do I achieve this?

I have searched about touch interfaces but I haven't been able to figure out how i can do this

otaku
  • 849
  • 2
  • 13
  • 33

2 Answers2

3

You should use TouchListener with following code to get to know that whether your touch event occur inside circle or not.

Formula

(x - center_x)^2 + (y - center_y)^2 < radius^2

code

int x = view.getX();  
int y = view.getY();  

if((xTouch - (x + radius)) * (xTouch - (x + radius)) + (yTouch - (y + radius)) * (yTouch - (y + radius)) <= (radius * radius)){
    ...
}

If this satisfies left side equation is less then right side one, your touch event is inside circle else outside circle.

Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
  • Any ideas how i can get a fixed center for the app? I am planning to add those circles as the background of the app and then calculate accordingly.. or are there any other methods of doing this? – otaku Sep 18 '13 at 13:09
  • Yes, you can check my answer http://stackoverflow.com/questions/8974088/how-to-create-a-resizable-rectangle-with-user-touch-events-on-android/16359284#16359284. You can create custom circles on screens like that.. Just implement that code and you will get idea how to do that. – Chintan Rathod Sep 18 '13 at 13:14
  • @Chintan Rathod : I didn't get this answer. I did like this In side onTouch, float x=v.getX(); float y=v.getY(); Toast.makeText(getApplicationContext(), "X= "+x, Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), "Y= "+y, Toast.LENGTH_SHORT).show(); But No toast is coming for me... – Aneez Mar 24 '14 at 07:25
  • @Aneez its simple maths formula to find radios of a point. It will compare two circle whether they are intersect each other or not my friend. :) – Chintan Rathod Mar 24 '14 at 09:02
1

Just set onTouchListener and make your logic with event.getX()/getY()

Lebedevsd
  • 950
  • 5
  • 12