前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Flutter 绘制探索 | 来一起画箭头吧

Flutter 绘制探索 | 来一起画箭头吧

作者头像
张风捷特烈
发布2022-09-08 11:56:33
7490
发布2022-09-08 11:56:33
举报
文章被收录于专栏:Android知识点总结

—\ntheme: cyanosis\n—\n##### 0. 前言\n\n可能有人会觉得,画箭头有什么好说的,不就一根线加两个头吗?其实箭头的绘制还是比较复杂的,其中也蕴含着很多绘制的小技巧。箭头本身有着很强的 示意功能 ,通常用于指示、标注、连接。各种不同的箭头端,再加上线型的不同,可以组合成一些固定连接语法,比如 UML 中的类图。\n\n

\n\n—\n\n一个箭头,其核心数据是两个点的坐标,由 左右端点线型 构成。这篇文章就来探索一下,如何绘制一个支持各种样式,而且容易拓展的箭头。\n\n

\n\n—\n\n##### 1. 箭头部位的划分\n\n首先要说一点,我希望获取的是箭头的 路径 ,而非单纯的绘制箭头。因为有了路径,可以做更多的事,比如根据路径裁剪、沿路径运动、多个路径间的合并操作等。当然,路径形成之后,绘制自然是非常简单的。所以在绘制技巧中,路径一个非常重要的话题。 \n如下所示,我们先来生成三个部分的路径,并进行绘制,两端暂时是圆形路径:\n\n

\n\n代码实现如下,测试使用的起始点分别是 (40,40)(200,40),圆形路径以起始点为中心,宽高为 10。可以看出虽然实现了需求,但是都写在一块,代码看起来比较乱。当要涉及生成各种样式箭头时,在这里修改代码也是非常麻烦的,接下来要做的就是对箭头的路径形成过程进行抽象。\n\ndart\nfinal Paint arrowPainter = Paint();\n\nOffset p0 = Offset(40, 40);\nOffset p1 = Offset(200, 40);\ndouble width = 10;\ndouble height = 10;\n\nRect startZone = Rect.fromCenter(center: p0, width: width, height: height);\nPath startPath = Path()..addOval(startZone);\nRect endZone = Rect.fromCenter(center: p1, width: width, height: height);\nPath endPath = Path()..addOval(endZone);\n\nPath linePath = Path()..moveTo(p0.dx, p0.dy)..lineTo(p1.dx, p1.dy);\n\narrowPainter\n ..style = PaintingStyle.stroke..strokeWidth = 1\n ..color = Colors.red;\n\ncanvas.drawPath(startPath, arrowPainter);\ncanvas.drawPath(endPath, arrowPainter);\ncanvas.drawPath(linePath, arrowPainter);\n\n\n—\n\n如下,定义抽象类 AbstractPathformPath 抽象出来,交由子类实现。端点的路径衍生出 PortPath 进行实现,这就可以将一些重复的逻辑进行封装,也有利于维护和拓展。整体路径的生成由 ArrowPath 类负责:\n\ndart\nabstract class AbstractPath{\n Path formPath();\n}\n\nclass PortPath extends AbstractPath{\n final Offset position;\n final Size size;\n\n PortPath(this.position, this.size);\n\n @override\n Path formPath() {\n Path path = Path();\n Rect zone = Rect.fromCenter(center: position, width: size.width, height: size.height);\n path.addOval(zone);\n return path;\n }\n}\n\nclass ArrowPath extends AbstractPath{\n final PortPath head;\n final PortPath tail;\n\n ArrowPath({required this.head,required this.tail});\n\n @override\n Path formPath() {\n Offset line = (tail.position - head.position);\n Offset center = head.position+line/2;\n double length = line.distance;\n Rect lineZone = Rect.fromCenter(center:center,width:length,height:2);\n Path linePath = Path()..addRect(lineZone);\n Path temp = Path.combine(PathOperation.union, linePath, head.formPath());\n return Path.combine(PathOperation.union, temp, tail.formPath());\n }\n}\n\n\n—\n\n这样,矩形域的确定和路径的生成,交由具体的类进行实现,在使用时就会方便很多:\n\ndart\ndouble width =10;\ndouble height =10;\nSize portSize = Size(width, height);\nArrowPath arrow = ArrowPath(\n head: PortPath(p0, portSize),\n tail: PortPath(p1, portSize),\n);\ncanvas.drawPath(arrow.formPath(), arrowPainter);\n\n\n

\n\n—\n\n##### 2. 关于路径的变换\n\n上面我们的直线其实是矩形路径,这样就会出现一些问题,比如当箭头不是水平线,会出现如下问题:\n\n

\n\n解决方案也很简单,只要让矩形直线的路径沿两点的中心进行旋转即可,旋转的角度就是两点与水平线的夹角。这就涉及了绘制中非常重要的技巧:矩阵变换 。如下代码添加的四行 Matrix4 的操作,就可以通过矩阵变换,让 linePathcenter 为中心旋转两点间角度。这里注意一下,tag1 处的平移是为了将变换中心变为 center、而tag2 处的反向平移是为了抵消 tag1 平移的影响。这样在两者之间的变换,就是以 center 为中心的变换: \n\ndart\nclass ArrowPath extends AbstractPath{\n final PortPath head;\n final PortPath tail;\n\n ArrowPath({required this.head,required this.tail});\n\n @override\n Path formPath() {\n Offset line = (tail.position - head.position);\n Offset center = head.position+line/2;\n double length = line.distance;\n Rect lineZone = Rect.fromCenter(center:center,width:length,height:2);\n Path linePath = Path()..addRect(lineZone);\n\n // 通过矩阵变换,让 linePath 以 center 为中心旋转 两点间角度 \n Matrix4 lineM4 = Matrix4.translationValues(center.dx, center.dy, 0); // tag1\n lineM4.multiply(Matrix4.rotationZ(line.direction));\n lineM4.multiply(Matrix4.translationValues(-center.dx, -center.dy, 0)); // tag2\n linePath = linePath.transform(lineM4.storage);\n\n Path temp = Path.combine(PathOperation.union, linePath, head.formPath());\n return Path.combine(PathOperation.union, temp, tail.formPath());\n }\n}\n\n\n这样就一切正常了,可能有人会疑惑,为什么不直接用两点形成路径呢?这样就不需要旋转了:\n\n

\n\n前面说了,这里希望获得的是一个 箭头路径 ,使用线型模式就可以看处用矩形的妙处。如果单纯用路径的移动来处理,需要计算点位,比较复杂。而用矩形加旋转,就方便很多:\n\n\n

\n\n—\n\n##### 3.尺寸的矫正\n\n可以看出,目前是以起止点为圆心的矩形区域,但实际我们需要让箭头的两端顶点在两点上。有两种解决方案:其一,在 PortPath 生成路径时,对矩形区域中心进行校正;其二,在合成路径前通过偏移对首位断点进行校正。\n\n

\n\n我更倾向于后者,因为我希望 PortPath 只负责断点路径的生成,不需要管其他的事。另外 PortPath 本身也不知道端点是起点还是终点,因为起点需要沿线的方向偏移,终点需要沿反方向偏移。处理后效果如下:\n\n

\n\ndart\n---->[ArrowPath#formPath]----\nPath headPath = head.formPath();\nMatrix4 headM4 = Matrix4.translationValues(head.size.width/2, 0, 0);\nheadPath = headPath.transform(headM4.storage);\n\nPath tailPath = tail.formPath();\nMatrix4 tailM4 = Matrix4.translationValues(-head.size.width/2, 0, 0);\ntailPath = tailPath.transform(tailM4.storage);\n\n\n—\n\n虽然表面上看起来和顶点对齐了,但换个不水平的线就会看出端倪。我们需要 沿线的方向 进行平移,也就是说,要保证该直线过矩形区域圆心:\n\n

\n\n如下所示,我们在对断点进行平移时,需要根据线的角度来计算偏移量:\n\n

\n\ndart\n Path headPath = head.formPath();\n double fixDx = head.size.width/2*cos(line.direction);\n double fixDy = head.size.height/2*sin(line.direction);\n \n Matrix4 headM4 = Matrix4.translationValues(fixDx, fixDy, 0);\n headPath = headPath.transform(headM4.storage);\n Path tailPath = tail.formPath();\n Matrix4 tailM4 = Matrix4.translationValues(-fixDx, -fixDy, 0);\n tailPath = tailPath.transform(tailM4.storage);\n\n\n—\n\n##### 4.箭头的绘制\n\n每个 PortPath 都有一个矩形区域,接下来只要专注于在该区域内绘制箭头即可。比如下面的 p0p1p2 可以形成一个三角形:\n\n

\n\n对应代码如下:\n\ndart\nclass PortPath extends AbstractPath{\n final Offset position;\n final Size size;\n\n PortPath(this.position, this.size);\n\n @override\n Path formPath() {\n Path path = Path();\n Rect zone = Rect.fromCenter(center: position, width: size.width, height: size.height);\n Offset p0 = zone.centerLeft;\n Offset p1 = zone.bottomRight;\n Offset p2 = zone.topRight;\n path..moveTo(p0.dx, p0.dy)..lineTo(p1.dx, p1.dy)..lineTo(p2.dx, p2.dy)..close();\n return path;\n }\n}\n\n\n由于在 PortPath 中无法感知到子级是头还是尾,所以下面可以看出两个箭头都是向左的。处理方式也很简单,只要转转 180° 就行了。\n\n

\n\n—\n\n另外,这样虽然看起来挺好,但也有和上面类似的问题,当改变坐标时,就会出现不和谐的情景。解决方案和前面一样,为断点的箭头根据线的倾角添加旋转变换即可。\n\n

\n\n—\n\n如下进行旋转,即可得到期望的箭头,tag3 处可以顺便旋转 180° 把尾点调正。这样任意指定两点的坐标,就可以得到一个箭头。\n\n

\n\ndart\nMatrix4 headM4 = Matrix4.translationValues(fixDx, fixDy, 0);\ncenter = head.position;\nheadM4.multiply(Matrix4.translationValues(center.dx, center.dy, 0));\nheadM4.multiply(Matrix4.rotationZ(line.direction));\nheadM4.multiply(Matrix4.translationValues(-center.dx, -center.dy, 0));\nheadPath = headPath.transform(headM4.storage);\n\nMatrix4 tailM4 = Matrix4.translationValues(-fixDx, -fixDy, 0);\ncenter = tail.position;\ntailM4.multiply(Matrix4.translationValues(center.dx, center.dy, 0));\ntailM4.multiply(Matrix4.rotationZ(line.direction-pi)); // tag3\ntailM4.multiply(Matrix4.translationValues(-center.dx, -center.dy, 0));\ntailPath = tailPath.transform(tailM4.storage);\n\n\n—\n\n##### 5.箭头的拓展\n\n从上面可以看出,这个箭头断点的拓展能力是很强的,只要在矩形区域内形成相应的路径即可。比如下面带两个尖角的箭头形式,路径生成代码如下:\n\n

\n\ndart\nclass PortPath extends AbstractPath{\n final Offset position;\n final Size size;\n\n PortPath(this.position, this.size);\n\n @override\n Path formPath() {\n Rect zone = Rect.fromCenter(center: position, width: size.width, height: size.height);\n return pathBuilder(zone);\n }\n\n Path pathBuilder(Rect zone){\n Path path = Path();\n Rect zone = Rect.fromCenter(center: position, width: size.width, height: size.height);\n final double rate = 0.8;\n Offset p0 = zone.centerLeft;\n Offset p1 = zone.bottomRight;\n Offset p2 = zone.topRight;\n Offset p3 = p0.translate(rate*zone.width, 0);\n path..moveTo(p0.dx, p0.dy)..lineTo(p1.dx, p1.dy)\n ..lineTo(p3.dx, p3.dy)..lineTo(p2.dx, p2.dy)..close();\n return path;\n }\n}\n\n\n这样如下所示,只要更改 pathBuilder 中的路径构建逻辑,就可以得到不同的箭头样式。而且你只需要在矩形区域创建正着的路径即可,箭头跟随直线的旋转已经被封装在了 ArrowPath 中。这就是 屏蔽细节 ,简化使用流程。不然创建路径时还有进行角度偏转计算,岂不麻烦死了。\n\n\n

\n\n—\n\n到这里,多样式的箭头设置方案应该就呼之欲出了。就像是 Flutter 动画中的各种 Curve 一样,通过抽象进行衍生,实现不同类型的数值转变。这里我们也可以对路径构建的行为进行抽象,来衍生出各种路径类。这样的好处在于:在实现类中,可以定义额外的参数,对绘制的细节进行控制。 \n如下,抽象出 PortPathBuilder ,通过 fromPathByRect 方法,根据矩形区域生成路径。在 PortPath 中就可以依赖 抽象 来完成任务: \n\ndart\nabstract class PortPathBuilder{\n const PortPathBuilder();\n Path fromPathByRect(Rect zone);\n}\n\nclass PortPath extends AbstractPath {\n final Offset position;\n final Size size;\n PortPathBuilder portPath;\n\n PortPath(\n this.position,\n this.size, {\n this.portPath = const CustomPortPath(),\n });\n\n @override\n Path formPath() {\n Rect zone = Rect.fromCenter(\n center: position, width: size.width, height: size.height);\n return portPath.fromPathByRect(zone);\n }\n}\n\n\n—\n\n在使用时,可以通过指定 PortPathBuilder 的实现类,来配置不同的端点样式,比如实现一开始那个常规的 CustomPortPath :\n\ndart\nclass CustomPortPath extends PortPathBuilder{\n const CustomPortPath();\n\n @override\n Path fromPathByRect(Rect zone) {\n Path path = Path();\n Offset p0 = zone.centerLeft;\n Offset p1 = zone.bottomRight;\n Offset p2 = zone.topRight;\n path..moveTo(p0.dx, p0.dy)..lineTo(p1.dx, p1.dy)..lineTo(p2.dx, p2.dy)..close();\n return path;\n }\n}\n\n\n—\n\n以及三个箭头的 ThreeAnglePortPath ,我们可以将 rate 提取出来,作为构造入参,这样就可以让箭头拥有更多的特性,比如下面是 0.50.8 的对比:\n\n

\n\ndart\nclass ThreeAnglePortPath extends PortPathBuilder{\n final double rate;\n\n ThreeAnglePortPath({this.rate = 0.8});\n\n @override\n Path fromPathByRect(Rect zone) {\n Path path = Path();\n Offset p0 = zone.centerLeft;\n Offset p1 = zone.bottomRight;\n Offset p2 = zone.topRight;\n Offset p3 = p0.translate(rate * zone.width, 0);\n path\n ..moveTo(p0.dx, p0.dy)\n ..lineTo(p1.dx, p1.dy)\n ..lineTo(p3.dx, p3.dy)\n ..lineTo(p2.dx, p2.dy)\n ..close();\n return path;\n }\n}\n\n\n—\n\n想要实现箭头不同的端点类型,只有在构造 PortPath 时,指定对应的 portPath 即可。如下红色箭头的两端分别使用 ThreeAnglePortPathCirclePortPath 。\n\n

\n\ndart\nArrowPath arrow = ArrowPath(\n head: PortPath(\n p0.translate(40, 0),\n const Size(10, 10),\n portPath: const ThreeAnglePortPath(rate: 0.8),\n ),\n tail: PortPath(\n p1.translate(40, 0),\n const Size(8, 8),\n portPath: const CirclePortPath(),\n ),\n);\n\n\n这样一个使用者可以自由拓展的箭头绘制小体系就已经能够完美运转了。大家可以基于此体会一下其中 抽象 的意义,以及 多态 的体现。本篇中有很多旋转变换的绘制小技巧,下一篇,我们来一起绘制各种各样的 PortPathBuilder 实现类,以此丰富箭头绘制,打造一个小巧但强大的箭头绘制库。\n\n- 我正在参与掘金技术社区创作者签约计划招募活动,点击链接报名投稿。\n

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-07-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档