问题描述
在Android KitKat(Nexus 7)中显示透明图像时出现问题,在nexus 4(KitKat)和其他以前的Android操作系统中都可以,这里图像:
I have problem when displaying image with transparency in Android KitKat (Nexus 7), it is OK in nexus 4 (KitKat) and other previous Android OS, here the image:
和ImageView布局:
and ImageView layout:
<ImageView
android:id="@+id/avatar"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="21dp"
android:padding="3dp"
android:src="@drawable/icon_button_profile_new"
android:tag="@string/avatar" />
这里是在Nexus 7(Android 4.4)上运行时的屏幕截图
and here the screenshot when running on Nexus 7 (Android 4.4)
此外,我使用Picasso进行下载&从URL缓存图像。
also, I use Picasso for download & caching image from the URL.
推荐答案
经过一些试验:首先我尝试使用图像作为资源可绘制但它仍然发生(透明)图像的一部分变成黑色),其次我将图像转换为png图像并且它是有效的,所以问题在于文件类型(gif)。
因为在我的真实应用程序中从服务器获取的图像我无法像png格式一样请求图像,我使用此链接的解决方案:
After some trial: first I try using the image as resource drawable and it still happen (transparent part of the image become black), secondly I convert the image to png image and it is work, so the problem is in the file type (gif).since in my real app the image obtained from server and I can't request image as always in png format, I use the solution from this link: Transparent GIF in Android ImageView
它只显示一个图像很简单(就像在我的问题中一样) )因为我使用毕加索,我使用目标从图像头像中删除黑色,如下所示:
it is simple for displaying only one image (like in my question) since I use Picasso I use target to erase black color from image avatar like this:
target = new Target() {
@Override
public void onPrepareLoad(Drawable arg0) {
}
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
if (Build.VERSION.SDK_INT == 19/*Build.VERSION_CODES.KITKAT*/){
Bitmap editedavatar = AndroidUtils.eraseColor(bitmap, -16777216);
avatar.setImageBitmap(editedavatar);
}
}
@Override
public void onBitmapFailed(Drawable arg0) {
avatar.setImageResource(R.drawable.ic_profile_default);
其中擦除颜色是静态方法
where erase color is static method
public static Bitmap eraseColor(Bitmap src, int color) {
int width = src.getWidth();
int height = src.getHeight();
Bitmap b = src.copy(Config.ARGB_8888, true);
b.setHasAlpha(true);
int[] pixels = new int[width * height];
src.getPixels(pixels, 0, width, 0, 0, width, height);
for (int i = 0; i < width * height; i++) {
if (pixels[i] == color) {
pixels[i] = 0;
}
}
b.setPixels(pixels, 0, width, 0, 0, width, height);
return b;
}
但是因为我使用Picasso在列表视图中显示图像,所以我发现了,到目前为止它的工作非常顺利。
but since I use Picasso for displaying images in a listview,I impelements Target in ViewHolder and it is work very well so far.
这篇关于ImageView中透明的图像部分变为黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!