简体   繁体   English

在画布上画一个正方形

[英]Drawing a square in canvas

I have a canvas that is drawing a circle with predefined bounds. 我有一块画布正在绘制具有预定义范围的圆。

canvas.drawCircle((float) (getBounds().right / 2), (float) (getBounds().bottom / 2), (float) (getBounds().right / 2), paint);

and now I need to make it draw a square instead of a circle. 现在我需要使其绘制一个正方形而不是一个圆形。 so this is what I modified 所以这就是我修改的

(getBounds().right / 2) instead of it dividing by 2 I didn't divide it by anything for the radius (getBounds().right / 2)而不是除以2我没有将半径除以任何东西

how ever it just overflows. 怎么会溢出。

So this is how it looks like when its a normal circle. 因此,这是正常圆时的样子。

在此处输入图片说明

and this is how it looks when I attempted to make it a square 这就是我尝试使其变成正方形时的外观

在此处输入图片说明

can someone please suggest me how I can draw a successful square with the bounds I have been given? 有人可以建议我如何用给定的边界绘制成功的正方形吗?

Look's like the answer was fairly simple. 看起来答案很简单。

all I had to do is pass my rect and paint to Canvas::drawRect 我所要做的就是将我的rectpaint传递到Canvas::drawRect

here is the code 这是代码

Rect rect = drawable.getBounds();
canvas.drawRect(rect, paint);

and image of the result 结果的图片

在此处输入图片说明

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

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