Androidd开源控件-Glide

  1. 1. Glide 4.3.1 (4.x用起来更麻烦了)
    1. 1.1. 集成
    2. 1.2. 清理缓存
    3. 1.3. 圆形图片
    4. 1.4. 默认和错误图片
    5. 1.5. 加载动画
  2. 2. Glide 3.8.0
    1. 2.1. 使用

Glide 4.3.1 (4.x用起来更麻烦了)

集成

1
2
3
4
5
6
7
8
9
repositories {
mavenCentral()
maven { url 'https://maven.google.com' }
}

dependencies {
compile 'com.github.bumptech.glide:glide:4.3.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.3.1'
}

清理缓存

1
2
3
4
5
6
7
8
9
10
11
12
13
Glide.get(getActivity()).clearMemory();
new Thread(new Runnable() {
@Override
public void run() {
Glide.get(getActivity()).clearDiskCache();
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
ToastUtils.showCustomBgToast("清理成功!");
}
});
}
}).start();

圆形图片

1
2
3
4
Glide.with(mContext).load(url)
.apply(new RequestOptions()
.circleCrop())
.into(ivPhoto);

默认和错误图片

1
2
3
4
5
Glide.with(mContext).load(url)
.apply(new RequestOptions()
.error(R.drawable.photo_me)
.placeholder(R.drawable.photo_me))
.into(ivPhoto);

加载动画

1
2
3
Glide.with(mContext).load(url)
.transition(new DrawableTransitionOptions().crossFade(300))
.into(ivPhoto);

Glide 3.8.0

1
2
3
4
5
6
7
8
repositories {
mavenCentral()
maven { url 'https://maven.google.com' }
}

dependencies {
compile 'com.github.bumptech.glide:glide:3.8.0'
}

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Glide.with(this)
.load(url)
.placeholder(R.drawable.ic_default)
.error(R.drawable.ic_default)
.crossFade()
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}

@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
return false;
}
})
.into(iv);