Why doesn't Paint.ANTI_ALIAS_FLAG seem to work when drawing in the same place on Canvas?

There is a problem with anti-alias when trying to draw several times in one place using android Canvas .

First, I initialize paint = new Paint(Paint.ANTI_ALIAS_FLAG) , and then set the maximum stroke length in Paint.Cap.ROUND .

Then, if I call canvas.drawPoint(x, y, paint) , it calls the following result once:

enter image description here

When calling canvas.drawPoint(x, y, paint) several times (100 in this example), it calls the following:

enter image description here

I created an example with minimal code to run this on GitHub: android-canvas-antialias

I noticed that if I draw points with a certain distance, the anti-alias works as expected (first image). But when drawing with a slight offset, the same problem arises with an alias (second image).

Is there any setting that needs to be made for this to work when drawing dots in one place? Or I just can’t paint in one place (or with a very slight offset)?

EDIT: The real problem is that I am trying to draw a line segment of variable width. See MainActivity.drawSegment in the git repository.

+6
source share
2 answers

I do not think this is a problem, I mean, the error itself. And even if solvable is trivial, I think.

The pixels of the edge of the circle are drawing with some alpha, for example. a red pixel with 25% alpha, if you overlay it with 3 pixels with the same alpha, you get a 100% red pixel.

A workaround will manage all the shapes created and check if some of them have the same position + (maybe the color too) and just draw one of them.

The link below explains how anti-aliasing works.

http://web.cs.wpi.edu/~matt/courses/cs563/talks/antialiasing/methods.html

+4
source

It is working correctly. Smoothing is when the edges of a form are translucent. When you multiply shapes, translucent pixels become opaque, and you get “dangling” edges.

The solution is not for this.

+3
source

All Articles