How to draw the contour of a union of Path

2019-07-21 14:05发布

问题:

Hello i have a pack of Hexagon Path and a want to draw the contour(border) of the union of those Path. I thought of using Region, to union the Path together then get the border resulting Path with getBoundaryPath(), but it draw nothing. So can someone tell me how can I have the contour(border) of the union of all my Path objects ?

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

回答1:

Try using

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

instead of using getBoundaryPath directly.