0

I have a circle and I want to place an image on the circle.

I know the angle and the circle radius and I need to get x and y coordinates for placing the image.

The image have to be ON the circle and not IN.

Code :

private void placeImageOnCircle(ImageView circle, RelativeLayout imageToMove, int angle)
{ 
    int radius = circle.getWidth()/2;

    // get marginX and marginY...

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.setMargins(marginX, marginY, 0, 0);
    imageToMove.setLayoutParams(params);
}
Florian Mac Langlade
  • 1,863
  • 7
  • 28
  • 57

1 Answers1

1

You know the x and y of your circle and you know the radius. So you know that at 3 o'clock its x+radius, and at 12 o'clock its y+radius.

However, to get other points you will need some math.

x = radius * cos(angle) + xCenter;
y = radius * sin(angle) + yCenter;

The angle has to be in radians. So what you might do is

x = radius * Math.cos(Math.toRadians(angle)) + xCenter
y = radius * Math.sin(Math.toRadians(angle)) + yCenter

Also se other SO questions about it, like
- Find point on Circle on Android
- Calculate points around a circle in android
- Getting Coordinates of a Point in a circle

Community
  • 1
  • 1
tritop
  • 1,655
  • 2
  • 18
  • 30
  • perhaps subtract xCenter, not add it? – Richard Le Mesurier Jun 12 '14 at 10:11
  • Are you sure about that? I am not too good with formulas and its hot today, but I think I get the right numbers when testing it with +. Please tell me if its wrong and you are sure so I can edit the answer. – tritop Jun 12 '14 at 11:53