在画布上同一位置绘制多次会使其颜色更饱和。

huangapple go评论71阅读模式
英文:

Drawing line on canvas in the same position multiple times makes it more saturated

问题

我遇到了一个非常奇怪的绘图问题。
我想要创建PDF,为此我正在使用一个提供画布供我绘制的Android本地PDF API。
不久之后,我注意到在同一位置多次绘制线条或矩形会比只绘制一次的线条更饱和。
以下是我的测试代码:

        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(0.5f);
        paint.setARGB(255,0,0,0);

        for(int i = 0;i < 1000;i++)
            canvas.drawLine(0, 10, PageRectangle.A4.getPointX(), 10, paint);

        canvas.drawLine(0, 20, PageRectangle.A4.getPointX(), 20, paint);

这里是结果链接

有没有办法消除这种饱和度?

英文:

I have encountered a really weird drawing problem.
I want to create PDF and for that, I am using an android native PDF API that gives me a canvas to draw on.
After a while, I noticed that drawing line or rectangle several times in the same place is more saturated than a one that has been drawn once.
Here is my test code:

        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(0.5f);
        paint.setARGB(255,0,0,0);

        for(int i = 0;i &lt; 1000;i++)
            canvas.drawLine(0, 10, PageRectangle.A4.getPointX(), 10, paint);

        canvas.drawLine(0, 20, PageRectangle.A4.getPointX(), 20, paint);

Here are the results

Is there a way to remove this saturation?

答案1

得分: 2

经过一些研究,我发现由PdfDocument.Page提供的Canvas并不使用像素进行绘制,而是使用PostScript点进行绘制。以下是将像素转换为点的代码:

/**
* 将像素转换为PostScript点
* @param px 要转换的像素
* @return PostScript点
*/
public float convert(float px){
    return (px+0.5f) * 72f / 96;
}
英文:

After some research, I found out that Canvas provided by PdfDocument.Page does not draw in pixels, it draws in PostScript Points. This is the code that converts pixels to points:

/**
* Converting pixels to PostScript Points
* @param px pixels to convert
* @return PostScript Points
*/
public float convert(float px){
    return (px+0.5f) * 72f / 96;
}

答案2

得分: 0

由于可伸缩的浮点几何,这是由于线条将穿过像素的部分,因此该像素会具有一定的透明度。

对于半个像素的红线:50% 的红色,下一次为 75% 的红色,依此类推。

也许您可以通过 Graphics2D 的“hinting”(将其移到像素边界)来减少这种效果,但它永远不会完全消失。(setStrokeWidth(0.5f)

英文:

That is due to scalable floating point geometry. A line will go through parts of pixels and thus there will be some transparency to such a pixel.

For half a pixel with a red line: 50% red, next time 75% red and so on.

Maybe you can reduce the effect by Graphics2D hinting (to shift to pixel borders), but it will never get completely lost. (setStrokeWidth(0.5f))

huangapple
  • 本文由 发表于 2020年8月19日 21:06:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63487649.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定