Android集成Glide(支持回调)

背景

Glide是当前非常流程的图片加载框架,功能强大而且非常稳定。

集成指导

最简集成方式

1.dependencies中增加依赖

implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
  1. Manifest文件中增加权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

3.使用
样例1:

String url = "https://p3a.pstatp.com/weili/l/79054173089494582.jpg";
Context context = this;
Glide.with(context)
       .load(url)
       .into(imageView);

样例2(带回调,使用样例:加载完图片再显示出图片控件):

String url = "https://p3a.pstatp.com/weili/l/79054173089494582.jpg";
Context context = this;
Glide.with(context)
       .load(url)
       .listener(new RequestListener<Drawable>() {
           @Override
           public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
               Toast.makeText(context, "加载失败!", Toast.LENGTH_SHORT).show();
               return false;
           }

           @Override
           public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
               Toast.makeText(context, "加载成功.", Toast.LENGTH_SHORT).show();
               return false;
           }
       })
       .into(imageView);

自定义Module集成方式

此种方式为了项目实际需要,改变Glide的默认行为。因还未完整使用过,暂不展开了,请参考附录的资料。

附录

https://blog.csdn.net/guolin_blog/article/details/53759439?utm_source=tuicool&utm_medium=referral
https://blog.csdn.net/mingyunxiaohai/article/details/79760784
https://www.jianshu.com/p/90b4749e59d2

猜你喜欢

转载自blog.csdn.net/yinxing2008/article/details/83313116
今日推荐