前言:
在平常的开发中作为开发人员的我们难免会遇到一些从服务器获取图片加载到控件上的操作,中所周知的Android中为每个APP分发的内存是有限的,可是随着当我们需要加载的图片慢慢的多了的时候就需要考虑到内存溢出的问题,当一个APP内存占用太大以至于大于等于这个APP所拥有的内存的时候就会出现卡顿甚至崩溃的问题。那么作为开发人员的我们又需要怎么对这些图片进行相应的处理才能解决这些问题?当然作为一个开发人员来讲,我们可以自己实现对这些图片的缓存——也就是所谓的三级缓存;但是Google也向我们推荐一些图片加载的框架,比如Picasso,Glide.ImageLoader这些用于图片加载的框架,今天就简单的介绍一下Glide这个加载框架在日常开发中的应用
先来说说使用步骤:
步骤一:引用
首先在APP的build.gradle文件下引入Glide
compile 'com.github.bumptech.glide:glide:3.6.1'
步骤二:使用
Glide.with(getContext())
.load(url)
.listener(mRequestListener)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(Drawables.sPlaceholderDrawable)
.error(Drawables.sErrorDrawable)
.into(mImageView)
解读一下这里面需要的参数:with(上下文) 这个上下文可以是当前activity或者是fragment的上下文,所以Glide也就支持了在activity或者fragment中使用;load(图片的网址URL):在使用的时候只需要把获取的图片的网址放进去就可以了(必需);Listener:这个参数呢是为加载图片提供的配置监听器的,在这里可以实现一些自己的特定的需求;
.diskCacheStrategy():这个参数呢是设置磁盘缓存的内容,这里提供了四个参数供我们选择all:缓存源资源和转换后的资源;none:不作任何磁盘缓存;source:缓存源资源;result:缓存转换后的资源;Placeholder:设置加载时候的图片;error:设置加载失败的图片;into:设置显示到那个控件上面
以上呢就是Glide在加载图片的时候的一些基本的使用,接下来就说说使用Glide怎么实现加载一张圆角或者圆形的图片:
先贴出Glide加载网络图片转换为圆角的方法
public class GlideRoundTransform extends BitmapTransformation {
private static float radius = 0f;
public GlideRoundTransform(Context context) {
this(context, 4);
}
public GlideRoundTransform(Context context, int dp) {
super(context);
this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
}
@Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return roundCrop(pool, toTransform);
}
private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
canvas.drawRoundRect(rectF, radius, radius, paint);
return result;
}
@Override public String getId() {
return getClass().getName() + Math.round(radius);
}
}
使用方法:
glideRequest.load(urlg").transform(new GlideRoundTransform(context)).into(imageView);
glideRequest.load(url
).transform(new GlideRoundTransform(context, 10)).into(imageView);
以下是加载图片转换为圆形图片的方法:
public class GlideCircleTransform extends BitmapTransformation {
public GlideCircleTransform(Context context) {
super(context);
}
@Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return circleCrop(pool, toTransform);
}
private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
// TODO this could be acquired from the pool too
Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
return result;
}
@Override public String getId() {
return getClass().getName();
}
}
使用方法:
private RequestManager glideRequest;
glideRequest = Glide.with(context);
glideRequest.load(url).transform(new GlideCircleTransform(context)).into(imageView);
以上就是Glide加载圆角图片以及实现圆形图片的方法
接下来看看怎么使用Glide实现加载一张GIF动画图片:
Glide.with(context).load(url).asBitmap()/asGif().into(imageview);
asBitmap和asGif的区别呢就是一个是静态和一个是动态
当然Glide也支持了从本地获取文件来加载图片
最后说一点Glide自己的认知和了解:Glide默认的Bitmap的格式呢是ARGB_565,当然这个框架开发的时候就已经很人性化了,你要是觉得这个格式加载出来的质量你觉得不爽的话也可以改的。