简体   繁体   English

绘图线小于1像素粗需要Android 4.2中的抗锯齿

[英]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. 我正在尝试在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. 这在Android 2.2上可以正常使用,但是当我在4.2(也可以是4.1,我认为-我已经简短测试过了-我没有测试过2.2、4.1.2和4.2之外的其他任何版本)中尝试过这些行时,当我画它们时出现。 To make them show up in 4.2, it seems like I have to set the anti-aliasing flag to be true. 为了使它们出现在4.2中,似乎我必须将抗锯齿标志设置为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? 这是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. 编辑:我也用0.9像素大小测试了这一点,问题是相同的。

Thanks for the help! 谢谢您的帮助!

If you want a very thin line, use setStrokeWidth(0) , as the documentation says Pass 0 to stroke in hairline mode. 如果您想要很细的线,请使用setStrokeWidth(0) ,因为文档Pass 0 to stroke in hairline mode.

Sub-pixel drawing obviously requires Anitaliasing or other workarounds. 亚像素绘图显然需要Anitaliasing或其他解决方法。 It is pretty possible that eg 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. 例如,使用软件渲染时,很有可能总是将一像素以下的线绘制为细线,而使用硬件渲染(在Android 4.2中可能会自动使用,而在2.2中不会自动使用),行为会发生变化,许多便利功能会消失。 (This is often the reason for drawing differences and weird glitches, btw.) (顺便说一句,这通常是造成差异和怪异故障的原因。)

You can't draw lines less than a pixel in width without anti-aliasing. 如果没有抗锯齿,则不能绘制width小于一个像素的线条。

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: 看一下这个图像,例如:

在此处输入图片说明

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. 我意识到示例中的线宽> 1像素,但原理相同。 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. 因此,灰色实线将不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM