【发布时间】:2016-05-19 04:03:36
【问题描述】:
我想画一条弧线。 android 画布上两点之间相当宽的圆弧。
当然,我在这里彻底搜索,发现有几个人在问同一个问题(such as),但尽我所能,我无法对答案做出正面或反面,也无法获得 drawArc 功能做我需要做的事!
然后我终于找到了detailed breakdown 的具体方法。
private static void arcBetween(PointF e1, PointF e2, Paint paint, Canvas canvas) {
float a1Degrees = 36.0f;
double a1 = Math.toRadians(a1Degrees);
// l1 is half the length of the line from e1 to e2
double dx = e2.x - e1.x, dy = e2.y - e1.y;
double l = Math.sqrt((dx * dx) + (dy * dy));
double l1 = l / 2.0;
// h is length of the line from the middle of the connecting line to the
// center of the circle.
double h = l1 / (Math.tan(a1 / 2.0));
// r is the radius of the circle
double r = l1 / (Math.sin(a1 / 2.0));
// a2 is the angle at which L intersects the x axis
double a2 = Math.atan2(dy, dx);
// a3 is the angle at which H intersects the x axis
double a3 = (Math.PI / 2.0) - a2;
// m is the midpoint of the line from e1 to e2
double mX = (e1.x + e2.x) / 2.0;
double mY = (e1.y + e2.y) / 2.0;
// c is the the center of the circle
double cY = mY + (h * Math.sin(a3));
double cX = mX - (h * Math.cos(a3));
// rect is the square RectF that bounds the "oval"
RectF oval =
new RectF((float) (cX - r), (float) (cY - r), (float) (cX + r), (float) (cY + r));
// a4 is the starting sweep angle
double rawA4 = Math.atan2(e1.y - cY, e1.x - cX);
float a4 = (float) Math.toDegrees(rawA4);
canvas.drawArc(oval, a4, a1Degrees, false, paint);
}
唉,其中的数学运算超出了我的能力范围,但我设法将其复制/粘贴到代码中,并且它确实产生了弧线(如您在 image 中所见)。
但是,就我的目的而言,这条弧线存在三个问题。
为什么还有一条填充了封闭区域的直线?我只需要一条曲线,而不是圆顶。我以为
drawArc()中的 false 就是这样做的,但它已关闭并且似乎没有这样做。虽然弧的上角位于正确的位置,但我还放置(并超级三重检查)下点位于蓝色元素的边缘,而不是底部屏幕。然而,圆弧的一角仍然存在。
不过,一个非常重要的部分是我需要将圆弧做成不同的形状,以便在中间有更多的凸起。真的,我需要能够以编程方式调整那个凸起,所以我需要它以某种方式作为变量传递给那个函数。
就是这样,我一直在尝试,但还没有研究过这个数学,很困惑。非常感谢您的解决方案。
【问题讨论】:
-
看,当我尝试不使用该功能时,我认为椭圆需要设置为:new RectF(e1.x - bulge, e2.y - bulge, e2.x, e1 .y); (凸起是我任意和可调节的)。但是当我这样做时,我只是在一个完全不同的地方得到了一个奇怪的小条子。我只是不明白椭圆应该定义什么,我只是没有得到 startAngle 或 sweepAngle!啊!
-
"Really, I need to be able to adjust that bulge programmaticly"所以使用Path#quadTo -
我将其标记为已回答,因为样式问题已解决,并且您的评论解决了 quadTo 的剩余问题。哦! :)
-
老实说,如果您可以使用this,我不知道为什么要制作复杂的数学东西(sin、cos、atan),使用权重:-1、0、1 等
-
你能写出有效的答案吗?
标签: android math graphics trigonometry curve