16

我需要在我的项目中创建贝塞尔曲线。为此,我正在用油漆绘制视图,但问题是我没有得到满足我需要的确切形状,如下图所示。因此,请帮助我解决您的解决方案以及我的代码中的更改或修改。提前致谢。

我用来创建贝塞尔曲线的代码:

public class DrawView extends View {

    public DrawView (Context context) {
        super (context);
    }

    protected void onDraw (Canvas canvas) {
        super.onDraw (canvas);

        Paint pLine = new Paint () {{
            setStyle (Paint.Style.STROKE);
            setAntiAlias (true);
            setStrokeWidth (1.5f);
            setColor (Color.RED); // Line color
        }};

        Paint pLineBorder = new Paint () {{
            setStyle (Paint.Style.STROKE);
            setAntiAlias (true);
            setStrokeWidth (3.0f);
            setStrokeCap (Cap.ROUND);
            setColor (Color.RED); // Darker version of the color
        }};
        Path p = new Path ();
        Point mid = new Point ();
        // ...
        Point start =new Point (30,90);
        Point end =new Point (canvas.getWidth ()-30,140);
        mid.set ((start.x + end.x) / 2, (start.y + end.y) / 2);

        // Draw line connecting the two points:
        p.reset ();
        p.moveTo (start.x, start.y);
        p.quadTo ((start.x + mid.x) / 2, start.y, mid.x, mid.y);
        p.quadTo ((mid.x + end.x) / 2, end.y, end.x, end.y);

        canvas.drawPath (p, pLineBorder);
        canvas.drawPath (p, pLine);
    }
}

主要活动

public class MainActivity extends Activity {

    private DrawView drawView;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        drawView = new DrawView (this);
        setContentView (drawView);

    }
}

我的实际需求:

在此处输入图像描述

我得到的输出:

在此处输入图像描述

4

2 回答 2

32

经过长时间的斗争,我从头开始找到了问题的根本原因。感谢帮助我生成坐标的工具以及向我展示了我需要的确切样本的博客。最后我的代码如下:

public class DrawView extends View {

    Paint paint;
    Path path;

    public DrawView(Context context) {
        super(context);
        init();
    }

    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DrawView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init(){
        paint = new Paint();

        paint.setStyle(Paint.Style.STROKE);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        path = new Path();
        paint.setColor(Color.RED);
        paint.setStrokeWidth(3);
        path.moveTo(34, 259);
        path.cubicTo(68, 151, 286, 350, 336, 252);
        canvas.drawPath(path, paint);

    }
于 2015-05-10T07:19:39.613 回答
1

你还没有关闭你的路径,也没有为你的绘画设置颜色。

于 2015-05-06T10:13:01.140 回答