在Java中,由于某种原因,Ellipse2D.Double
使用参数(height, width, x, y)
,就像我创建RectF
时一样code> 在 Android 中,参数是 (left, top, right, Bottom)
所以我对调整差异有点困惑。
如果在 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。更改参数以使用高度和宽度而不是右和底部?
最佳答案
对于覆盖部分,我认为这不是一个好主意,因为 RectF 不仅仅用于椭圆。
您可以轻松地编写一个通过按照您喜欢的方式传递数据来绘制椭圆形的方法...
类似于:
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);
关于java - Android 和 Java 中绘制椭圆的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6903568/