Android 和 Java 中绘制椭圆的区别
在Java中,由于某种原因,Ellipse2D.Double
使用参数(height, width, x, y)
,就像我在中创建RectF
时一样Android 的参数是(左、上、右、下)
,所以我在调整差异时有点困惑。
如果在 Java 中创建一个椭圆并使用以下内容:
//Ellipse2D.Double(height, width, x, y)
x = 100;
y = 120;
centerX = getWidth() / 2;
centerY = getHeight() / 2;
//Ellipse2D.Double(100, 120, (centerX - 100) * 2, (centerY - 120) * 2);
new Ellipse2D.Double(x, y, (centerX - x) * 2, (centerY - y) * 2);
这对于 Android 来说是否等效:
//RectF(left, top, right, bottom)
x = 100;
y = 120;
centerX = getWidth() / 2;
centerY = getHeight() / 2;
new RectF((centerX - 100) * 2, (centerY - 120) * 2), 120 - ((centerX - 100) * 2), 100 - ((centerY -120) * 2);
//canvas.drawOval(myRectF, paint);
我不太确定它们是否等效,并且想知道我是否正确计算了它?
或者,可以重写 RectF 使其与 Ellipse2D 相似吗? IE。更改参数以使用高度和宽度而不是右和底部?
In Java for some reason the Ellipse2D.Double
uses the parameters (height, width, x, y)
where as when I create a RectF
in Android the parameters are (left, top, right, bottom)
so I'm a bit confused on adjusting to the differences.
If a create an Ellipse in Java and use the following:
//Ellipse2D.Double(height, width, x, y)
x = 100;
y = 120;
centerX = getWidth() / 2;
centerY = getHeight() / 2;
//Ellipse2D.Double(100, 120, (centerX - 100) * 2, (centerY - 120) * 2);
new Ellipse2D.Double(x, y, (centerX - x) * 2, (centerY - y) * 2);
Would this be equivalent for Android:
//RectF(left, top, right, bottom)
x = 100;
y = 120;
centerX = getWidth() / 2;
centerY = getHeight() / 2;
new RectF((centerX - 100) * 2, (centerY - 120) * 2), 120 - ((centerX - 100) * 2), 100 - ((centerY -120) * 2);
//canvas.drawOval(myRectF, paint);
I'm not quite sure if they are equivalent, and am wondering if I am calculating it correctly?
Alternatively, can one override the RectF
to make it simliar to how Ellipse2D
? Ie. change the parameters to work with height and width rather than right and bottom?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于覆盖部分,我认为这不是一个好主意,因为 RectF 不仅仅用于椭圆。
您可以轻松地编写一个通过按照您喜欢的方式传递数据来绘制椭圆形的方法...
类似于:
For the override part, I don't thing it would be a good idea since RectF isn't only used for ellipses.
you can easily write a method that draw the Oval by passing the data the way you prefer...
something like:
为了保持 x、y、宽度、高度的思维,您可以构造一个实用函数来按照您喜欢的顺序构建一个带有坐标的 RectF:
目前还不清楚您要对代码示例执行什么操作有。
Ellipse2D.Double 采用 4 个参数:
x
、y
、width
和height
。看起来您将宽度设置为
(centerX - x) * 2
;如果中心位于点 (100, 120) 的右侧,这将确保宽度是从代码所在组件的中心到点 (100, 120) 的距离的两倍。但是,如果您的组件变得太小,您将分配一个负宽度,这可能会很尴尬。另外,您在 RectF 示例中使用硬编码值,并将 120(y?)和 100(x?)组合到 RectF 的相同参数中,这很可能不是您想要做的。
我建议在一张纸上画一幅画,用您认为应该的值标记坐标,然后编写代码。您应该能够更清楚地看到左上、下和右(或 x、y、宽度和高度)值应该是什么。
To keep in the x, y, width, height thinking, you can construct a utility function to build a RectF with the coordinates in the order you like to think of them:
It is unclear what you are trying to do with the code sample you have.
Ellipse2D.Double takes 4 parameters:
x
,y
,width
andheight
.It looks like you are setting width to be
(centerX - x) * 2
; this will ensure that the width is twice the distance from the center of the component your code resides in to the point (100, 120), if the center is to the right of the point (100, 120). If your component gets too small, though, you will assign a negative width, which could be awkward.Also, you are using hardcoded values in your RectF example, and combining 120 (the y?) and 100 (the x?) in the same arguments to RectF, which is most likely not what you want to do.
I'd suggest drawing a picture on a piece of paper, label the coordinates with the values you think they should be, then write your code. You should be able to more clearly see what your top left bottom and right (or x, y, width and height) values should be.