NoviceMe
NoviceMe

Reputation: 3256

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: enter image description here

Upvotes: 0

Views: 2940

Answers (1)

isnot2bad
isnot2bad

Reputation: 24464

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

Upvotes: 3

Related Questions