ImageView中图像的透明部分变黑

Transparent part of image in ImageView become black(ImageView中图像的透明部分变黑)

本文介绍了ImageView中图像的透明部分变黑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 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.

推荐答案

经过一番尝试:首先我尝试使用图像作为资源drawable,它仍然发生(图像的透明部分变黑),其次我将图像转换为png 图像,它可以工作,所以问题出在文件类型(gif)上.因为在我的真实应用程序中,从服务器获取的图像,我不能像往常一样以 png 格式请求图像,所以我使用此链接中的解决方案:Android ImageView 中的透明 GIF

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 在列表视图中显示图像,所以我使用 Target ,到目前为止它工作得很好.

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中图像的透明部分变黑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:ImageView中图像的透明部分变黑

基础教程推荐