Glide之前一直用的是3.7版本,但是後面在使用的過程中經常遇到各種各樣的奇葩問題,解決問題需要花很長時間去百度谷歌,因此將自己日常使用的方法記錄下來,下次就可以直接ctrl+C了,,,,,
- 集成
- 混淆
- 用法介紹
- 集成
repositories {
mavenCentral()
google()
}
dependencies {
implementation 'com.github.bumptech.glide:glide:4.6.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'
}
- 混淆
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep public enum com.bumptech.glide.load.ImageHeaderParser$** {
**[] $VALUES;
public *;
}
# for DexGuard only
-keepresourcexmlelements manifest/application/meta-data@value=GlideModule
- 用法介紹
基本用法同3.7版本一致,代碼如下:
Glide.with(this).load(url).into(imageView);
- 添加佔位圖
RequestOptions options = new RequestOptions()
.placeholder(R.drawable.loading);
Glide.with(this)
.load(url)
.apply(options)
.into(imageView);
- 關閉磁盤緩存
RequestOptions options = new RequestOptions()
.placeholder(R.drawable.loading)
.diskCacheStrategy(DiskCacheStrategy.NONE);
Glide.with(this)
.load(url)
.apply(options)
.into(imageView);
佔位圖+異常圖+磁盤緩存
RequestOptions options = new RequestOptions()
.placeholder(R.drawable.ic_launcher_background)
.error(R.drawable.error)
.diskCacheStrategy(DiskCacheStrategy.NONE);
Glide.with(this)
.load(url)
.apply(options)
.into(imageView);
- 指定圖片資源大小
RequestOptions options = new RequestOptions()
.override(300, 400);
Glide.with(this)
.load(url)
.apply(options)
.into(imageView);
- 加載原圖大小 圖片不被壓縮
RequestOptions options = new RequestOptions()
.override(Target.SIZE_ORIGINAL);
Glide.with(this)
.load(url)
.apply(options)
.into(imageView);
- 禁用內存緩存
RequestOptions options = new RequestOptions()
.skipMemoryCache(true);
Glide.with(this)
.load(url)
.apply(options)
.into(imageView);
禁用磁盤緩存:
DiskCacheStrategy.NONE: 表示不緩存任何內容
- DiskCacheStrategy.DATA: 表示只緩存原始圖片
- DiskCacheStrategy.RESOURCE: 表示只緩存轉換過後的圖片
- DiskCacheStrategy.ALL : 表示既緩存原始圖片,也緩存轉換過後的圖片
- DiskCacheStrategy.AUTOMATIC: 表示讓Glide根據圖片資源智能地選擇使用哪一種緩存策略(默認選項)
RequestOptions options = new RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.NONE);
Glide.with(this)
.load(url)
.apply(options)
.into(imageView);
- 指定加載格式
(默認自動加載圖片格式,如加載gif動圖)
Glide.with(this)
.load("http://guolin.tech/test.gif")
.into(imageView);
- 指定靜態圖片
Glide.with(this)
.asBitmap()
.load("http://guolin.tech/test.gif")
.into(imageView);
- 獲取圖片資源
只獲取圖片資源(res圖片、file圖片、服務端圖片),不加載到視圖上
備註:SimpleTarget的泛型可變,比如BitmapDrawable等
SimpleTarget<Drawable> simpleTarget = new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
imageView.setImageDrawable(resource);
}
};
public void loadImage(View view) {
Glide.with(this)
.load("http://guolin.tech/book.png")
.into(simpleTarget);
}
- 獲取圖片文件對象
submit()方法同上面代碼的效果類似,就是下載圖片文件(得到是圖片文件,而非圖片資源對象),且必須要用在子線程當中,因爲FutureTarget的get()方法是會阻塞線程的。(注意避免內存泄漏,儘量使用ApplicationContext)
submit()方法是用於下載原始尺寸的圖片,而submit(int width, int height)則可以指定下載圖片的尺寸。
String url = "http://www.guolin.tech/book.png";
final Context context = getApplicationContext();
FutureTarget<File> target = Glide.with(context)
.asFile()
.load(url)
.submit();
final File imageFile = target.get();
- 預加載
跟獲取圖片資源類似,只是前者可獲得圖片資源,而預加載是不獲取圖片資源
備註:preload()方法有兩個方法重載,一個不帶參數,表示將會加載圖片的原始尺寸,另一個可以通過參數指定加載圖片的寬和高。
Glide.with(this)
.load("http://guolin.tech/book.png")
.preload();
listener()方法
listener()方法的作用非常普遍,它可以用來監聽Glide加載圖片的狀態。舉個例子,比如說我們上面使用了preload()方法來對圖片進行預加載,但是我怎樣確定預加載有沒有完成呢?還有如果Glide加載圖片失敗了,我該怎樣調試錯誤的原因呢?答案都在listener()方法當中。
下面來看下listener()方法的基本用法吧,不同於剛纔幾個方法都是要替換into()方法的,listener()是結合into()方法一起使用的,當然也可以結合preload()方法一起使用。備註:onResourceReady()方法和onLoadFailed()方法都有一個布爾值的返回值,返回false就表示這個事件沒有被處理,還會繼續向下傳遞,返回true就表示這個事件已經被處理掉了,從而不會再繼續向下傳遞,用事件分發機制的屬於叫做消費了當前的事件。
Glide.with(this)
.load("http://www.guolin.tech/book.png")
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
return false;
}
})
.into(imageView);
- 圖片變換
RequestOptions options = new RequestOptions()
.centerCrop();
RequestOptions options = new RequestOptions()
.fitCenter();
RequestOptions options = new RequestOptions()
.circleCrop();
Glide.with(this)
.load(url)
.apply(options)
.into(imageView);
自定義圖片變換
public class CircleCrop extends BitmapTransformation {
public CircleCrop(Context context) {
super(context);
}
public CircleCrop(BitmapPool bitmapPool) {
super(bitmapPool);
}
@Override
public String getId() {
//保證該字段的唯一
return "com.example.glidetest.CircleCrop";
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
//transform 自定義
int diameter = Math.min(toTransform.getWidth(), toTransform.getHeight());
final Bitmap toReuse = pool.get(outWidth, outHeight, Bitmap.Config.ARGB_8888);
final Bitmap result;
if (toReuse != null) {
result = toReuse;
} else {
result = Bitmap.createBitmap(diameter, diameter, Bitmap.Config.ARGB_8888);
}
int dx = (toTransform.getWidth() - diameter) / 2;
int dy = (toTransform.getHeight() - diameter) / 2;
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
BitmapShader shader = new BitmapShader(toTransform, BitmapShader.TileMode.CLAMP,
BitmapShader.TileMode.CLAMP);
if (dx != 0 || dy != 0) {
Matrix matrix = new Matrix();
matrix.setTranslate(-dx, -dy);
shader.setLocalMatrix(matrix);
}
paint.setShader(shader);
paint.setAntiAlias(true);
float radius = diameter / 2f;
canvas.drawCircle(radius, radius, radius, paint);
if (toReuse != null && !pool.put(toReuse)) {
toReuse.recycle();
}
return result;
}
}
總結
前兩天,我開發登錄頁的時候,遇到圖片驗證碼刷新,發現每次都是加載的圖片完全一樣的,並沒有完成刷新(圖片url一樣)!一看這個情形我就知道,是Glide自動緩存的“功勞”,但是百度了好久,測試都不行,如常見的方法:
GlideApp.with(fragment)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(view);
最後發現代碼編譯並未生成GlideApp對象,按照網上的方法繼承、Clean等等操作半天,結果還是並未實現。
後來通過在QQ羣請教,得到了正確而簡潔的方法:
RequestOptions options = new RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true);
Glide.with(this)
.load(url)
.apply(options)
.into(imageView);
後來我再次請教對方這個知識點的來源,結果是來自郭神的專欄Glide的全面解析,而這個專欄自己是知道的,並在第二章看了不止五次(當然,還是說不出這篇文章的脈絡的),但是後面的文章並未來得及拜讀。最後,自己將整個專欄挑着閱讀了一遍,對自己比較感興趣的緩存、圖片變換等章節細細品味,覺得收穫頗大。而本文的所有知識點均來自郭神的Glide最全解析第八章Android圖片加載框架最全解析(八),帶你全面瞭解Glide 4的用法