【问题标题】:Android: how to combine the paths of two shapes and remove overlap?Android:如何组合两个形状的路径并消除重叠?
【发布时间】:2011-04-19 13:16:06
【问题描述】:

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

我尝试这样做的方法是创建一个 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是一个封闭的路径,所以它的边缘从三角形部分的下方显示出来。

一张图片胜过一千个字,所以你去:

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

如果我所做的只是一个直矩形,我可以自己从头开始创建整个路径。但我想做圆角,用 Path 来做这件事有点费力(是的,我知道你可以做 quad to 和 arcTo,但它仍然不像我想的那样干净喜欢)。

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

【问题讨论】:

    标签: android graphics path


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-08
        • 2020-07-16
        相关资源
        最近更新 更多