最近一次更新: 2019.08.08
背景Glide是当前非常流程的图片加载框架,功能强大而且非常稳定。
集成指导 最简集成方式1.dependencies中增加依赖
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
- Manifest文件中增加权限
- 使用
- 样例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() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
Toast.makeText(context, "加载失败!", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
Toast.makeText(context, "加载成功.", Toast.LENGTH_SHORT).show();
return false;
}
})
.into(imageView);
- 样例3:显示圆角及居中裁剪
//居中裁剪
private fun centerCropImg() {
Glide.with(this)
.load(url)
.centerCrop()
.into(roundCornerIv)
}
//圆角
private fun roundCornerImg() {
Glide.with(this)
.load(url)
.transform(RoundedCorners(20))
.into(centerCropIv)
}
//居中裁剪+圆角
private fun centerCropAndRoundCornerImg() {
Glide.with(this)
.load(url)
.transform(MultiTransformation(CenterCrop(), RoundedCorners(20)))
.into(roundCornerAndCenterCropIv)
}
效果图:
- 样例4:gif动图
val url = "https://c-ssl.duitang.com/uploads/item/201703/17/20170317135641_dFL5w.gif"
Glide.with(this)
.load(url)
.into(imageView)
#源代码: https://gitee.com/cxyzy1/glideDemo.git
附录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
安卓开发技术分享: https://blog.csdn.net/yinxing2008/article/details/84555061