Android:如何组合两个形状的路径并删除重叠?

时间:2010-09-30 19:43:26

标签: android graphics path

我想创建一个语音气球类型的形状,其中有一个矩形或椭圆形,其中突出了三角形。

我如何尝试这样做是为了创建一个Path对象,它将三角形与另一个形状组合在一起(圆形矩形)。

我这样做:

Path path = new Path();
// Create triangular segment

Point drawOffset = getAttributes().getDrawOffset();
int leaderGap = getAttributes().getLeaderGapWidth();

// Recall that we have a coordinate system where (0,0) is the
// bottom midpoint of the annotation rectangle.

// the point to left of gap
int x1 = -leaderGap/2;
int y1 = 0;

// the point to right of gap
int x2 = leaderGap/2;
int y2 = 0;

// The point where we're drawing to; the end of the pointy segment of triangle
int x3 = -drawOffset.x;
int y3 = drawOffset.y;

path.moveTo(x2, y2);
path.lineTo(x3, y3);
path.lineTo(x1, y1);
// path.close();


// Add the rectangular portion to the path
path.addRoundRect(backgroundShape, 5, 5, Path.Direction.CW);

问题是roundRect是一条封闭的路径,因此它的边缘显示在三角形截面的下方。

一张图片胜过千言万语,所以你走了:

Rounded balloon

我想要的是三角形的这两个端点之间的线段消失,所以它看起来像一条无缝路径。

如果我所做的只是一个直的矩形,我可以自己创建整个路径。但是我想做圆角,用Path做这件事是有点油漆的(是的,我知道你可以做四边形和弧线但是它仍然没有像我那样干净的解决方案等)。

一般来说,是否可以组合两个路径并创建一个跟踪两者周长的联合对象?

2 个答案:

答案 0 :(得分:0)

我看不到以这种方式组合路径的便捷方法。我通常会通过使用Path.arcTo()和Path.lineTo()手动绘制圆角矩形部分来实现这一点,这需要额外的努力,但它会达到你想要的结果。

如果您决定更改主题,可能更灵活的另一个选项是使用ninepatch drawable,甚至还有一个编辑器可以创建名为Draw 9-patch

的编辑器

答案 1 :(得分:0)

从API 19开始,这是可能的。您可以在两个路径之间执行逻辑操作。与您的情况一样,您可以使用Path.op()创建两个路径的UNION。有关可执行操作的完整列表,请参阅here