gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:27:24 28 4
gpt4 key购买 nike

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

为什么将 CornerPathEffect 添加到 Paint 似乎“破坏”(?)EVEN_ODD FillType?

更具体地说,我正在测试 this HexagonDrawable按原样上课。这是我得到的:

Hard corners, outlined, expected behavior.

但是,如果我将 CornerPathEffect 设置为 Paint,如下所示(构造函数)...

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

...这是我得到的:

Rounded corners, outline effect disappears, unexpected behavior?

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

最佳答案

HexagonDrawable 类绘制两个不同的六边形,彼此堆叠。我不知道您是否需要那样做,但我认为实现相同结果的最佳方法是使用 Paint with Stroke 样式。

为此,您需要删除第二个六边形路径并减小六边形的大小(这样 View 就不会剪切它):

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

并为绘画添加描边效果:

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

这应该会产生与您正在寻找的效果非常相似的效果。

希望对您有所帮助! ;)

编辑:我忘了警告你,Stroke 宽度不能大于 CornerPathEffect 的半径,否则它无论如何都会被切割。

关于android - 添加 CornerPathEffect 时 FillType.EVEN_ODD 的不同行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30316845/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com