Andorid DrawPath不会填满封闭区域 [英] Andorid DrawPath doesn't fill-up the enclosed area

查看:687
本文介绍了Andorid DrawPath不会填满封闭区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下列表显示了x,y坐标以及用于创建路径的特定方法.

0 0 moveTO

180 0行至

180 0 moveTO

246 227、127 24、115150立方到127276、246 71、180300
cubeTo

180300 moveTO

44 261、166 332、90 339立方英尺

14 332、136 261、0 300立方英尺

0 300 moveTO

0 0 lineTo

原始数据点

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]

此算法的构想是绘制四个面.两侧是直线.其他两个方面 由贝塞尔曲线组成,每条曲线中有七个点.这是使用1 moveTo和2cubicTo方法绘制的.

当前,如果我使用Paint.Style.STROKE,它将创建一个封闭的不规则形状.当使用Paint.Style.FILL将其绘制在画布上时,它仅填充形状的一部分,而在中间留下一个大的方形部分.某些已填充的零件不在形状封闭区域内.

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

我的目标是绘制一个覆盖此封闭区域的位图.但是,由于填充路径无法正确填充区域,因此无法实现我的目标.

有没有办法实现这一目标.

谢谢

解决方案

请发布您的实际代码.从格式上不清楚,在到达点集之前还是之前要调用moveTo或lineTo.

moveTo调用是不必要的,lineTo和cubicTo调用已经将您移至新点.

您的代码:

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

产生了这个:

删除moveTo调用(并使用close()代替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);
}

产生了这个:

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

解决方案

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:

这篇关于Andorid DrawPath不会填满封闭区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆