创建椭圆/圆矩形内矩形、椭圆

由网友(蹲墙角沉默)分享简介:我想创建一个矩形内椭圆/圆。我试图做到这一点在画布上的位图图像。这里是我的code:I am trying to create an oval/circle inside a rectangle. I am trying to do this on canvas for a bitmap image. Here is...

我想创建一个矩形内椭圆/圆。我试图做到这一点在画布上的位图图像。这里是我的code:

I am trying to create an oval/circle inside a rectangle. I am trying to do this on canvas for a bitmap image. Here is my code:

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

现在我想创建这个矩形内的一个椭圆/圆。我尝试这样做,一直在努力了几个小时不能得到它的工作:

Now i want to create an oval/circle inside this rectangle. I tried this and been trying for hours cant get it to work:

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

有人可以帮我想出解决办法?下面是可视化,以帮助我试图来实现:

Can someone please help me figure this out? Here is visual to help what i am trying to achieve:

推荐答案

您应该使用相同的范围比你用于绘制矩形:

You should use the same bounds than you used for drawing the rectangle:

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

相关推荐

最新文章