你好,我有一组六边形路径,想要绘制这些路径的并集的轮廓(边界)。我想过使用 Region 将 Path 联合在一起,然后使用 getBoundaryPath() 获取边框结果 Path,但它什么也没绘制。 那么有人可以告诉我如何获得所有 Path 对象的并集的轮廓(边界)吗?
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// if (pmBack != null) {
// canvas.drawBitmap(pmBack, new Matrix(), paint);
// }
if (mCells != null) {
for (int i = 0; i < mCells.length; i++) {
final HexCell cell = mCells[i];
if (cell != null) {
final PointF p = cell.getDrawPoint();
paint.setColor(cell.mColor);
Path path = drawHexagon(cell.mSize, mCenterX + p.x, mCenterY + p.y);
canvas.drawPath(drawHexagon(cell.mSize, mCenterX + p.x, mCenterY + p.y), paint);
if (i == 0) {
region.setPath(path, mClip);
} else {
region2.setPath(path, mClip);
region.op(region2, Op.UNION);
}
}
}
canvas.drawPath(region.getBoundaryPath(), paintContour);
}
}
private Path drawHexagon(final float size, float centerX, float centerY) {
Path path = new Path();
for (int i = 0; i <= 6; i++) {
double angle = 2 * Math.PI / 6 * (i + 0.5);
float x_i = (float) (centerX + size * Math.cos(angle));
float y_i = (float) (centerY + size * Math.sin(angle));
if (i == 0) {
path.moveTo(x_i, y_i);
} else {
path.lineTo(x_i, y_i);
}
}
return path;
}
最佳答案
尝试使用
Path outline = region.getBoundaryPath();
Path newpath = new Path();
Matrix matrix = new Matrix();
matrix.setScale(1, 1, 0, 0);
outline.transform(matrix,newpath);
canvas.drawPath(newpath, paint);
而不是直接使用getBoundaryPath。
关于java - 如何绘制 Path 并集的轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19115283/