DashPathEffect是PathEffect类的一个子类,可以使paint画出类似虚线的样子,并且可以任意指定虚实的排列方式.

/** intervals: array containing an even number of entries (>=2), with
     the even indices specifying the length of "on" intervals, and the odd
     indices specifying the length of "off" intervals.
    count: number of elements in the intervals array
    phase: offset into the intervals array (mod the sum of all of the
     intervals).

    For example: if intervals[] = {10, 20}, count = 2, and phase = 25,
     this will set up a dashed path like so:
     5 pixels off
     10 pixels on
     20 pixels off
     10 pixels on
     20 pixels off
     ...
    A phase of -5, 25, 55, 85, etc. would all result in the same path,
     because the sum of all the intervals is 30.

    Note: only affects stroked paths.
*/
static SkPathEffect* Create(const SkScalar intervals[], int count, SkScalar phase)

以上是官方的代码注释。

intervals定义了虚线的排列方式,数组必须是大于2的偶数个数的数组。下标为偶数为绘制像素,下标为奇数为空白像素。

phase表示偏移,也就是定义了第一段间距的绘制方式。

如例子中的intervals[] = {10, 20}, count = 2, and phase = 25

表示,绘制10个像素的线,然后空白20个像素,以此循环。

phase = 25表示,第25个像素开始下一个循环。所以,如果从0开始绘制的话,第一段虚线的实线部分只有5个像素。

用法:

// 因为PathEffect是引用计数的,所以记得有释放问题
paint->setPathEffect(SkDashPathEffect::Create(intervals, count, phase))->unref();