令人困惑的 PNG 条带解决方案

发布于 2024-10-16 08:57:48 字数 576 浏览 2 评论 0原文

我遇到了 PNG 文件的条带问题。深入研究这个问题得出了两种解决方案。两者单独来看都有意义,但放在一起就没有意义了。我发现的解决方案:

1)将 PNG 文件移动到“raw”文件夹中。这可以防止 AAPT“优化”图像,从而导致出现条带。

2) 将 Activity 窗口的像素格式更改为 RGBA_8888(即在 onCreate 中添加此行“getWindow().setFormat(PixelFormat.RGBA_8888)”)。在 Android 2.2 及更低版本上,默认像素格式为 16 位 (565)。

我已经尝试过这两种方法,它们纠正了图像中的条带效应,但现在我对 Android 正在做什么更加困惑。

一方面,如果我将 PNG 留在可绘制文件夹中,它会被“优化”,从而导致图像中出现条带效果。当我将像素格式更改为 32 位时,它神奇地消失了。如果图像被“优化”,我预计条带会保留下来。

另一方面,如果我将 PNG 移动到 raw 文件夹,即使 PixelFormat 应该是 16 位,它也会保留漂亮的渐变并很好地显示。

如果有人对正在发生的事情有任何见解,我将不胜感激。

谢谢,

-丹

I've run into issues with banding of my PNG files. Digging into the problem has yielded two solutions. Both make sense individually, but together they don't. The solutions I've discovered:

1) Move the PNG file into the "raw" folder. This prevents AAPT from "optimizing" the image which results in banding.

2) Change the pixel format of your Activity's window to RGBA_8888 (i.e. in onCreate add this line "getWindow().setFormat(PixelFormat.RGBA_8888)"). On Android 2.2 and lower the default pixel format is 16-bit (565).

I have tried both of these and they correct the banding effect in my images, however now I am even more confused as to what Android is doing.

On the one hand, if I leave my PNG in the drawable folder it is "optimized" which results in a banding effect in the image. It magically goes away when I change the pixel format to 32-bit. If the image was "optimized" though, I would have expected the banding to remain.

On the other hand, if I move the PNG to the raw folder it will retain the nice gradient and display nicely even though the pixelFormat is supposedly 16-bit.

If anyone has any insight into what is going on I would appreciate it.

Thanks,

-Dan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦途 2024-10-23 08:57:48

我相信它很简单:

您必须将 Activity 的像素格式(RGBA_8888)视为位图的默认优化。

如果未设置,则在 2.2 之前,默认情况下会将位图压缩为 RGB_565。

但是,如果您要以编程方式创建位图并将其设置为 RGBA_8888,那么应用程序将照此使用它。

当您将位图放入原始文件夹时,同样适用:即使默认 PixelFormat 设置为 RGB_565,活动也会按原样使用它,而不会“优化”它。

当您将位图放入 raw 文件夹时,即使默认 PixelFormat 仍为 RGB_565,它也不会被压缩并按原样使用。

I believe its quite simple :

You have to think of the pixel format of your Activity(RGBA_8888) as a DEFAULT optimization for your bitmaps.

If it is not set, then prior to 2.2, by default it will compress your bitmap to RGB_565.

But if you were to create programmatically a bitmap and set it to RGBA_8888, then it would be used as such by the app.

Same applies when you put your bitmap in the raw folder : Even though the default PixelFormat is set to RGB_565, the activity will use it as it is without "optimizing" it.

When you put your bitmap in the raw folder it will not be compressed at all and used as is even though the default PixelFormat is still RGB_565.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文