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

前端 未结 2 1046
伪装坚强ぢ
伪装坚强ぢ 2020-12-21 09:21

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);
black         


        
相关标签:
2条回答
  • 2020-12-21 10:16

    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.)

    0 讨论(0)
  • 2020-12-21 10:22

    You can't draw lines less than a pixel in width without anti-aliasing.

    The whole point of anti-aliasing is to calculate what a pixel's colour would be, if a element doesn't completely fill the pixel (Like a line that's only a tenth of a pixel's width).

    You could somewhat simulate it by drawing a lighter line, but that still wouldn't come close to a actually anti-aliased line.

    Take a look at this image, for example:

    enter image description here

    It's really not possible to replicate the anti-aliased result by changing the line's colour.

    I realise the lines in the example are > 1 pixel in width, but the same principle applies. The "darkness" of a pixel is calculated as a result of how much of the pixel is filled with the line. For that reason, a solid grey line won't work.

    0 讨论(0)
提交回复
热议问题