方法一 使用Gif三方库加载
1, 在moudle的build.gradle文件的dependencies节点下加上下面这句代码
implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.16'

2, 静态加载: 在布局文件中引用三方库的GifImageView静态加载如下所示
<pl.droidsonroids.gif.GifImageView
android:id="@+id/gif_view"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:scaleType="centerCrop"
tools:src="@mipmap/app_splash"/>

动态加载

从上图可以看到 创建GifGDrawable的第二个参数可以是一个本地资源ID,也可以是一个本地文件,还可以是一个Uri
顺便贴上代码:
//使用三方库加载Gif
private void gifDrawableFun() {
try {
gifDrawable =new GifDrawable(getResources(), R.mipmap.app_splash);
}catch (IOException e) {
e.printStackTrace();
}
gif_view.setImageDrawable(gifDrawable);
}
方法二 使用Glide加载:
1, 在moudle的build.gradle文件的dependencies节点下加上下面这句代码

implementation 'com.github.bumptech.glide:glide:4.11.0'
2,调用下面的方法

这里的R.mipmap.app_splash是我本地gif图片资源,同样你也可以传入一个uri;
//使用Glide加载Gif
private void GlideFun(@NonNull View imageView) {
Glide.with(SplashActivity.this)
.load(R.mipmap.app_splash)
.listener(new RequestListener() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target,
boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target target,
DataSource dataSource, boolean isFirstResource) {
if (resourceinstanceof GifDrawable){
//Glide默认会加载多次这里设置只加载1次
((GifDrawable) resource).setLoopCount(1);
}
return false;
}
}).into((ImageView) imageView);
}
注意事项:
至此两种加载Gif图片的方法已经写完但是你会发现在加载图片的最开始会先出现一个空白页面才开始加载gif图片.
这个时候需要在我们的Manifast文件下的activity节点节点下加上一个跟gif背景一直的主题我是这样加的:

android:theme="@style/libSplashTheme">

<style name="libSplashTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="android:windowBackground">@color/color_splash
</style>

<color name="color_splash">#151c2e</color>
网友评论