Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中绘制Bezier曲线_Android_Android Canvas_Bezier_Best Fit Curve - Fatal编程技术网

如何在Android中绘制Bezier曲线

如何在Android中绘制Bezier曲线,android,android-canvas,bezier,best-fit-curve,Android,Android Canvas,Bezier,Best Fit Curve,我有一个在我的项目中创建贝塞尔曲线的要求。出于这个目的,我用油漆画了一个视图,但问题是我没有得到下图中提到的我需要的确切形状。因此,请帮助我与您的解决方案和更改或修改我的代码。提前谢谢 我用来创建贝塞尔曲线的代码: public class DrawView extends View { public DrawView (Context context) { super (context); } protected void onDraw (Canva

我有一个在我的项目中创建贝塞尔曲线的要求。出于这个目的,我用油漆画了一个视图,但问题是我没有得到下图中提到的我需要的确切形状。因此,请帮助我与您的解决方案和更改或修改我的代码。提前谢谢

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

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);
    }
}
main活动

public class MainActivity extends Activity {

    private DrawView drawView;

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

    }
}
我的实际需要:

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

    }

博客和工具就是答案!使用path.reset()代替创建新的路径使用
倒带()感谢您在此处突出显示的工具。。。伟大的作品:)。。。