-
禁止内存缓存:
.skipMemoryCache(true)
-
清除内存缓存:
// 必须在UI线程中调用
Glide.get(context).clearMemory();
-
禁止磁盘缓存:
.diskCacheStrategy(DiskCacheStrategy.NONE)
-
清除磁盘缓存:
// 必须在后台线程中调用,建议同时clearMemory()
Glide.get(applicationContext).clearDiskCache();
-
获取缓存大小:
new GetDiskCacheSizeTask(textView).execute(new File(getCacheDir(), DiskCache.Factory.DEFAULT_DISK_CACHE_DIR));
class GetDiskCacheSizeTask extends AsyncTask<File, Long, Long> {
private final TextView resultView;
public GetDiskCacheSizeTask(TextView resultView) {
this.resultView = resultView;
}
@Override
protected void onPreExecute() {
resultView.setText("Calculating...");
}
@Override
protected void onProgressUpdate(Long... values) { /* onPostExecute(values[values.length - 1]); */ }
@Override
protected Long doInBackground(File... dirs) {
try {
long totalSize = 0;
for (File dir : dirs) {
publishProgress(totalSize);
totalSize += calculateSize(dir);
}
return totalSize;
} catch (RuntimeException ex) {
final String message = String.format("Cannot get size of %s: %s", Arrays.toString(dirs), ex);
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
resultView.setText("error");
Toast.makeText(resultView.getContext(), message, Toast.LENGTH_LONG).show();
}
});
}
return 0L;
}
@Override
protected void onPostExecute(Long size) {
String sizeText = android.text.format.Formatter.formatFileSize(resultView.getContext(), size);
resultView.setText(sizeText);
}
private static long calculateSize(File dir) {
if (dir == null) return 0;
if (!dir.isDirectory()) return dir.length();
long result = 0;
File[] children = dir.listFiles();
if (children != null)
for (File child : children)
result += calculateSize(child);
return result;
}
}
-
指定资源的优先加载顺序:
//优先加载
Glide
.with(context)
.load(heroImageUrl)
.priority(Priority.HIGH)
.into(imageViewHero);
//后加载
Glide
.with(context)
.load(itemImageUrl)
.priority(Priority.LOW)
.into(imageViewItem);
-
先显示缩略图,再显示原图:
//用原图的1/10作为缩略图
Glide
.with(this)
.load("http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png")
.thumbnail(0.1f)
.into(iv_0);
//用其它图片作为缩略图
DrawableRequestBuilder<Integer> thumbnailRequest = Glide
.with(this)
.load(R.drawable.news);
Glide.with(this)
.load("http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png")
.thumbnail(thumbnailRequest)
.into(iv_0);
-
对图片进行裁剪、模糊、滤镜等处理:
推荐使用独立的图片处理库:wasabeef/glide-transformations,使用也很简单:
compile 'jp.wasabeef:glide-transformations:2.0.0'
之后我们就可以使用GenericRequestBuilder或其子类的transform()
或bitmapTransform()
方法设置图片转换了:
//圆形裁剪
Glide.with(this)
.load("http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png")
.bitmapTransform(new CropCircleTransformation(this))
.into(iv_0);
//圆角处理
Glide.with(this)
.load("http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png")
.bitmapTransform(new RoundedCornersTransformation(this,30,0, RoundedCornersTransformation.CornerType.ALL))
.into(iv_0);
//灰度处理
Glide.with(this)
.load("http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png")
.bitmapTransform(new GrayscaleTransformation(this))
.into(iv_0);
//其它变换...
可根据情况使用GenericRequestBuilder子类DrawableRequestBuilder的bitmapTransform(Transformation<Bitmap>... bitmapTransformations)
,transform(BitmapTransformation... transformations)
,transform(Transformation<GifBitmapWrapper>... transformation)
,或其子类BitmapRequestBuilder的transform(BitmapTransformation... transformations)
,transform(Transformation<Bitmap>... transformations)
方法。
当然如果想自己写Transformation:
最简单的方式就是继承BitmapTransformation
:
private static class MyTransformation extends BitmapTransformation {
public MyTransformation(Context context) {
super(context);
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform,
int outWidth, int outHeight) {
Bitmap myTransformedBitmap = ... //对Bitmap进行各种变换处理。
return myTransformedBitmap;
}
@Override
public String getId() {
// 返回代表该变换的唯一Id,会作为cache key的一部分。
// 注意:最好不要用getClass().getName(),因为容易受混淆影响。如果变换过程不影响缓存数据,可以返回空字符串。
return "com.example.myapp.MyTransformation";
}
}
使用时只需使用transform()
或bitmapTransform()
方法即可:
Glide.with(yourFragment)
.load(yourUrl)
.asBitmap()
.transform(new MyTransformation(context))
.into(yourView);
自定义图片处理时Glide会自动计算View/Target大小,我们不需要传View的宽高,当然你可以使用override(int, int)
去改变这种行为。
自定义图片处理时,为了避免创建大量Bitmap以及减少GC,可以考虑重用Bitmap,这就需要BitmapPool
,典型地就是,从Bitmap池中拿一个Bitmap,用这个Bitmap生成一个Canvas, 然后在这个Canvas上画初始的Bitmap并使用Matrix、Paint、或者Shader处理这张图片。
为了有效并正确重用Bitmap需要遵循以下三条准则:
- 永远不要把
transform()
传给你的原始resource或原始Bitmap给recycle()
了,更不要放回BitmapPool
,因为这些都自动完成了。值得注意的是,任何从BitmapPool
取出的用于自定义图片变换的辅助Bitmap,如果不经过transform()
方法返回,就必须主动放回BitmapPool
或者调用recycle()
回收。
- 如果你从BitmapPool拿出多个Bitmap或不使用你从BitmapPool拿出的一个Bitmap,一定要返回extras给BitmapPool。
- 如果你的图片处理没有替换原始resource(例如由于一张图片已经匹配了你想要的尺寸,你需要提前返回), transform()`方法就返回原始resource或原始Bitmap。
如:
private static class MyTransformation extends BitmapTransformation {
public MyTransformation(Context context) {
super(context);
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
Bitmap result = pool.get(outWidth, outHeight, Bitmap.Config.ARGB_8888);
// 如果BitmapPool中找不到符合该条件的Bitmap,get()方法会返回null,就需要我们自己创建Bitmap了
if (result == null) {
// 如果想让Bitmap支持透明度,就需要使用ARGB_8888
result = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
}
//创建最终Bitmap的Canvas.
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setAlpha(128);
// 将原始Bitmap处理后画到最终Bitmap中
canvas.drawBitmap(toTransform, 0, 0, paint);
// 由于我们的图片处理替换了原始Bitmap,就return我们新的Bitmap就行。
// Glide会自动帮我们回收原始Bitmap。
return result;
}
@Override
public String getId() {
// Return some id that uniquely identifies your transformation.
return "com.example.myapp.MyTransformation";
}
}
也可以直接实现Transformation
接口,进行更灵活的图片处理,如进行简单地圆角处理:
public class RoundedCornersTransformation implements Transformation<Bitmap> {
private BitmapPool mBitmapPool;
private int mRadius;
public RoundedCornersTransformation(Context context, int mRadius) {
this(Glide.get(context).getBitmapPool(), mRadius);
}
public RoundedCornersTransformation(BitmapPool mBitmapPool, int mRadius) {
this.mBitmapPool = mBitmapPool;
this.mRadius = mRadius;
}
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
//从其包装类中拿出Bitmap
Bitmap source = resource.get();
int width = source.getWidth();
int height = source.getHeight();
Bitmap result = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
//以上已经算是教科书式写法了
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
canvas.drawRoundRect(new RectF(0, 0, width, height), mRadius, mRadius, paint);
//返回包装成Resource的最终Bitmap
return BitmapResource.obtain(result, mBitmapPool);
}
@Override
public String getId() {
return "RoundedTransformation(radius=" + mRadius + ")";
}
}
-
对请求状态进行监听:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private ImageView iv_0;
private LoggingListener mCommonRequestListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_0 = (ImageView) findViewById(R.id.iv_0);
mCommonRequestListener = new LoggingListener<String, GlideDrawable>();
Glide
.with(this)
.load("http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png")
.listener(mCommonRequestListener)
.into(iv_0);
}
}
/**
* @param <A> model类型
* @param <B> resource类型
*/
public class LoggingListener<A, B> implements RequestListener<A, B> {
private final int level;
private final String name;
private final RequestListener<A, B> delegate;
public LoggingListener() {
this("");
}
public LoggingListener(@NonNull String name) {
this(Log.VERBOSE, name);
}
public LoggingListener(int level, @NonNull String name) {
this(level, name, null);
}
public LoggingListener(RequestListener<A, B> delegate) {
this(Log.VERBOSE, "", delegate);
}
public LoggingListener(int level, @NonNull String name, RequestListener<A, B> delegate) {
this.level = level;
this.name = name;
this.delegate = delegate == null ? NoOpRequestListener.<A, B>get() : delegate;
}
@Override
public boolean onException(Exception e, A model, Target<B> target, boolean isFirstResource) {
android.util.Log.println(level, "GLIDE", String.format(Locale.ROOT,
"%s.onException(%s, %s, %s, %s)\n%s",
name, e, model, strip(target), isFirst(isFirstResource), android.util.Log.getStackTraceString(e)));
return delegate.onException(e, model, target, isFirstResource);
}
@Override
public boolean onResourceReady(B resource, A model, Target<B> target, boolean isFromMemoryCache,
boolean isFirstResource) {
String resourceString = strip(getResourceDescription(resource));
String targetString = strip(getTargetDescription(target));
android.util.Log.println(level, "GLIDE", String.format(Locale.ROOT,
"%s.onResourceReady(%s, %s, %s, %s, %s)",
name, resourceString, model, targetString, isMem(isFromMemoryCache), isFirst(isFirstResource)));
return delegate.onResourceReady(resource, model, target, isFromMemoryCache, isFirstResource);
}
private String isMem(boolean isFromMemoryCache) {
return isFromMemoryCache ? "sync" : "async";
}
private String isFirst(boolean isFirstResource) {
return isFirstResource ? "first" : "not first";
}
private String getTargetDescription(Target<B> target) {
String result;
if (target instanceof ViewTarget) {
View v = ((ViewTarget) target).getView();
LayoutParams p = v.getLayoutParams();
result = String.format(Locale.ROOT,
"%s(params=%dx%d->size=%dx%d)", target, p.width, p.height, v.getWidth(), v.getHeight());
} else {
result = String.valueOf(target);
}
return result;
}
private String getResourceDescription(B resource) {
String result;
if (resource instanceof Bitmap) {
Bitmap bm = (Bitmap) resource;
result = String.format(Locale.ROOT,
"%s(%dx%d@%s)", resource, bm.getWidth(), bm.getHeight(), bm.getConfig());
} else if (resource instanceof BitmapDrawable) {
Bitmap bm = ((BitmapDrawable) resource).getBitmap();
result = String.format(Locale.ROOT,
"%s(%dx%d@%s)", resource, bm.getWidth(), bm.getHeight(), bm.getConfig());
} else if (resource instanceof GlideBitmapDrawable) {
Bitmap bm = ((GlideBitmapDrawable) resource).getBitmap();
result = String.format(Locale.ROOT,
"%s(%dx%d@%s)", resource, bm.getWidth(), bm.getHeight(), bm.getConfig());
} else if (resource instanceof Drawable) {
Drawable d = (Drawable) resource;
result = String.format(Locale.ROOT,
"%s(%dx%d)", resource, d.getIntrinsicWidth(), d.getIntrinsicHeight());
} else {
result = String.valueOf(resource);
}
return result;
}
private static String strip(Object text) {
return String.valueOf(text).replaceAll("(com|android|net|org)(\\.[a-z]+)+\\.", "");
}
}
public final class NoOpRequestListener<A, B> implements RequestListener<A, B> {
private static final RequestListener INSTANCE = new NoOpRequestListener();
@SuppressWarnings("unchecked")
public static <A, B> RequestListener<A, B> get() {
return INSTANCE;
}
private NoOpRequestListener() {
}
@Override public boolean onException(Exception e, A a, Target<B> target, boolean b) {
return false;
}
@Override public boolean onResourceReady(B b, A a, Target<B> target, boolean b2, boolean b1) {
return false;
}
}
通过GenericRequestBuilder的listener()
方法添加一个RequestListener
实现,但要注意,最好不要用匿名类,也不要每次都创建新的监听器,要使用单例进行统一监听处理,以避免不必要的内存申请和不必要的引用。方法最好返回false,以便Glide能继续进行后续处理(如显示error占位符)。
- 对资源的下载进度进行监听:
可以借助OkHttp的拦截器进行进度监听。OkHttp的拦截器官方Sample请移步这里。我们可以利用这个拦截器进行监听并处理,需要自定义ModelLoader和DataFetcher,具体请详见我的Git:https://github.com/shangmingchao/ProgressGlide,欢迎Star啊,不过没有太大必要告诉用户图片加载的进度(sjudd和TWiStErRob他们说的)。同时也可以看一下TWiStErRob大神的实现(自备梯子哈~.~)。