版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangxumh520/article/details/85003676
一、配置
dependencies {
//...
implementation 'com.github.bumptech.glide:glide:4.8.0'
implementation 'jp.wasabeef:glide-transformations:4.0.1'
}
如果想使用Glide新特性-->GlideApp建造者模式的话请配置,使用方法请搜索Glide新特性
dependencies {
//...
annotationProcessor 'com.github.bumptech.glide:compiler::4.8.0'
}
二、使用
1.基本使用
Glide.with(context).load(xxx).into(xxx);
2.占位符、异常使用
RequestOptions option = new RequestOptions()
.placeholder(R.drawable.ic_launcher_background)// 占位符
.error(R.drawable.ic_launcher_background)// 异常;
Glide.with(context).load(xxx).apply(option).into(xxx);
3.圆角处理
RequestOptions option = new RequestOptions()
.transform(new RoundedCornersTransformation(CommonUtil.INSTANCE.dp2px(context, 6), 0));
Glide.with(context).load(xxx).apply(option).into(xxx);
4.模糊处理
RequestOptions option = new RequestOptions()
.transform(new BlurTransformation(25));
Glide.with(context).load(xxx).apply(option).into(xxx);
5.黑白处理
RequestOptions option = new RequestOptions()
.transform(new GrayscaleTransformation());
Glide.with(context).load(xxx).apply(option).into(xxx);
6.多个效果
MultiTransformation multi = new MultiTransformation(
new BlurTransformation(25),
new RoundedCornersTransformation(128, 0, RoundedCornersTransformation.CornerType.ALL));
Glide.with(context).load(xxx).apply(multi).into(xxx);
7.压缩图片
Glide.with(context)
.asBitmap()
.load(xxx)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource,Transition<? super Bitmap> transition) {
mThumBitmap = Bitmap.createScaledBitmap(resource, 50, 50, true);
Log.d(TAG, "压缩后图片的大小" + (mThumBitmap.getByteCount() / 1024)
+ "KB宽度为" + mThumBitmap.getWidth() + "高度为" + mThumBitmap.getHeight());
imageView.setImageBitmap(resource);
}
});