Flutter 绘制虚线

Flutter 没有自带的虚线绘制工具,需要自己绘制,下面提供一个方法来绘制吧,可以作为Paint的扩展,也可以直接用这个方法绘制。上代码。

Dart 复制代码
void dashPaint(
      Canvas canvas, startdx, startdy, enddx,enddy, dashWidth, gapWidth, Paint paint) {
    // 使用循环来模拟虚线效果
// dashWidth 虚线的宽度,gapWidth的宽度, start起点坐标,end终点坐标
    double currentX = startdx;
    double currentY = startdy;
    double d= sqrt((enddx- startdx)*(enddx- startdx)+(enddy- startdy)*(enddy- startdy)); //计算线段长度
    // sin
    double sin = (enddy - startdy) /d;
    double cos = (enddx - startdx) /d;
    while ((sin>0&&currentY < enddy) ||(sin<0&&currentY > enddy)){
      canvas.drawLine(
        Offset(currentX, currentY),
        Offset(currentX+ dashWidth* cos, currentY + dashWidth * sin ),
        paint,
      );
      currentX += (dashWidth + gapWidth)* cos;
      currentY += (dashWidth + gapWidth)* sin;
    }
  }
相关推荐
我怎么能这么帅气6 分钟前
JavaScript 变量提升 (Hoisting):那些让你代码“行为异常”的隐形规则
前端·javascript
江城开朗的豌豆7 分钟前
JavaScript篇:JS类型转换的黑魔法:从入门到怀疑人生
前端·javascript·面试
我怎么能这么帅气12 分钟前
告别旧标签:HTML5 废弃标签清单与现代替代方案
前端·javascript·html
椒盐煎蛋16 分钟前
新建的Flutter插件工程,无法索引andorid工程代码;无法索引io.flutter包下代码。
flutter
黄雪超18 分钟前
JVM——JVM中的字节码:解码Java跨平台的核心引擎
java·开发语言·jvm
Shining_Jiang22 分钟前
打卡第36天:模型可视化以及推理
开发语言·python
Am1nnn23 分钟前
【定时器】定时器存在的内存泄露问题
开发语言·javascript
张风捷特烈24 分钟前
每日一题 Flutter#3 | 说说 Widget 的派生体系
android·flutter·面试
烬奇小云25 分钟前
怎么通过 jvmti 去 hook java 层函数
java·开发语言