Android正在使用GIF文件

时间:2014-01-11 05:42:33

标签: java android bitmap gif

我一直在尝试实现一些我给出的代码,但似乎无法使其正常运行。我已经开始了,2个图像,一个是jpg,可以没有问题,我只是有一个ImageView。

下一张图片最终成为具有透明度的gif。我需要它,所以一个朋友帮我一些代码来消耗它。但它不会消失......

我从我的UI线程开始:

    // BMP is a async task used for regular images:
    bmp = new BitmapTask(imageview_topo);
    bmp.execute("http://radar.weather.gov/ridge/Overlays/Topo/Short/ILN_Topo_Short.jpg");

    // decodeBlack is an async class used for getting rid of black transperancy
    // I execute the URL to the asynctask:
    decodeBlack = new BitmapDecodeBlack(imageview_counties); 
    decodeBlack.execute("http://radar.weather.gov/ridge/Overlays/Highways/Short/ILN_Highways_Short.gif");

BitmapDecodeBlack对象类:

public class BitmapDecodeWhite extends AsyncTask<String, Void, Bitmap> {

private ImageView imageview;

public BitmapDecodeWhite(ImageView imageview) {
    this.imageview = imageview;
}
protected Bitmap doInBackground(String... url) {

    String urldisplay = url[0];

    //New Bitmap to return:
    Bitmap icon = null;

    //Try to retrieve the icon from the NOAA:
    try {
        InputStream in = new java.net.URL(urldisplay).openStream();
        icon = BitmapFactory.decodeStream(in);

    } catch (Exception e) { 
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }
    //Return the icon fetched:
    return icon;
}

protected void onPostExecute(Bitmap result) {

    Bitmap b= eraseBG(result, -1); 
    // I then set my imageview to the bitmap!
    imageview.setImageBitmap(b);
    imageview.setVisibility(1);
}

// Rids the black from the image:
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;
}

}

我不完全确定为什么它不起作用,另一类BitmapDecodeWhite与替换目标白色的值相同。我不知道它是否重要但是我将多个图像叠加在一起......

非常感谢帮助! : - )

1 个答案:

答案 0 :(得分:0)

您是否检查过多少像素被替换?

Bitmap b= eraseBG(result, -1);
[...]
if (pixels[i] == color) {

您要替换的颜色没有值-1

pixels[i] = 0;

0替换它可能会适得其反,因为它是完全透明的黑色。