android - 如何根据超过 3 个点的控制点列表绘制曲线

标签 android

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

这里是控制点: (0,90) (1,100) (-3,145) (10,150) (23,155) (73,108) (80,120) (86,131) (40,210) (50,220) (60,230) (148,185) (140,180) (131,175) (23,188) (0,190)

这是我的代码:

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

出了什么问题,我怎样才能画出正确的曲线?

最佳答案

我已经通过下面的代码解决了这个问题:

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

关于android - 如何根据超过 3 个点的控制点列表绘制曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40915250/

相关文章:

android - 在布局中将 ShareActionProvider 与按钮一起使用

android - 当用户按下设备音量键时,seekbar 拇指位置发生变化

android - 只有在 GridLayout 中显示 ViewGroup 的第一个 subview - 后续单元格在视觉上是无 subview 的

android - 将数据发送到 WebView 中加载的页面

java - 使用 Java 和 Google 的 Firestore 运行查询时没有输出

android - 如何在 Android 11 上运行 Boot Broadcast Receiver

android - 在组件和应用程序之间传递数据

android - 如何放大 Android 图像而不使其模糊?

使用首选项管理器时出现 java.lang.NullPointerException

android - ObjectAnimator 不淡入