DashPathEffect not working

由于公司的文字渲染引擎是自己实现的,在对单词进行标识时用到了虚线下划线的效果,通过 Paint 设置 setPathEffect() 方法,其中 DashPathEffect 类是官方提供的设置虚线的显示效果。在源码注释中明确说明 Paint 的 style 设置成 STROKE or FILL_AND_STROKE 才会生效。最近发现在高版本的 Android 设备(8.0)上,原本应该显示的虚线变成了实线。

官方的 API 在最新的 Android P 中并未对 DashPathEffect 类的说明进行修改

The intervals array must contain an even number of entries (>=2), with the even indices specifying the “on” intervals, and the odd indices specifying the “off” intervals. phase is an offset into the intervals array (mod the sum of all of the intervals). The intervals array controls the length of the dashes. The paint’s strokeWidth controls the thickness of the dashes. Note: this patheffect only affects drawing with the paint’s style is set to STROKE or FILL_AND_STROKE. It is ignored if the drawing is done with style == FILL.

然而,莫名其妙的在高版本失效,肯定是版本导致的问题。

Paint 中的 setPathEffect() 方法都是调用 Native 方法实现的,未能看到具体的实现逻辑。

所以只能尝试修改 Paint 的一些设置。

最终发现, style 设置成 FILL_AND_STROKE 是无效的, 设置成 STROKE 才能在高版本中完美展示虚线效果。

虽然是一个很小的问题,但是文档没有更新真心很坑。