在Android的ImageView的透明GIF [英] Transparent GIF in Android ImageView

查看:1922
本文介绍了在Android的ImageView的透明GIF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我没有成功的应用程序显示了透明的GIF图像。我可以下载和显示的图标中的ImageView但与白色背景,而不是透明的。

I am trying to display the transparent GIF image in my app with no success. I can get to download and display the icon in an ImageView but with white background instead of transparent.

我已经尝试了这些解决方案,没有sucess: 转换为GIF PNG编程

I have already tried these solutions with no sucess: Convert GIF to PNG programatically

有没有人想出如何显示与透明GIF图像?我使用了Android SDK 18

Has anyone figured out how to display the GIF image with transparency? I am using the Android 18 SDK.

推荐答案

下面是一个已经工作了我一个解决方法。这不是一个很好的解决方案。它可能无法在所有情况下工作,并且在图像处理显著开销。希望别人会找到一个更好的解决方案。

Here is a workaround that has worked for me. It is not a good solution. It may not work in all cases and there is significant overhead in image processing. Hopefully someone else will find a better solution.

在我的测试中,在4.4 GIF图像具有透明度值都用白色(-1)或黑色(-16777216)。当您加载一个位图,您可以将白色/黑色像素回透明。当然,这将仅工作,如果该图像的其余部分不使用相同的颜色。如果是的话那么你也将转换成图像,以透明的零件不是透明的原始图像。在我而言,这是没有问题的。

In my testing, GIF images in 4.4 have transparency values as either white (-1) or black (-16777216). After you load a Bitmap, you can convert the white/black pixels back to transparent. Of course this will only work if the rest of the image doesn't use the same color. If it does then you will also convert parts of your image to transparent that were not transparent in the original image. In my case this wasn't a problem.

您可以使用下面的code为白色或黑色像素转换成透明。

You can use the following code to convert either white or black pixels to transparent.

Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
bitmap = eraseBG(bitmap, -1);         // use for white background
bitmap = eraseBG(bitmap, -16777216);  // use for black background



private static Bitmap eraseBG(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;
}

注:我不得不将位图复制到一个新的ARGB_8888形象这个工作。加载位图作为可变即使,我仍然无法改变的像素。这是为什么有那么多的开销。

Note: I had to copy the Bitmap to a new ARGB_8888 image for this to work. Even when loading the bitmap as mutable, I still couldn't modify the pixels. This is partly why there is so much overhead.

这篇关于在Android的ImageView的透明GIF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆