在矩形内创建椭圆/圆

时间:2013-11-23 23:04:54

标签: java android eclipse android-canvas

我正在尝试在矩形内创建一个椭圆/圆。我试图在画布上为位图图像执行此操作。这是我的代码:

int x = (int) (midpoint.x*xRatio);
int y = (int) (midpoint.y*yRatio);
int radius = (int) (distance/2);
int left =  x - radius;
int right = x + radius;
int top = y - radius;

canvas.drawRect(left, top, right, bottom, paint);

现在我想在这个矩形内创建一个椭圆/圆。我试过这个并且一直试着让它无法工作:

RectF ovalBounds = new RectF();
//ovalBounds.set(x, y,  (right - left)/2, (bottom-top)/2);
ovalBounds.set(x, y-radius, radius * 2, radius * 2);
canvas.drawOval(ovalBounds, paint);                 

有人可以帮我解决这个问题吗? 这是视觉,以帮助我想要实现的目标: enter image description here

1 个答案:

答案 0 :(得分:3)

您应该使用与绘制矩形时相同的边界:

RectF rect = new RectF(left, top, right, bottom);
canvas.drawRect(rect, paint);
canvas.drawOval(rect, paint);