代码之家  ›  专栏  ›  技术社区  ›  Perraco

如何使用DashPathEffect绘制等距虚线

  •  2
  • Perraco  · 技术社区  · 7 年前

    float[] intervals = new float[]{ 3, 18 };
    DashPathEffect path = new DashPathEffect(intervals, 0); 
    paint.setPathEffect(path);
    … … … …
    canvas.drawCircle(x, y, radius, paint);
    

    但这会在圆的开始和结束处产生一个非等距的破折号,如下图所示:

    enter image description here

    计算等距破折号的公式是什么?

    1 回复  |  直到 7 年前
        1
  •  7
  •   Ben P.    7 年前

    n 破折号加号 n center radius 你想用的。

    double circumference = 2 * Math.PI * radius;
    float dashPlusGapSize = (float) (circumference / NUM_DASHES);
    intervals[0] = dashPlusGapSize * DASH_PORTION;
    intervals[1] = dashPlusGapSize * GAP_PORTION;
    
    DashPathEffect effect = new DashPathEffect(intervals, 0);
    paint.setPathEffect(effect);
    
    canvas.drawCircle(center, center, radius, paint);
    

    例如,我曾经 NUM_DASHES = 20 , DASH_PORTION = 0.75f GAP_PORTION = 0.25f ,我看到:

    enter image description here

    DASH_PORTION + GAP_PORTION 总计 1 ).

        2
  •  0
  •   Mattia Ferigutti    6 年前

        val measure = PathMeasure(path, false)
        val length = measure.getLength()
    
    推荐文章