1.Gradle引入
compile 'com.github.bumptech.glide:glide:3.7.0'
2.Glide最简单使用
Glide.with(PhotoPreviewsActivity.this).load(path).into(iv_show);
2.1这里的with在源码里有4中类型
* @see #with(android.app.Activity)
* @see #with(android.app.Fragment)
* @see #with(android.support.v4.app.Fragment)
* @see #with(android.support.v4.app.FragmentActivity)
*
* @param context Any context, will not be retained.
* @return A RequestManager for the top level application that can be used to start a load.
*/
public static RequestManager with(Context context) {
RequestManagerRetriever retriever = RequestManagerRetriever.get();
return retriever.get(context);
}
2.2这里的load参数在源码里给的可以是文件路径,uri或url
* Returns a request builder to load the given {@link java.lang.String}.
* signature.
*
* @see #fromString()
* @see #load(Object)
*
* @param string A file path, or a uri or url handled by {@link com.bumptech.glide.load.model.UriLoader}.
*/
public DrawableTypeRequest<String> load(String string) {
return (DrawableTypeRequest<String>) fromString().load(string);
}
2.3这里的into就是要显示的控件,但是在源码里有这样一句话,如果没有transformation,就会有默认的transformation,它的值是getScaleType(),如果不想用transformation,则使用dontTransform()。
* Note - If no transformation is set for this load, a default transformation will be applied based on the
* value returned from {@link android.widget.ImageView#getScaleType()}. To avoid this default transformation,
* use {@link #dontTransform()}.
* </p>
*
* @param view {@inheritDoc}
* @return {@inheritDoc}
*/
@Override
public Target<GlideDrawable> into(ImageView view) {
return super.into(view);
}
3.Glide还有以下常用的设置:
3.1 placeholder() 加载过程中的占位图,值可以是Drawable或resouceId
3.2 error() 加载失败显示的图,值可以是Drawable或resouceId
3.3 override(int width,int height)设置加载图片的大小,值可以是Drawable或resouceId
3.4 dontAnimate()设置淡入淡出的加载动画,其作用效果和crossFade(1000)一样,但是crossFade可以设置时间,也可以不设置
3.5 animate()自定义动画
3.6 priority(Priority.NORMAL) 设置优先级,优先级越高就会越先加载,其值可以取:
public enum Priority { IMMEDIATE, HIGH, NORMAL, LOW, priority, }
3.7 asBitmap() 当成bitmap加载,如果是gif则停留在第一帧。
3.8 skipMemoryCache(false) 跳过内存缓存。
3.9 diskCacheStrategy(DiskCacheStrategy.NONE) 。设置磁盘缓存模式:它的值有以下几种:
public enum DiskCacheStrategy {
/** Caches with both {@link #SOURCE} and {@link #RESULT}. */
ALL(true, true), //缓存所有数据
/** Saves no data to cache. */
NONE(false, false), //不缓存数据
/** Saves just the original data to cache. */
SOURCE(true, false), //缓存源数据
/** Saves the media item after all transformations to cache. */
RESULT(false, true);//缓存转换后的数据
}
3.10 thumbnail(0.1f) 设置缩略图
3.11 asGif() 加载gif图片的时候使用,如果不是gif,则会加载失败
3.12 Glide.get(this).clearDiskCache() 清理磁盘缓存,子线程中进行
3.13 Glide.get(this).clearMemory() 清理内存缓存,子线程中进行
4.图片加载会闪烁
去掉crossFade(1000) 和dontAnimate()动画
5.图片加载缓慢,而且会出现短暂的空白,还显示不全
1.去掉crossFade(1000) 和dontAnimate()动画
2.可以使用先加载缩略图thumbnail
3.也可以使用placeholder(imgView.getDrawable()),先加载占位图,再加载要显示的图片
4.我觉得不要跳过内存缓存skipMemoryCache(false)
5.对于显示不全,可以对imageview设置scaleType为 centerCrop,因为比例不会变
//第一种缓存图片方式
void cacheImage(final String imgUrl,final ImageView imgView)
Glide.with(this).load(imgUrl).skipMemoryCache(false).placeholder(imgView.getDrawable()).priority (Priority.HIGH ).listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
// Log.d("GlideLoad","onException"+"path="+imgUrl);
cacheImage(imgUrl,imgView);
//加载异常后的回调方法
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
// Log.d("GlideLoad","onResourceReady"+"path="+imgUrl);
//加载完成后的回调方法
return false;
}
}).diskCacheStrategy(DiskCacheStrategy.SOURCE)/*.thumbnail(0.1f)*//*.crossFade(1000)*//*.dontAnimate()*/.into(imgView);
}
//第二种缓存图片方式
Glide.with(context) .load(imgUrl).downloadOnly(Target.SIZE_ORIGINAL,Target.SIZE_ORIGINAL).get();
6.Glide的磁盘缓存地址: