flutter)如何用画布制作箭线?
I want to put an arrow at the end (or on both sides) of a line drawn with a drawLine() of canvas.
Like this
Can I know how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,这并不难 - 只涉及一点数学。
让我们有一条线并像这样设置油漆:
这将为我们提供:
现在,如果我们将此行视为向量,我们记得向量有方向。为了获得方向,我们必须找到线路和末端之间的角度。可以使用DART的数学库的
atan2
函数来完成。因此,导入数学库
导入'dart:数学'作为数学;
之后添加以下行
,让我们确定我们将要放在行结尾处的箭头的大小和角度:
剩下的一切实际上都在绘制箭头。这基本上是通过以正确角度绘制三角形的三角形来完成的。
最终将给我们:
That's actually not that hard to do - there's just a little math involved.
Let's have a line and paint set up like this:
this will give us:
Now if we think of this line as a vector, we remember a vector has a direction. To get the direction we must find the angle between the start and the end of the line. This can be done using the
atan2
function of dart's math library.So import the math library
import 'dart:math' as math;
and add the following lines
Afterwards let's determine a size and an angle for the arrow we're about to put at the end of the line:
Everything that's left is actually drawing the arrow. This is basically done by drawing a triangle at the proper angle the line is facing using a path.
Which will finally give us:
这是从晦涩的答案中得出的解决方案,从线上减去箭头的头部,因此不会伸出来:
Here's a solution derived from obscure's answer to subtract the head of the arrow from the line so it does not stick out:
简单:
致电课:
Simple:
Call the class: