简体   繁体   English

如何在android的画布上绘制多段线?

[英]How can I draw a polyline on a canvas in android?

I've been trying to draw a polyline on a canvas.我一直在尝试在画布上绘制多段线。 There are no errors in my codes, but the connected sequence of line segments are not showing.Please find code snippet below;我的代码没有错误,但是没有显示连接的线段序列。请在下面找到代码片段;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.View;

public class MyView extends View {
    private Paint redPaint;

    public MyView(Context context) {
        super(context, null);
        redPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        redPaint.setStyle(Paint.Style.STROKE);
        redPaint.setColor(0xffff0000);
        redPaint.setStrokeWidth(5);

    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(10,30,200,200,redPaint);
        canvas.drawCircle(300,300,250,redPaint);

        Path mylines=new Path();
        mylines.moveTo(0,0);
        mylines.lineTo(1,1);
        mylines.lineTo(2,2);
        mylines.lineTo(3,3);
        mylines.lineTo(4,4);
        Paint GreenPaint=new Paint();
        GreenPaint.setARGB(255,0,255,0);
        canvas.drawPath(mylines,GreenPaint);

    }
}

Your code is largely correct.您的代码在很大程度上是正确的。 You do not completely initialized GreenPaint like you do redPaint and that is a problem.你不完全初始化GreenPaint像你这样redPaint ,这是一个问题。 The second issue, although it may not be a problem, is that your polyline shape is so small that you may miss it even with a fully initialized GreenPaint .第二个问题,虽然它可能不是问题,但您的折线形状太小以至于即使使用完全初始化的GreenPaint也可能会错过它。

Here is an updated version of your custom view with an additional constructor and an initialized greenPaint .这是自定义视图的更新版本,带有一个额外的构造函数和一个初始化的greenPaint I also changed the shape of the polyline and made it larger to be easily seen - it is just a speck in your code.我还更改了多段线的形状并使其变大以便于查看 - 它只是您代码中的一个斑点。 In addition I moved object allocation out of onDraw() .此外,我将对象分配移出了onDraw()

    public class MyView extends View {
    Path mylines = new Path();
    private Paint redPaint;
    private Paint greenPaint;

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

    public MyView(Context context) {
        super(context, null);
        init();
    }

    private void init() {
        redPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        redPaint.setStyle(Paint.Style.STROKE);
        redPaint.setColor(0xffff0000);
        redPaint.setStrokeWidth(5);

        greenPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        greenPaint.setStyle(Paint.Style.STROKE);
        greenPaint.setARGB(255, 0, 255, 0);
        greenPaint.setStrokeWidth(5);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(10, 30, 200, 200, redPaint);
        canvas.drawCircle(300, 300, 250, redPaint);

        mylines.moveTo(0, 0);
        mylines.lineTo(200, 50);
        mylines.lineTo(300, 150);
        mylines.lineTo(400, 250);
        mylines.lineTo(500, 300);

        canvas.drawPath(mylines, greenPaint);

    }
}

Here is the display.这是显示器。 (I added a gray background for visibility of the view but it is not needed.) (我为视图的可见性添加了灰色背景,但不需要。)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM