下载的位图在添加到 ImageView 时会失去透明度
我试图从远程位置显示一个可能透明的图像,但当我将其添加到我的 ImageView
时,Alpha 通道似乎呈白色。
我正在使用以下代码下载远程图像:
public static Bitmap loadBitmap(String url) throws IOException {
int bufferSize = 1024;
InputStream in = new BufferedInputStream(new URL(url).openStream(), bufferSize);
Bitmap bitmap = BitmapFactory.decodeStream(in);
try {
in.close();
} catch (Exception ignored) {
}
return bitmap;
}
在我的 Activity
和提到的 ImageView
中,我有以下代码(省略了 try-catch):
if (bitmap != null) {
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
imageView.setImageBitmap(bitmap);
}
bitmap.getConfig ()
返回 ARGB_8888
。
我正在针对 Android 1.6(即 SDK 版本 4)进行编码。
我是否缺少一些魔法设置器?当我从 res 文件夹中加载与 Drawable
完全相同的图片时,它工作正常。
我注意到 Bitmap
上有一个名为 setHasAlpha
的 setter,但这是从 SDK 级别 12 开始的。
编辑: 我尝试获取一些我知道是透明的像素的颜色,并且它们的颜色== 0
,这是透明的。
I am trying to show a potentially transparent image from a remote location, but the alpha channel seems to be colored white when I add it to my ImageView
.
I am downloading a remote image with the following code:
public static Bitmap loadBitmap(String url) throws IOException {
int bufferSize = 1024;
InputStream in = new BufferedInputStream(new URL(url).openStream(), bufferSize);
Bitmap bitmap = BitmapFactory.decodeStream(in);
try {
in.close();
} catch (Exception ignored) {
}
return bitmap;
}
In my Activity
with the mentioned ImageView
I have the following code (try-catch omitted):
if (bitmap != null) {
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
imageView.setImageBitmap(bitmap);
}
bitmap.getConfig()
returns ARGB_8888
.
I am coding against Android 1.6, i.e. SDK version 4.
Am I missing some magic setter? When I load the exact same picture as a Drawable
from my res folder it works fine.
I noticed a setter called setHasAlpha
on Bitmap
, but this is since SDK level 12.
EDIT:
I tried getting the color of some of the pixels I know are transparent, and their color == 0
, which is transparent.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来 ImageView 本身有一个背景。尝试将背景颜色设置为透明(例如,
imageView.setBackgroundColor(0);
Sounds like the ImageView itself has a background. Try setting the background color to transparent (eg,
imageView.setBackgroundColor(0);
使用PNG压缩格式
use compress format as PNG