方法一:通过webview的方式显示gif图 ,这种方式现在只发现一个问题,gif图播放久了会有卡顿一下,但是过一会又正常。其他的问题还没有发现,有待后面发现解决:
这是xml中定义的webview
<WebView
android:id="@+id/id_gif_web"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scrollbars="none"
android:visibility="gone"/>
WebView webgif = (WebView)findViewById(R.id.id_gif_web);
<pre name="code" class="java">if (imageUri.endsWith(".gif")) {
imageView.setVisibility(View.GONE);
webgif.setVisibility(View.VISIBLE);
String html = "<html><body style=\"text-align: center; background-color: #000000; vertical-align: middle;\"><img width=\"100%\" src=\""+imageUri+"\" /></body></html>";
webgif.loadDataWithBaseURL(null,html,"text/html", "UTF-8", null);
}
} imageUri是String 类型的 String imageUri=http://img.newyx.net/news_img/201306/20/1371714170_1812223777.gif;
width是webview中的图片自适应屏幕宽度
loadDataWithBaseURL可以支持2.3版本出现无法找到网页的问题
loadData(html,"text/html", "UTF-8")这种方式在2.3系统中会出现无法找到网页的问题。详细介绍参考http://www.eoeandroid.com/thread-185654-1-1.html;
方法二:使用开源的jar包Android gif drawable ;下载地址:https://github.com/koral--/android-gif-drawable/ 使用方法参照:本地加载http://my.oschina.net/u/1175746/blog/288258;网络加载:http://my.oschina.net/u/1175746/blog/345159
但是按照此办法加到项目中使用的时候,发现问题1:gif图播放不是很流畅。2、不知道怎么设置gif图自适应屏幕宽度。
本文探讨了在Android应用中通过WebView和开源库Androidgifdrawable显示GIF图时遇到的卡顿问题,并提供了两种解决方案。第一种方法是利用WebView直接加载GIF,但在某些情况下会卡顿;第二种方法是使用Androidgifdrawable库,虽然解决了卡顿问题,但GIF图自适应屏幕宽度仍有待解决。文章最后提出了解决方案并给出了相关代码示例。
1364

被折叠的 条评论
为什么被折叠?



