Canvas.drawLines()在线条之间产生间隙

时间:2019-03-24 21:43:35

标签: android canvas

我在我的点数据集上使用drawLines()。当它从(0,0)开始时绘制完美,但是当数据集从另一个点开始时会产生一些间隙。

此处的图表以(0,0)开头(在左下角添加了一条小线):

enter image description here

此处从(83,56)开始并产生间隙:

enter image description here

我尝试过的事情:

  1. 设置抗锯齿功能。
  2. 不同的数据集(如果在(0,0)处未绘制任何内容,它们都将在y顶部产生间隙。
  3. 我已经读过有关尝试drawPath()的信息,但是由于我需要画很多线,因此据说效率不如drawLines()

以下是我的项目中的摘要:

我的数据集:
x = { 0,12,83,84,84,121,121,128,128,151,151,173,173,203,203,217,217,224,224,229,229,253,253,294,294,305,305,331,331,355,355,364,364,409,409,411,411,416,416,431,431,448,448,497,497,504,504,508,508,536,536,582,582,586,586,630,630,646,646,660,660,689,689,728,728,761,761,768,768,798,798,822,822,860,860,894,894,908,908,952,952,996,996,1039,1039,1085,1085,1085,1085,1099,1099,1119,1119,1133,1133,1169 }

y = { 0,12,56,115,115,220,220,232,232,170,170,350,350,117,117,157,157,205,205,290,290,184,184,127,127,181,181,231,231,210,210,278,278,142,142,120,120,299,299,29,29,290,290,50,50,258,258,127,127,203,203,168,168,27,27,27,27,83,83,116,116,228,228,295,295,62,62,299,299,121,121,216,216,266,266,164,164,234,234,116,116,182,182,130,130,208,208,218,218,202,202,85,85,59,59,114 }

我如何设置绘画:

private void init() {
    chartPaint = new Paint();
    chartPaint.setStyle(Paint.Style.STROKE);
    chartPaint.setStrokeWidth(4);
    chartPaint.setColor(color);
}

我的onDraw():

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

    canvas.save();

    // let chart start from the most left without gaps
    canvas.translate(0, viewHeight);

    // TODO: find place to move calcs outside onDraw()
    final float xScale = (float)  viewWidth / (Util.getMax(x) - Util.getMin(x));
    final float yScale = (float) -viewHeight / (Util.getMax(y) - Util.getMin(y));

    valuesScaled = new float[values.length];

    for (int i = 0; i < values.length; i++) {
        valuesScaled[i] = (i%2==0) ? values[i] * xScale : values[i] * yScale;
    }

    canvas.drawLines(valuesScaled, chartPaint);
    canvas.restore();
}

values数组是xy数组交替存储的地方,即values的前4个元素是{x[0], y[0], x[1], y[1], ...}

viewWidthviewHeight来自onSizeChange()

0 个答案:

没有答案