关于GlideUtil类的封装:
带默认图,错误图,圆角,ScaleType, 高斯模糊等是常见的需求。
随着Glide的版本升级,老的写法可能不起作用了, 比如关于圆角的处理:.apply( RequestOptions.bitmapTransform( new RoundedCorners( 5 ) ) )这种写法常常因为xml中提前设置了scaleType等失效了。所以规范的做法就是统一使用RequestOptions去封装一起请求,以免某些选项被擦除了。
规范做法:
//使用说明2
GlideUtils.displayImageWithCorner(imageView2, R.drawable.logo_ftd, 50);
//使用说明1
RequestOptions requestOptions = new RequestOptions().transform();
requestOptions.override(500, 200);
requestOptions.placeholder(R.drawable.icon_coin_rank);
requestOptions.error(R.drawable.load_err);
requestOptions.transform(new CenterCrop(), new RoundedCorners( 80 ), new BlurTransformation(15));
Glide.with(getApplication())
.load(R.drawable.logo_ftd)
.apply(requestOptions)
.into(imageView);
错误写法:
Glide.with(getApplication())
.load(R.drawable.logo_ftd)
.apply( RequestOptions.bitmapTransform( new RoundedCorners( 50 ) ) )
.centerCrop()
.into(imageView);
关于依赖包:
默认依赖库:
implementation 'com.github.bumptech.glide:glide:4.13.2'
annotationProcessor 'com.github.bumptech.glide:compiler:4.13.2'
如果额外想使用高斯模糊功能:需要引入这个库。
//高斯模糊库implementation 'jp.wasabeef:glide-transformations:4.3.0'
参考博客: