Glide4是一个专门为Android开发的图片加载程序库,glide框架加载网络图片,本地图片,glide让显示圆角图片,圆形图片更加方便快捷,Google非常推荐Glide框架,它已经被用于许多Google开源程序,Glide已经添加了GIF支持以及图片的加载/缓存功能,下面来开始学习Glide吧!
一:添加Glide 4版本依赖
在app/build.gradle下添加Glide框架的依赖,代码如下:
implementation 'com.github.bumptech.glide:glide:4.11.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
添加MyAppGlideModule.jar
配置文件,并继承于AppGlideModule
类,添加@GlideModule
注解,先不用添加任何内容,留一个空类,注意:Glide3版本不需要添加这种配置文件。
import com.bumptech.glide.annotation.GlideModule; import com.bumptech.glide.module.AppGlideModule; // new since Glide v4 @GlideModule public final class MyAppGlideModule extends AppGlideModule { // leave empty for now }
编译一下我们的Android Studio之后,我们就可以使用GlideApp.with()
这种写法了,而在Glide3版本中,是这样写的:Glide.with()
二:Glide4框架的基本使用
GlideApp.with(context) .load("http://via.placeholder.com/300.png") .into(imageView);
三:Glide框架的高级使用
1:重新设置图片大小,并加载图片
GlideApp.with(context) .load("http://via.placeholder.com/300.png") .override(300, 200) .into(imageView);
2:添加Placeholder占位图片和错误时显示的图片
GlideApp.with(context) .load("http://via.placeholder.com/300.png") .placeholder(R.drawable.placeholder) .error(R.drawable.imagenotfound) .into(imageView);
3:glide从中间裁剪图片
GlideApp.with(context) .load("http://via.placeholder.com/300.png") .centerCrop() .into(imageView);
四:配置Glide
在MyAppGlideModule
中重写applyOptions方法
@GlideModule public final class MyAppGlideModule extends AppGlideModule { @Override public void applyOptions(Context context, GlideBuilder builder) { // Glide default Bitmap Format is set to RGB_565 since it // consumed just 50% memory footprint compared to ARGB_8888. // Increase memory usage for quality with: // Glide默认是RGB_565图片格式,设置成ARGB_8888可以节约50%内存,提高内存使用效率 builder.setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_ARGB_8888)); } }
五:Glide图片Transformations转换(此为扩展学习)
Glide图片转换需要使用第三方库,这个非常不错:glide-transformations,在app/build.gradle中加入此依赖,代码如下:
dependencies { implementation 'jp.wasabeef:glide-transformations:4.1.0' // If you want to use the GPU Filters implementation 'jp.co.cyberagent.android:gpuimage:2.0.4' }
1:Glide设置圆角图片
int radius = 30; // corner radius, higher value = more rounded int margin = 10; // crop margin, set to 0 for corners with no crop GlideApp.with(this) .load("http://via.placeholder.com/300.png") .transform(new RoundedCornersTransformation(radius, margin)) .into(ivImg);
2:Glide裁剪成圆形图片
GlideApp.with(this) .load("http://via.placeholder.com/300.png") .transform(new CircleCrop()) .into(ivImg);
3:Glide添加模糊图片
GlideApp.with(this) .load("http://via.placeholder.com/300.png") .transform(new BlurTransformation()) .into(ivImg);
Glide多重效果叠加,如Glide设置成模糊以及圆形图片
GlideApp.with(this) .load("http://via.placeholder.com/300.png") .transform(new MultiTransformation<Bitmap>(new BlurTransformation(25), new CircleCrop())) .into(ivImg);
六:动态调整ImageView图片大小
要ImageView在检索图像后重新调整大小,请首先定义一个SimpleTarget<Bitmap>对象以在Bitmap加载图像后对其进行拦截:
private SimpleTarget target = new SimpleTarget<Bitmap>() { @Override public void onResourceReady(Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) { // do something with the bitmap // set it to an ImageView imageView.setImageBitmap(bitmap); } };
通过 SimpleTarget
传给Glide的into()方法,代码如下:
GlideApp.with(context) .asBitmap() .load("http://via.placeholder.com/300.png") .into(target);
注意事项:
SimpleTarget
对象必须作为一个方法或者属性,不能以匿名类的方式传入到into方法中,例如这样传入是错误的:GlideApp.with(this).load("url").into(new SimpleTarget<Bitmap>() { ... })