简体   繁体   English

在矩形内创建椭圆/圆形

[英]Creating oval/circle inside a rectangle

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM