加载GIF动态图

转载:http://blog.csdn.net/longer__/article/details/67636456


最近因为项目中需要用到gif动态图片,而android目前还不支持gif动态图片的加载,所以搜索了网上已有的第三方自定义控件,帮助实现加载gif图片。 

第一种是使用gifView: 
demo的介绍和下载地址: 
http://code.google.com/p/gifview/ 
在google上下载demo和.jar包,但是目前google被屏蔽了,需要翻墙才能下载。 
首先需要导入.jar包到你的工程里。 把下载的.jar包文件copy到工程libs文件夹里,然后右键点击add as library就可以了。 
其次在对应的layout布局文件里添加这个自定义GifView。如下面的代码所示。 
     <com.ant.liao.GifView
    android:id="@+id/gif1" 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:paddingRight="14px" android:enabled="false" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

” 
最后在对应页面.class文件的程序中配置上gif图片。需要的gif图片提前放到res/drawable里。如下图代码所示: 
”gf1.setGifImage(R.drawable.gif1);“ 
这样也就全部完成了,很完美,前人栽树后人乘凉。但是马上问题就来了,多次进入这个放有gif动画的页面,很容易OOM。要解决这个OOM,还需要添加一个C语言开发的什么鬼,反正挺麻烦的,导致我直接放弃这个方法。 
第二种就是使用android-gif-drawable 
demo的介绍和下载地址: 
https://github.com/koral–/android-gif-drawable 
这个更加方便,只需要两个步骤。 
首先添加引用文件 
不需要再下载.jar包,只需要的app的build.gradle里添加 
dependencies { 
compile ‘pl.droidsonroids.gif:android-gif-drawable:1.2.6’ 

其次在对应layout文件下添加自定义gif控件,在这个控件里直接添加gif动画文件。如下图代码所示。 

<pl.droidsonroids.gif.GifImageView
        android:id="@+id/gv_error"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/gif"/>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

” 
这样就可以了,非常方便,多次 点击也不会出现OOM。

猜你喜欢

转载自blog.csdn.net/liu_ser/article/details/79590207