如何在Android中的Canvas上绘制锐利的曲线和斜角线

时间:2015-07-21 05:33:19

标签: android canvas drawing

下图显示了Canvas的输出。曲线和斜线不像水平或垂直直线那样尖锐。我android:hardwareAccelerated="true"也开启了ANTI_ALIAS_FLAGCanvas。请参阅下面的代码。如何改善这些曲线的质量(与直线相同)?我使用的是Nexus S.有没有办法提高Canvas的分辨率。

Screenshot of result

enter image description here

结果的特写

Closeup of issue

Path path1 = new Path();
Path path2 = new Path();
public Paint fillPaint = null;
// called in constructor
public void createPath()
{
    //path 1 samll one
    Point[] araay = new Point[]{new Point(36,72),new Point(29,46), new Point(41,70),new Point(42,54),new Point(41,38),new Point(29,64),new Point(36,36)};
    AddBeziers(path1, araay, 72, 36);

    AddLine(path1,72, 36 );
    AddLine(path1, 72, 72 );

    AddLine(path1, 36, 72 );

    //path 2 Big one
    araay = new Point[]{new Point(144,320),new Point(109,200), new Point(171,308),new Point(178,240),new Point(171,172),new Point(109,282),new Point(144,160)};
    AddBeziers(path2, araay, 320, 144);
    AddLine(path2, 216, 144 );
    AddLine(path2, 216, 216 );
    AddLine(path2, 144, 320);

    fillPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    fillPaint.setColor(Color.WHITE);
    fillPaint.setFlags(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
    fillPaint.setAntiAlias(true);
    fillPaint.setDither(true);
    fillPaint.setStrokeJoin(Paint.Join.ROUND);
    fillPaint.setStrokeCap(Paint.Cap.ROUND);
    fillPaint.setStyle(Paint.Style.FILL);

}

 // add lines to the path
protected Path AddLine(Path path, int endX, int endY) {
    //path.moveTo(startX, startY);

    path.lineTo(endX, endY);
    return path;
}

// add curves to the path
protected Path AddBeziers(Path path, Point[] points, int lastX, int lastY) {

    if (points[0].X != lastX && points[0].Y != lastY)
        path.moveTo(points[0].X, points[0].Y);

    int index = 1;

    path.cubicTo(points[index].X, points[index].Y, points[index + 1].X,
        points[index + 1].Y, points[index + 2].X, points[index + 2].Y);
    index = index + 3;
    path.cubicTo(points[index].X, points[index].Y, points[index + 1].X,
        points[index + 1].Y, points[index + 2].X, points[index + 2].Y);

    return path;
}

//draw on canvas
@Override
public void onDraw(Canvas canvas) {

    canvas.drawPath(path1, fillPaint);
    canvas.drawPath(path2, fillPaint);
    super.onDraw(canvas);
}

enter image description here

1 个答案:

答案 0 :(得分:1)

  

有没有办法提高Canvas的分辨率。

您可以为任意大小的Canvas创建Bitmap,使用Canvas绘制内容,最后将Bitmap绘制到您的初始Canvas 1}}以缩放分辨率,以适应屏幕上的所有内容。

为什么你不应该这样做

您设备的屏幕,以及世界上任何屏幕,都有正方形网格中的方形像素(不用担心子像素)。这意味着既不是水平线也不是垂直线的线总是会有伪影。像你一样使用抗锯齿是减轻视觉伪像的常用技术,但它们仍然存在。

提高Canvas的分辨率对您没有任何帮助,因为Canvas上的单个像素已经映射到设备屏幕上的单个像素。

在我看来,你太担心了。当以常规眼距在设备屏幕上查看结果时,边缘的不清晰度不明显。当您捕获设备屏幕内容的屏幕截图并在更大的屏幕上检查时,边缘的不清晰度几乎不可察觉,当然也不是我会担心的任何事情。当你放大时,任何人都可以看到它,但是你不再期待锋利的边缘。