android - BitmapFactory.decodeResource() 忽略 jpg 图像的 inPreferredConfig 选项

标签 android colors bitmap jpeg decode

我尝试将 jpeg 资源图像加载到 ARGB_8888 格式的位图:

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap b = BitmapFactory.decodeResource(resources, resId, opts);
Log.d("test", b.getConfig().toString());

这里的resId是一个jpeg图片资源的id。 输出为“RGB_565”。 在 android 2.2 和 2.3 的模拟器中试过这个。

“inPreferredConfig”的文档说:

If this is non-null, the decoder will try to decode into this internal configuration. If it is null, or the request cannot be met, the decoder will try to pick the best matching config based on the system's screen depth, and characteristics of the original image such as if it has per-pixel alpha (requiring a config that also does). Image are loaded with the ARGB_8888 config by default.

那么我是否遇到了“无法满足请求”的情况? :) 但老实说,我看不出将 RGB_565 解码为 ARGB_8888 有多么困难。

所以我想也许我做错了或者这是 Android 中的错误...

最佳答案

加载 jpeg 时,您必须将 alpha channel 设置为 true:

bitmap.setHasAlpha(true);

关于android - BitmapFactory.decodeResource() 忽略 jpg 图像的 inPreferredConfig 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10413628/

相关文章:

android - 引用 Android XML 资源时星号 (*) 的含义是什么?

java - 如何在我的 Android 应用程序中添加对 Android 4.4 的支持

css - 如何使用ext js中的标签<span>更改文本字段中值的颜色

vim - 有没有办法确定一个突出显示是否已经在 Vim 中定义?

c# - 将位图数据提取到字节数组

android - 保存的位图为黑色

android - 如何在 Android 上通过 SOCKS 代理使用 HttpURLConnection

android - 在 Android Studio 中使用外部 JAR 文件

r - 在 Windows 上运行的 R/RStudio 中,我们可以让 message()、warning() 和 error() 在控制台中使用不同的颜色吗?

java - 将图像加载为文件