Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

canvas - Drawing line less than one pixel thick requires anti-aliasing in Android 4.2

I'm trying to draw a very thin line (less than one pixel thick) in android. I'm using

Paint blackThin = new Paint();
blackThin.setStrokeWidth(0.1f);
blackThin.setColor(Color.BLACK);

to initialize the paint object.

This works fine in Android 2.2, but when I try it in 4.2 (also 4.1, I think - I tested that one briefly - I haven't tested any other versions other that 2.2, 4.1.2 and 4.2) the lines won't show up when I draw them. To make them show up in 4.2, it seems like I have to set the anti-aliasing flag to be true. (I tried that, and the lines showed up.) But I really don't want that, because that blurs the lines and such.

Is this a bug in Android? Is there something I can do to make it work other than turning on anti-aliasing?

EDIT: I also tested this with a pixel size of 0.9, and the problem is the same.

Thanks for the help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want a very thin line, use setStrokeWidth(0), as the documentation says Pass 0 to stroke in hairline mode.

Sub-pixel drawing obviously requires Anitaliasing or other workarounds. It is pretty possible that e.g. with software rendering, lines below one pixel are always drawn as hairlines, but with hardware rendering (which may be automatically used in Android 4.2 but not in 2.2), the behavior changes and many of the convenient features disappear. (This is often the reason for drawing differences and weird glitches, btw.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...