2014-01-11 24 views
1

我一直在嘗試執行一些我給出的代碼,但似乎無法使其正常工作。我已經開始了,2張圖片,一張是jpg,可以不會被取消,我只是有一個ImageView。使用GIF文件的Android

下一張圖片變成了一張帶有透明度的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與替換用於定位白色的值相同。我不知道它是否重要,但我在另一個之上鋪設多個圖像....

幫助將不勝感激! :-)

回答

0

您是否檢查過有多少像素被替換?

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

要替換不具有價值-1的顏色。

pixels[i] = 0; 

0取代它可能會適得其反,因爲這是一個完全透明的黑色。