在Android和Java画椭圆之间的差异 [英] Differences between drawing an Ellipse in Android and Java

查看:205
本文介绍了在Android和Java画椭圆之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中由于某种原因, Ellipse2D.Double 使用参数(高度,宽度,X,Y)在那里,当我创建一个 RectF Android中的参数是(左,上,右,下)所以我M于适应的差异有点混乱。

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.

如果一个Java创建一个椭圆,并使用下列内容:

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

请问这是相当于Android的:

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?

另外,可以将一个覆盖 RectF 使其这类似于如何 Ellipse2D的? IE浏览器。更改参数与的高度和宽度的工作,而不是的右侧和底部的?

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?

推荐答案

有关覆盖的部分,我不知道的事情这将是一个好主意,因为RectF不仅用于椭圆。

For the override part, I don't thing it would be a good idea since RectF isn't only used for ellipses.

您可以轻松编写通过传递数据的方式绘制椭圆的方法你preFER ...

you can easily write a method that draw the Oval by passing the data the way you prefer...

是这样的:

public RectF myOval(float width, float height, float x, float y){
    float halfW = width/2;
    float halfH = height/2;
    return new RectF(x-halfW, y-halfH, x+halfW, y+halfH);
}

canvas.drawOval(myOval(width, height, x, y), paint);

这篇关于在Android和Java画椭圆之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆