如何根据具有3个以上点的控制点列表绘制曲线

时间:2016-12-01 16:05:23

标签: android

我想根据控制点列表绘制曲线。 这是我的预期: enter image description here

以下是控制点: (0,90) (1100) (-3,145) (10150) (23155) (73108) (80120) (86131) (40210) (50220) (60230) (148185) (140180) (131175) (23188) (0190)

这是我的代码:

public List<PointType> controlPoints;
public void render(Canvas canvas, Paint paint) {
        int size = controlPoints.size();
        if (size < 2) {
            return;
        }

        paint.setColor(this.color);
        paint.setStyle(this.style);

        Path curvePath = new Path();
        PointType firstPoint = null;
        PointType beginPoint = null;

        for (PointType point : controlPoints) {
            if (firstPoint == null) {
                firstPoint = point;
            } else if (beginPoint == null) {
                beginPoint = point;
            } else {
                curvePath.moveTo(firstPoint.x, firstPoint.y);
                curvePath.quadTo(beginPoint.x, beginPoint.y, point.x, point.y);
                firstPoint = beginPoint;
                beginPoint = point;
            }
        }

        canvas.drawPath(curvePath, paint);
    }

但结果如下:

enter image description here

出了什么问题,如何绘制正确的曲线?

1 个答案:

答案 0 :(得分:1)

我已通过以下代码解决了问题:

public void render(Canvas canvas, Paint paint) {
        int size = controlPoints.size();
        if (size < 2) {
            return;
        }

        paint.setColor(this.color);
        paint.setStyle(this.style);

        Path curvePath = new Path();
        curvePath.moveTo(controlPoints.get(0).x, controlPoints.get(0).y);
        for (int idx = 1; idx < controlPoints.size(); idx += 3) {
            curvePath.cubicTo(controlPoints.get(idx).x,
                    controlPoints.get(idx).y, controlPoints.get(idx+1).x,
                    controlPoints.get(idx+1).y, controlPoints.get(idx+2).x,
                    controlPoints.get(idx+2).y);
        }

        canvas.drawPath(curvePath, paint);
    }
相关问题