【问题标题】:Android working with GIF filesAndroid 处理 GIF 文件
【发布时间】:2014-01-30 06:58:19
【问题描述】:

我一直在尝试实现一些我收到的代码,但似乎无法使其正常运行。我已经开始了,两张图片,一张是 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 与替换目标白色的值相同。我不知道这是否重要,但我将多张图像叠加在一起......

我们将不胜感激! :-)

【问题讨论】:

    标签: java android bitmap gif


    【解决方案1】:

    你检查过多少像素被替换了吗?

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

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

    pixels[i] = 0;
    

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

    【讨论】:

      猜你喜欢
      • 2017-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-29
      • 2015-05-29
      • 1970-01-01
      • 2017-05-07
      • 1970-01-01
      相关资源
      最近更新 更多