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

标签 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 做这件事有点麻烦(是的,我知道你可以做 quad to 和 arcTo,但它仍然不像我想的那样干净喜欢)。

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

最佳答案

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

关于Android:如何合并两个形状的路径并去除重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3834227/

相关文章:

android - 我可以在 Android ADB shell 中编辑文件吗?

java - java.awt.Graphics 中的方法在哪里定义?

c# 填充除 GraphicsPath 以外的所有内容

R 图形 : output to several file formats

android - SimpleCursorAdapter中如何绑定(bind)联系人姓名、电话、联系人照片?

android - Canvas get size 随着不同设备屏幕尺寸的变化而变化

java - 如何使用 Eclipse 启动将 native 库添加到 "java.library.path"(而不是覆盖它)

c# - 选择输出目录更改数据库文件路径

file - <input type =‘file’ > for IE 给出完整路径,只需要文件名

android - 如何在 Android 中自动接受 Wi-Fi Direct 连接请求