将图像放在圆上

时间:2014-06-12 08:55:44

标签: android

我有一个圆圈,我想在圆圈上放置一个图像。

我知道角度和圆半径,我需要获得放置图像的x和y坐标。

图像必须在圆圈上而不是IN。

代码:

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);
}

1 个答案:

答案 0 :(得分:1)

你知道圆圈的x和y,你知道半径。所以你知道它在x +半径的3点钟位置,在12点位置它的y +半径。

然而,要获得其他积分,你需要一些数学。

x =半径* cos(角度)+ xCenter;
y =半径* sin(角度)+ yCenter;

角度必须是弧度。所以你可能会做的是

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

还有关于它的其他SO问题,如
  - Find point on Circle on Android
  - Calculate points around a circle in android
  - Getting Coordinates of a Point in a circle

相关问题