如何在android中绘制曲线

时间:2016-10-19 07:09:59

标签: android canvas

我想像这样画一条曲线required output。但我得到了输出 像这样current output。这里的蓝色是背景&曲线是红色。我的代码。

for (i = 10; i <= 360; i = i + 10) {
    new_x = i;
    new_y = (float) Math.sin(new_x / 180.0 * Math.PI);

    canvas.drawLine((float) (old_x / 360.0 * w), 100 + 90 * old_y, (float) (new_x / 360.0 * w), 100 + 90 * new_y, paint);

    old_x = new_x;
    old_y = new_y;
}

1 个答案:

答案 0 :(得分:0)

不是每次调用drawline,而是使用drawPath可能更合理: 即

Path path = new Path();
boolean first = true;
for (i = 10; i <= 360; i = i + 10) {
    new_x = i;
    new_y = (float) Math.sin(new_x / 180.0 * Math.PI);


    if (first) {
        first = false;
        path.moveTo(new_x, new_y);
    }
    else{
        path.lineTo(new_x, new_y);
    }
}
canvas.drawPath(path, paint);

然后尝试使用quadTo而不是lineTo,对于中间的点来平滑它:https://developer.android.com/reference/android/graphics/Path.html#quadTo(float,float,float,float)