添加CornerPathEffect时FillType.EVEN_ODD的不同行为?

dav*_*ino 13 android path paint shape drawable

我在Drawable这里试验,发现了一些我无法解释的东西,希望有人可以帮助我.

为什么加入CornerPathEffectPaint似乎"破"(?)的EVEN_ODD FillType

更具体地说,我正在按原样测试这个HexagonDrawable类.这就是我得到的:

硬角,概述,预期的行为.

但是,如果我设置CornerPathEffectPaint,如下所示(构造函数)...

public HexagonDrawable(int color) {
    paint.setColor(color);
    paint.setPathEffect(new CornerPathEffect(6)); // added
    hexagon.setFillType(Path.FillType.EVEN_ODD);
}
Run Code Online (Sandbox Code Playgroud)

......这就是我得到的:

圆角,轮廓效果消失,意外行为?

圆角,是的,但没有轮廓的外观(奇数/偶数/奇数).有人可以解释一下原因吗?

Ren*_*ari 5

HexagonDrawable类绘制两个不同的六边形,堆叠在一起.我不知道你是否需要它,但我认为实现相同结果的最佳方法是使用Paint with Stroke样式.

为此,您需要删除第二个六边形路径并减小六边形的大小(因此视图不会切割它):

public void computeHex(Rect bounds) {

    final int width = bounds.width();
    final int height = bounds.height();
    // We need to decrease the hexagon's size, so the view won't cut our stroke
    final int size = Math.min(width - (strokeWidth / 2), height - (strokeWidth / 2));
    final int centerX = bounds.left + (width / 2);
    final int centerY = bounds.top + (height / 2);

    hexagon.reset();
    hexagon.addPath(createHexagon(size, centerX, centerY));
    // Remove the second path
    // hexagon.addPath(createHexagon((int) (size * .8f), centerX, centerY));
} 
Run Code Online (Sandbox Code Playgroud)

并将描边效果添加到绘画中:

private int strokeWidth;

public HexagonDrawable(int color, int strokeWidth) {        
    this.strokeWidth = strokeWidth;

    paint.setColor(color);

    // Add Stroke style and width
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(strokeWidth);

    // You can add these other attributes if you need to
    // paint.setDither(true);
    // paint.setStrokeJoin(Paint.Join.ROUND);
    // paint.setStrokeCap(Paint.Cap.ROUND);

    // Remove the fill type, you won't need anymore
    // hexagon.setFillType(Path.FillType.EVEN_ODD);

    // Finally add the Path Effect
    paint.setPathEffect(new CornerPathEffect(30.0f));
}
Run Code Online (Sandbox Code Playgroud)

这应该会产生与你想要的非常相似的效果.

希望能帮助到你!;)

编辑:我忘了警告你,行程宽度不能大于CornerPathEffect的半径,否则它会被切断.