白不是白

发布于 2024-10-25 21:38:10 字数 600 浏览 1 评论 0原文

在处理 Android 中的一些位图时,我注意到视图中使用的白色并不总是与位图上渲染的白色相同。考虑这个屏幕截图。

在此处输入图像描述

背景白色来自具有白色背景颜色的视图。

前景“白色”来自从 SD 卡解码的白色位图,显示在 ImageView 中。此位图使用 RGB_565 进行解码,如下所示:

BitmapFactory.Options resample = new BitmapFactory.Options();
resample.inPreferredConfig = Config.RGB_565;
resample.inSampleSize = sampleSize;
return BitmapFactory.decodeFile(filePath, resample);

作为参考,此处 是位图。

这是为什么?如何解决?

When dealing with some bitmaps in Android I noticed that the white used in views is not always the same white rendered on bitmaps. Consider this screenshot.

enter image description here

The background white is from a view with white background color.

The foreground "white" is from a white bitmap decoded from the SD card, displayed in an ImageView. This bitmap is decoded using RGB_565 as follows:

BitmapFactory.Options resample = new BitmapFactory.Options();
resample.inPreferredConfig = Config.RGB_565;
resample.inSampleSize = sampleSize;
return BitmapFactory.decodeFile(filePath, resample);

For reference, here's the bitmap.

Why is this and how can it be fixed?

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

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

发布评论

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

评论(6

灵芸 2024-11-01 21:38:10

我有同样的问题,经过一些实验,我注意到注释 解决了问题。任何高于 3 的 android:minSdkVersion 值都会出现这种效果(删除 标记会有效地将 minSdkVersion 更改为 1。

I have the same issue and after some experiments I noticed that commenting <uses-sdk> solves the problem. Any value for android:minSdkVersion above 3 will make this effect appear (removing <uses-sdk> tag effectively changes minSdkVersion to 1.

独木成林 2024-11-01 21:38:10

尝试在 onCreate 中设置 getWindow().setFormat(PixelFormat.RGB_565)

默认格式似乎会根据 SDK 版本和设备类型而变化,因此只需强制其保持 RGB_565

Try setting getWindow().setFormat(PixelFormat.RGB_565) in your onCreate.

The default format seems to change based on SDK version and device type, so just force it to stay at RGB_565

挽清梦 2024-11-01 21:38:10

有一个非常相似(如果不完全相同)的问题,即使将 inPreferredConfig 设置为 ARGB_8888 也没有帮助。

从我可以收集到的内容: http://android.nakatome.net/2010/ 04/bitmap-basics.html

问题是 Android 会自动将 24 位图像抖动为 16 位,这可能会扰乱颜色。该链接提到您可以通过向图像添加 alpha 通道或从原始目录而不是作为资源加载它来禁用此功能。

看来这些对我来说都不是一个选择,我发现以下最终有效:

Paint ditherPaint = new Paint();
ditherPaint.setDither(true);
canvas.drawBitmap(mDrawable.getBitmap(), null, 
                  mDrawable.getBounds(), ditherPaint);

这里 mDrawable 是 BitmapDrawable 类型。

Had a very similar if not exact same problem, even setting inPreferredConfig to ARGB_8888 didn't help.

From what I can glean from: http://android.nakatome.net/2010/04/bitmap-basics.html

the problem is Android automatically dithers 24 bit images down to 16 bit, which can mess with the colors. The link mentions you can disable this by either adding an alpha channel to your image, or loading it from the raw directory instead of as a resource.

Seen as neither of these were an option for me I found the following finally worked:

Paint ditherPaint = new Paint();
ditherPaint.setDither(true);
canvas.drawBitmap(mDrawable.getBitmap(), null, 
                  mDrawable.getBounds(), ditherPaint);

Here mDrawable is of type BitmapDrawable.

不交电费瞎发啥光 2024-11-01 21:38:10

这可能是图像类型和 ImageView 渲染的位图之间的差异,请参阅 位图.配置。以不同的模式加载您的图像,看看是否有帮助。查看位图质量和条带 了解更多信息。

It may be a difference between the image type and the Bitmap the ImageView is rendering on, see Bitmap.Config. Load your image in different modes and see if that helps. Take a look at Bitmap quality and banding for more info.

风苍溪 2024-11-01 21:38:10

这就是为我解决这个问题的方法:

您需要设置屏幕密度以防止位图工厂重新缩放位图。这是通过以下代码完成的:

DisplayMetrics displayMetrics=new DisplayMetrics();
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(displayMetrics);
resample.inScreenDensity = displayMetrics.densityDpi;

Here's what solved this for me:

You need to set the screen density to prevent the bitmap factory from rescaling your bitmap. This is done with the following code:

DisplayMetrics displayMetrics=new DisplayMetrics();
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(displayMetrics);
resample.inScreenDensity = displayMetrics.densityDpi;
半夏半凉 2024-11-01 21:38:10

您可以更改

resample.inPreferredConfig = Config.RGB_565; 

resample.inPreferredConfig = Config.ARGB_8888;

但请注意 RGB_565 有助于获得更好的性能,如下所述:
https://medium.com/@ali.muzaffar/performance-improvement-and-bitmap-pooling-in-android-f97b380cf965#:~:text= ARGB_8888%20有%2032%2Dbit%20颜色,更快%20到%20渲染%20as%20well

You can change

resample.inPreferredConfig = Config.RGB_565; 

to

resample.inPreferredConfig = Config.ARGB_8888;

But please note that RGB_565 contributes to a better performance, as described here:
https://medium.com/@ali.muzaffar/performance-improvement-and-bitmap-pooling-in-android-f97b380cf965#:~:text=ARGB_8888%20has%2032%2Dbit%20color,faster%20to%20render%20as%20well.

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