如何减少褪色路径

时间:2016-11-03 08:12:13

标签: android android-canvas android-paint

如何使用渐变(不透明度或粗细)线绘制Path?这样的事情。

Fading example

我知道LinearGradientPaint着色器,但它在Path上不会弯曲

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

1 个答案:

答案 0 :(得分:1)

我想出了以下代码。最重要的是PathMeasure's getPosTan() method

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);
        }
    }
}