2013-02-23 42 views
1

我有一個GIF,我從網站(http://radar.weather.gov/ridge/RadarImg/N0R/)抓取並想要顯示給用戶。我正在構建Jelly Bean(4.1),並且在關於此主題的搜索中發現,GIF兼容性正在爲Android平臺解決,而扁平化不適用於Jelly Bean。以編程方式將GIF轉換爲PNG

所以,我想將GIF轉換爲動態PNG。我將如何做到這一點?是否像讀取GIF中的字節到PNG文件一樣簡單?

我將使用ImageView在UI上顯示圖像。

回答

2

改變我的問題有點後,我發現通過全能的谷歌這裏的答案:這裏總結

http://gayashan-a.blogspot.com/2012/02/android-how-to-display-gif-image-in.html

public Bitmap getBitmap(String url) 
{ 
    Bitmap bmp = null; 
    try 
    { 
     HttpClient client = new DefaultHttpClient(); 
     URI imageUri = new URI(url); 
     HttpGet req = new HttpGet(); 
     req.setURI(imageUri); 
     HttpResponse resp = client.execute(req); 
     bmp = BitmapFactory.decodeStream(resp.getEntity().getContent());    
    } 
    catch(URISyntaxException ex) 
    {   
     Log.e("ERROR", ex.getMessage()); 
    } 
    catch(ClientProtocolException ex) 
    { 
     Log.e("ERROR", ex.getMessage()); 
    } 
    catch(IllegalStateException ex) 
    { 
     Log.e("ERROR", ex.getMessage()); 
    } 
    catch(IOException ex) 
    { 
     Log.e("ERROR", ex.getMessage()); 
    } 

    return bmp; 
} 

這是解碼爲可被壓縮的位圖到PNG ...

Bitmap mCurrentRadar = getBitmap("http://radar.weather.gov/ridge/RadarImg/N0R/ABC_N0R_0.gif"); 
ByteArrayOutputStream stream = new ByteArrayOutputStream();  
mCurrentRadar.compress(Bitmap.CompressFormat.PNG, 100, stream); 

...或者可以是imm在ImageView ediately使用...

ImageView imageView = (ImageView) findeViewById(R.id.radarImageView); 
imageView.setImageBitmap(getBitmap("http://radar.weather.gov/ridge/RadarImg/N0R/ABC_N0R_0.gif"); 
+1

這似乎在Android 4.4的奇巧GIF失去透明性做'BitmapFactory.decodeStream時()'。所以除非我失去了一些東西,當壓縮到PNG透明度仍然失去。 [問題報告](https://code.google.com/p/android/issues/detail?id=62016)。 – paul 2013-11-12 02:18:33

+0

你可以在代碼中指定mCurrentRadar的對象類型嗎? Thx – IcedDante 2014-02-27 13:48:38

+0

@IcedD​​ante mCurrentRadar必須是位圖實例的一種方法。 – Lanitka 2016-08-05 10:05:37

相關問題