Gilde 定义
Gilde 是google开源的图片加载库。
glide依赖
//glide
compile 'com.github.bumptech.glide:glide:4.0.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.0.0'
glide 基础用法
glide 用法传承目前流行的链式调用。
/**
* 基础用法.
*/
private void baseUsed(){
Glide.with(this)
.load(URL)
.into(mImageView);
}
glide 可以用RequestOptions配置各种参数:
private void gildeOptions(){
RequestOptions options = new RequestOptions();
options.centerCrop()
.placeholder(R.drawable.default_avatar)
.error(R.drawable.image_error)
.fallback(R.drawable.fallback_nodata);
Glide.with(this)
.load(URL)
.apply(options)
.into(mImageView);
}
其中Built in types:
- CenterCrop:类似 ScaleType.CENTER_CROP
- FitCenter: 类似 ScaleType.FIT_CENTER
- CircleCrop: 圆形裁剪
- placeholder: 占位即加载中的图片。
- error: 错误图片.
- fallback: 当url为null的时候,判断是否设置了fallback,是的话则显示fallback图片,否的话显示error图片,如果error还是没有设置则显示placeholder图片.
这几个图片显示规则:
glide RequestBuilders还可以重用开始多个加载
private void multipleLoad(){
RequestOptions options = new RequestOptions();
options.centerCrop()
.placeholder(R.drawable.default_avatar)
.error(R.drawable.image_error)
.fallback(R.drawable.fallback_nodata);
RequestBuilder<Drawable> requestBuilder =
Glide.with(this)
.asDrawable().apply(options);
for(int i=0;i<mImageGroup.getChildCount();i++){
ImageView view = (ImageView) mImageGroup.getChildAt(i);
requestBuilder.clone()
.load(urls[i])
.into(view);
}
}
AppGlideModule 用法
在glide4中引入 glideApp,通过annotation将option封装自动生成代码。
第一步必须自定义一个module 继承于AppGlideModule。
@GlideModule
public final class MyGlideModule extends AppGlideModule {
}
注意: AppGlideModule实现必须始终与@GlideModule注释一起出现.如果注释不存在,则该模块将不会被发现。重新编译,会自动生成代码,后面可用GlideApp.with() 代替Glide.with()
1、设置手机默认推荐缓存大小。MemorySizeCalculator类通过考虑设备给定的可用内存和屏幕大小想出合理的默认大小.
/**
* MemorySizeCalculator类通过考虑设备给定的可用内存和屏幕大小想出合理的默认大小.
* 通过LruResourceCache进行缓存。
* @param context
* @param builder
*/
@Override
public void applyOptions(Context context, GlideBuilder builder) {
MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context)
.setMemoryCacheScreens(2)
.build();
builder.setMemoryCache(new LruResourceCache(calculator.getMemoryCacheSize()));
}
2、自定义内存缓存大小
/**
* 自定义缓存大小.
* @param context
* @param builder
*/
@Override
public void applyOptions(Context context, GlideBuilder builder) {
int memoryCacheSizeBytes = 1024 * 1024 * 20; // 20mb
builder.setMemoryCache(new LruResourceCache(memoryCacheSizeBytes));
}
3、Disk Cache.自定义内置磁盘缓存大小
/**
* Disk Cache.自定义内置磁盘缓存大小.
* @param context
* @param builder
*/
@Override
public void applyOptions(Context context, GlideBuilder builder) {
int diskCacheSizeBytes = 1024 * 1024 * 100; // 100 MB
builder.setDiskCache(new InternalCacheDiskCacheFactory(context, diskCacheSizeBytes));
}
4、Disk Cache.自定义内置磁盘缓存大小并指定路径.
/**
* Disk Cache.自定义内置磁盘缓存大小并指定路径.
* @param context
* @param builder
*/
@Override
public void applyOptions(Context context, GlideBuilder builder) {
int diskCacheSizeBytes = 1024 * 1024 * 100; // 100 MB
builder.setDiskCache(
new InternalCacheDiskCacheFactory(context, "cacheFolderName", diskCacheSizeBytes));
}
5、Disk Cache.自定义外置磁盘缓存大小并指定路径.
/**
* Disk Cache.自定义外置磁盘缓存大小并指定路径.
* @param context
* @param builder
*/
@Override
public void applyOptions(Context context, GlideBuilder builder) {
int diskCacheSizeBytes = 1024 * 1024 * 100; // 100 MB
builder.setDiskCache(
new ExternalCacheDiskCacheFactory(context, "cacheFolderName", diskCacheSizeBytes));
}
isManifestParsingEnabled 设置清单解析,设置为false,避免添加相同的modules两次
@Override
public boolean isManifestParsingEnabled() {
return false;
}
GlideApp用法
基本用法
private void gildeAppUsed(){
GlideApp.with(this)
.load(URL1)
.placeholder(R.drawable.default_avatar)
.error(R.drawable.image_error)
.fallback(R.drawable.fallback_nodata)
.into(mImageView);
}
设置完全缓存
/**
* 缓存.
*/
private void diskCacheStrategyAll(){
GlideApp.with(this)
.asBitmap()
.load(URL)
.placeholder(R.drawable.default_avatar)
.error(R.drawable.image_error)
.fallback(R.drawable.fallback_nodata)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(mImageView);
}
- DiskCacheStrategy.NONE 不做磁盘缓存
- DiskCacheStrategy.SOURCE 只缓存图像原图
- DiskCacheStrategy.RESULT 只缓存加载后的图像,即处理后最终显示时的图像
- DiskCacheStrategy.ALL 缓存所有版本的图像(默认行为)
只从缓存中读取,如果缓存没有,则失败.
private void retrieveFromCache(){
GlideApp.with(this)
.asBitmap()
.load(URL)
.placeholder(R.drawable.default_avatar)
.error(R.drawable.image_error)
.fallback(R.drawable.fallback_nodata)
.onlyRetrieveFromCache(true)
.into(mImageView);
}
跳过缓存. 每次都从服务端获取最新.
/**
* 跳过缓存. 每次都从服务端获取最新.
* diskCacheStrategy: 磁盘缓存
* skipMemoryCache:内存缓存
*/
private void skipCache(){
GlideApp.with(this)
.asBitmap()
.load(URL)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(mImageView);
}
清除缓存
/**
* 清除内存缓存.
*/
private void clearMemoryCache(){
// This method must be called on the main thread.
Glide.get(this).clearMemory();
}
/**
* 清除磁盘缓存.
*/
private void clearDiskCache(){
new AsyncTask<Void,Void,Void>(){
@Override
protected Void doInBackground(Void... params) {
// This method must be called on a background thread.
Glide.get(getApplicationContext()).clearDiskCache();
return null;
}
};
}
其他
自定义圆角Transform
public class CircleTransform extends BitmapTransformation {
public CircleTransform(Context context){
super(context);
}
@Override
protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
return circleCrop(pool,toTransform);
}
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {
}
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;
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;
}
}
调用:
/**
* 自定义圆形裁剪.
*/
private void customerOptions(){
RequestOptions options = new RequestOptions();
options.centerCrop()
.placeholder(R.drawable.default_avatar)
.error(R.drawable.image_error)
.fallback(R.drawable.fallback_nodata)
.transform(new CircleTransform(this));
Glide.with(this)
.load(URL)
.apply(options)
.into(mImageView);
}