Andorid DrawPath doesn't fill-up the enclosed area

為{幸葍}努か 提交于 2019-12-12 01:45:20

问题


Following list shows the x,y coordinates and particular method used to create a Path.

0 0 moveTO

180 0 lineTo

180 0 moveTO

246 227, 127 24, 115 150 cubicTo
127 276, 246 71, 180 300 cubicTo

180 300 moveTO

44 261, 166 332, 90 339 cubicTo

14 332, 136 261, 0 300 cubicTo

0 300 moveTO

0 0 lineTo

Raw data points

x = [0, 180, 180, 246, 127, 115, 127, 246, 180, 180, 44, 166, 90, 14, 136, 0, 0, 0]

y = [0, 0, 0, 227, 24, 150, 276, 71, 300, 300, 261, 332, 339, 332, 261, 300, 300, 0]

Idea of this algo is to draw four sides. Two sides are straight lines. Other two sides are composed of bezier curves having seven points in each curve. This is drawn using 1 moveTo and 2 cubicTo methods.

Currently it creates a closed irregular shape if I use Paint.Style.STROKE. When it is drawn on the canvas using Paint.Style.FILL, it fills up only a part of the shape leaving a large square part empty in the middle. Some parts that is been filled are out of the shape closed area.

paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL);
canvas.drawPath(path, paint);

My objective is to draw a bitmap covering this closed area. However, because the fill path does not fill the area correctly my objective cannot be achieved.

Is there a way to realize this.

Thanks


回答1:


Please post your actual code. It's unclear from your formatting if you're calling moveTo or lineTo after arriving at the set of points or before.

EDIT:

The moveTo calls are unnecessary, the lineTo and cubicTo calls already move you to the new point.

Your code:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    paint.setColor(Color.RED);
    paint.setStyle(Paint.Style.FILL);

    p.moveTo(0, 0);
    p.lineTo(180, 0);
    p.moveTo(180, 0);
    p.cubicTo(246, 227, 127, 24, 115, 150);
    p.cubicTo(127, 276, 246, 71, 180, 300);
    p.moveTo(180, 300);
    p.cubicTo(44, 261, 166, 332, 90, 339);
    p.cubicTo(14, 332, 136, 261, 0, 300);
    p.moveTo(0, 300);
    p.lineTo(0, 0);

    canvas.drawPath(p, paint);
}

produced this:

Removing the moveTo calls (and using close() instead of lineTo(0,0)):

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    paint.setColor(Color.RED);
    paint.setStyle(Paint.Style.FILL);

    p.lineTo(180, 0);
    p.cubicTo(246, 227, 127, 24, 115, 150);
    p.cubicTo(127, 276, 246, 71, 180, 300);
    p.cubicTo(44, 261, 166, 332, 90, 339);
    p.cubicTo(14, 332, 136, 261, 0, 300);
    p.close();

    canvas.drawPath(p, paint);
}

produced this:



来源:https://stackoverflow.com/questions/25149934/andorid-drawpath-doesnt-fill-up-the-enclosed-area

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!