以编程方式将GIF转换为PNG [英] Convert GIF to PNG programmatically

查看:111
本文介绍了以编程方式将GIF转换为PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从网站抓取的GIF( http://radar.weather .gov/ridge/RadarImg/N0R/),并希望向用户显示.我正在构建Jelly Bean(4.1),并且在对此主题的搜索中发现,GIF兼容性正逐渐消失,无法在Android上使用,而完全无法在Jelly Bean上使用.

I have a GIF that I am grabbing from a website (http://radar.weather.gov/ridge/RadarImg/N0R/) and wanting to display to the user. I am building to Jelly Bean (4.1) and in my searches on this subject found that GIF compatibility is on its way out for Android and flat out doesn't work on Jelly Bean.

因此,我想即时将GIF转换为PNG.我该怎么做?像从GIF读取字节到PNG文件一样简单吗?

So, I want to convert the GIF over to a PNG on the fly. How would I do this? Is it as simple as reading in the bytes from the GIF to a PNG file?

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

I will be using an ImageView to display the image on the UI.

推荐答案

稍微改变了我的问题之后,我在这里通过全能的Google找到了答案:

After changing my question a bit, I found the answer through the almighty Google here:

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的位图...

This decodes it to a Bitmap which can be compressed to a 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 ...

... or can be immediately used in an ImageView ...

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

这篇关于以编程方式将GIF转换为PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆