贝塞尔曲线和画布 [英] Bezier curve and canvas

查看:41
本文介绍了贝塞尔曲线和画布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在画布上绘制贝塞尔曲线.我只有起点和终点.我想从起点到终点画线.我该怎么做?

How I can draw bezier curve in canvas. I have only start point and end point. I want to draw line from start point to end point. How I can do this?

推荐答案

您可以使用 Path.quadTo()Path.cubicTo() 来解决这个问题.示例可以在 SDK 示例 (FingerPaint) 中找到.在您的情况下,您只需要计算中间点,然后将您的三个点传递给 quadTo()..

You can use Path.quadTo() or Path.cubicTo() for that. Examples can be found in the SDK Examples (FingerPaint). In your case you would simply need to calculate the middle point and pass then your three points to quadTo()..

给你一些代码:

  • (x1,y1)(x3,y3) 分别是您的起点和终点.
  • 只创建一次绘制对象(例如在你的构造函数中)

  • (x1,y1) and (x3,y3) are your starting and ending points respectively.
  • create the paint object only once (e.g. in your constructor)

Paint paint = new Paint() {
    {
        setStyle(Paint.Style.STROKE);
        setStrokeCap(Paint.Cap.ROUND);
        setStrokeWidth(3.0f);
        setAntiAlias(true);
    }
};

final Path path = new Path();
path.moveTo(x1, y1);

final float x2 = (x3 + x1) / 2;
final float y2 = (y3 + y1) / 2;
path.quadTo(x2, y2, x3, y3);
canvas.drawPath(path, paint);

这篇关于贝塞尔曲线和画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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