以编程方式将GIF转换为PNG

时间:2013-02-23 16:51:23

标签: android image-processing png gif

我有一个GIF,我从网站(http://radar.weather.gov/ridge/RadarImg/N0R/)抓取并想要显示给用户。我正在构建Jelly Bean(4.1)并且在我对此主题的搜索中发现GIF兼容性正在逐步推出Android,而flat out不适用于Jelly Bean。

所以,我希望将GIF转换为动态PNG。我该怎么做?是否像从GIF到PNG文件的字节读取一样简单?

我将使用ImageView在UI上显示图像。

1 个答案:

答案 0 :(得分: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;
}

将其解码为Bitmap,可以将其压缩为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);

...或者可以立即用于ImageView ...

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