Bezier curve and canvas

我的未来我决定 提交于 2019-11-26 17:39:10

问题


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?


回答1:


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()..

Some code for you:

  • (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);
    



回答2:


With Path you can draw cubic and quadratic bezier curves. See cubicTo() and quadTo()




回答3:


You need to set color...your code is correct and change x3,x3 to x3,y3 in the quadTO().



来源:https://stackoverflow.com/questions/9993030/bezier-curve-and-canvas

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