有了android-gif-drawable这个开源框架,在项目中使用gif动态图片不再是问题。 android-gif-drawable的
开源地址是:https://github.com/koral–/android-gif-drawable。其主要作用就是可以展示gif格式的图片及动画,还可以对动画进行监听操作。开源库中封闭了GifImageView,GifImageButton,GifTextView等控件。和我们普通的ImageView,ImageButton,TextView使用方法基本一样,只要在布局文件中添加这些控件,然后就可以直接设置gif图片作为背景等。
首先在AndroidStudio中配置相关依赖库代码,在配置文件build.gradle文件中添加:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'pl.droidsonroids.gif:android-gif-drawable:1.1.+'
}
- 1
- 2
- 3
- 4
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity">
<pl.droidsonroids.gif.GifImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_bg"
/>
</FrameLayout>
其中项目中的图片是我在百度上随便找的一张gif格式 的图片,运行效果就是手机上有两张动态图片,其中一张上有文字“这个TextView的背景好酷”。