2013-02-23 93 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