网上很多教程,告诉你怎样去做,但是现在Glide升级到4.0以上版本了,变化比较大,3.*那些方法已经不适应了。我说一下过程:
1在app的Gradle中添加以下依赖项
implementation'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor'com.github.bumptech.glide:compiler:4.8.0'
implementation'com.github.bumptech.glide:annotations:4.8.0'
//解决Glide找不到Android声明库问题
annotationProcessor'androidx.annotation:annotation:1.0.0'
implementation"com.github.bumptech.glide:okhttp3-integration:4.8.0"
2新建一个类
import android.content.Context;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.module.AppGlideModule;
import com.qdgaoshanyun.dianxiaoerapplication.utils.Http.ImgOkHttpClient;
import java.io.InputStream;
import io.reactivex.annotations.NonNull;
import okhttp3.OkHttpClient;
@GlideModule
public final class MyAppGlideModuleextends AppGlideModule {
@Override
public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
//super.registerComponents(context, glide, registry);
OkHttpClient client = ImgOkHttpClient.getImgOkHttpClient();
registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client));
}
}
其中,getImgOkHttpClient()就是获取已经实现了证书认证的Okhttp。
3使用
GlideApp.with(WelcomeActivity.this)
.load("图片的完整url路径")
.listener(new RequestListener() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
Log.e("wlcm_load_img", "图片加载失败");
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
return false;
}
})
.into(binding.adImg);