如何绘制衰落路径 [英] How tro draw fading path
本文介绍了如何绘制衰落路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何绘制带有淡入淡出(不透明或较粗)线条的Path
?像这样的东西.
How can I draw Path
with fading (opacity or thicknes) line? Something like this.
我知道有LinearGradient
着色器用于Paint
,但不会沿Path
弯曲 .
I know there is LinearGradient
shader for Paint
, but it won't bend along the Path
.
一种可能的解决方案是沿Path
获取点,然后由我自己通过线段将其绘制出来.但是我也找不到任何方法.
One possible solution might be to get points along the Path
and just draw it by myself through the segments`. But I coouldn't find any method for that either.
推荐答案
I came up with the following code. The mos important thing is 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);
}
}
}
这篇关于如何绘制衰落路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文