gpt4 book ai didi

android - 以编程方式将 GIF 转换为 PNG

转载 作者:行者123 更新时间:2023-11-29 21:48:42 25 4
gpt4 key购买 nike

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

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

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

最佳答案

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

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 - 以编程方式将 GIF 转换为 PNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15042993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com