2

如何Path使用褪色(不透明度或粗细)线进行绘制?像这样的东西。

衰落的例子

我知道 有LinearGradient着色器,Paint但它不会沿.Path

一种可能的解决方案可能是沿线获得点,Path然后自己通过线段绘制它。但我也找不到任何方法。

4

1 回答 1

1

我想出了以下代码。最重要的PathMeasuregetPosTan()方法

if (getGesturePath() != null) {
    final short steps = 150;
    final byte stepDistance = 5;
    final byte maxTrailRadius = 15;
    pathMeasure.setPath(getGesturePath(), false);
    final float pathLength = pathMeasure.getLength();
    for (short i = 1; i <= steps; i++) {
        final float distance = pathLength - i * stepDistance;
        if (distance >= 0) {
            final float trailRadius = maxTrailRadius * (1 - (float) i / steps);
            pathMeasure.getPosTan(distance, pathPos, null);
            final float x = pathPos[0] + RandomUtils.nextFloat(0, 2 * trailRadius) - trailRadius;
            final float y = pathPos[1] + RandomUtils.nextFloat(0, 2 * trailRadius) - trailRadius;
            paint.setShader(new RadialGradient(
                    x,
                    y,
                    trailRadius > 0 ? trailRadius : Float.MIN_VALUE,
                    ColorUtils.setAlphaComponent(Color.GREEN, random.nextInt(0xff)),
                    Color.TRANSPARENT,
                    Shader.TileMode.CLAMP
            ));
            canvas.drawCircle(x, y, trailRadius, paint);
        }
    }
}
于 2016-11-03T11:56:54.220 回答