【问题标题】:Convert GIF to PNG programmatically以编程方式将 GIF 转换为 PNG
【发布时间】:2013-02-09 04:21:12
【问题描述】:

我有一个 GIF,我从网站 (http://radar.weather.gov/ridge/RadarImg/N0R/) 抓取并希望显示给用户。我正在构建 Jelly Bean (4.1) 并且在我对这个主题的搜索中发现 GIF 兼容性即将用于 Android 并且完全不适用于 Jelly Bean。

所以,我想即时将 GIF 转换为 PNG。我该怎么做?是否像从 GIF 读取字节到 PNG 文件一样简单?

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

【问题讨论】:

    标签: android image-processing png gif


    【解决方案1】:

    稍微改变一下我的问题后,我通过全能的谷歌在这里找到了答案:

    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);
    

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

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

    【讨论】:

    • 似乎在 Android 4.4 kitkat GIF 透明度在执行 BitmapFactory.decodeStream() 时丢失。因此,除非我遗漏了某些东西,否则在压缩为 PNG 透明度时仍然会丢失。 issue report.
    • 你能在你的代码中指定mCurrentRadar的对象类型吗?谢谢
    • @IcedD​​ante mCurrentRadar 必须是 Bitmap 实例的方法。
    猜你喜欢
    • 2011-10-12
    • 2013-06-23
    • 2011-05-11
    • 2011-07-07
    • 2021-02-01
    • 2012-01-25
    • 2011-06-21
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多