Gilde 定義
Gilde 是google開(kāi)源的圖片加載庫(kù)术瓮。
glide依賴
//glide
compile 'com.github.bumptech.glide:glide:4.0.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.0.0'
glide 基礎(chǔ)用法
glide 用法傳承目前流行的鏈?zhǔn)秸{(diào)用。
/**
* 基礎(chǔ)用法.
*/
private void baseUsed(){
Glide.with(this)
.load(URL)
.into(mImageView);
}
glide 可以用RequestOptions配置各種參數(shù):
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: 錯(cuò)誤圖片.
- fallback: 當(dāng)url為null的時(shí)候胞四,判斷是否設(shè)置了fallback,是的話則顯示fallback圖片伶椿,否的話顯示error圖片辜伟,如果error還是沒(méi)有設(shè)置則顯示placeholder圖片.
這幾個(gè)圖片顯示規(guī)則:
glide RequestBuilders還可以重用開(kāi)始多個(gè)加載
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,通過(guò)annotation將option封裝自動(dòng)生成代碼脊另。
第一步必須自定義一個(gè)module 繼承于AppGlideModule导狡。
@GlideModule
public final class MyGlideModule extends AppGlideModule {
}
注意: AppGlideModule實(shí)現(xiàn)必須始終與@GlideModule注釋一起出現(xiàn).如果注釋不存在,則該模塊將不會(huì)被發(fā)現(xiàn)偎痛。重新編譯旱捧,會(huì)自動(dòng)生成代碼,后面可用GlideApp.with() 代替Glide.with()
1踩麦、設(shè)置手機(jī)默認(rèn)推薦緩存大小枚赡。MemorySizeCalculator類通過(guò)考慮設(shè)備給定的可用內(nèi)存和屏幕大小想出合理的默認(rèn)大小.
/**
* MemorySizeCalculator類通過(guò)考慮設(shè)備給定的可用內(nèi)存和屏幕大小想出合理的默認(rèn)大小.
* 通過(guò)LruResourceCache進(jìn)行緩存。
* @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靖榕、自定義內(nèi)存緩存大小
/**
* 自定義緩存大小.
* @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.自定義內(nèi)置磁盤緩存大小
/**
* Disk Cache.自定義內(nèi)置磁盤緩存大小.
* @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.自定義內(nèi)置磁盤緩存大小并指定路徑.
/**
* Disk Cache.自定義內(nèi)置磁盤緩存大小并指定路徑.
* @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 設(shè)置清單解析,設(shè)置為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);
}
設(shè)置完全緩存
/**
* 緩存.
*/
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 只緩存加載后的圖像星压,即處理后最終顯示時(shí)的圖像
- DiskCacheStrategy.ALL 緩存所有版本的圖像(默認(rèn)行為)
只從緩存中讀取践剂,如果緩存沒(méi)有,則失敗.
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);
}
跳過(guò)緩存. 每次都從服務(wù)端獲取最新.
/**
* 跳過(guò)緩存. 每次都從服務(wù)端獲取最新.
* diskCacheStrategy: 磁盤緩存
* skipMemoryCache:內(nèi)存緩存
*/
private void skipCache(){
GlideApp.with(this)
.asBitmap()
.load(URL)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(mImageView);
}
清除緩存
/**
* 清除內(nèi)存緩存.
*/
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;
}
}
調(diào)用:
/**
* 自定義圓形裁剪.
*/
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);
}