前面總結學習了圖片的使用以及Lru算法,今天來學習一下比較優秀的圖片緩存開源框架。技術自己就要不斷的更迭,從最初的本身使用SoftReference實現本身的圖片緩存,到後來作電商項目本身的實現方案不能知足項目的需求改用Afinal,因爲Afinal再也不維護而選擇了師出同門的Xutils,中間也接觸過別的開源框架好比Picasso,對Picasso的第一次印象就不太好,初次接觸是拿到了公司剛從外包公司接手過來的圖片社交類app,對內存佔用太大,直接感覺就是致使ListView滑動有那麼一點卡頓,老牌的圖片緩存框架universalImageLoader據說過一直沒有真正使用過,以前項目都很小,差很少幾百萬級別的app,一直使用的都是Xutils,最近以爲項目大起來了,萬一Xutils不維護了或者說要求支持的圖片格式多起來的時候,可能Xutils就不是最佳選擇了,這也是來學習Gilde的根本動機吧。其實原本想着去學習Facebook的Fresco圖片框架,可是簡單的看了一下,須要連同自定義控件一塊兒使用,功能雖然強大,可是對於已經在維護的項目修改爲本那可不是通常的高,之後有興趣在學習吧!android
Glide 是 Google 員工的開源項目, Google I/O 上被推薦使用,一個高效、開源、Android設備上的媒體管理框架,它遵循BSD、MIT以及Apache 2.0協議發佈。Glide具備獲取、解碼和展現視頻劇照、圖片、動畫等功能,它還有靈活的API,這些API使開發者可以將Glide應用在幾乎任何網絡協議棧裏。建立Glide的主要目的有兩個,一個是實現平滑的圖片列表滾動效果,另外一個是支持遠程圖片的獲取、大小調整和展現。git
gitHub地址:https://github.com/bumptech/glidegithub
compile 'com.github.bumptech.glide:glide:3.7.0'
咱們能夠更加高效的使用Glide提供的方式進行綁定,這樣能夠更好的讓加載圖片的請求的生命週期動態管理起來web
Glide.with(Context context);// 綁定Context Glide.with(Activity activity);// 綁定Activity Glide.with(FragmentActivity activity);// 綁定FragmentActivity Glide.with(Fragment fragment);// 綁定Fragment
Glide.with(this).load(imageUrl).into(imageView);
api裏面對placeholder()、error()函數中有多態實現 用的時候能夠具體的熟悉一下算法
Glide.with(this).load(imageUrl).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(imageView);
Glide.with(this).load(imageUrl).skipMemoryCache(true).into(imageView);
Glide.with(this).load(imageUrl).priority(Priority.NORMAL).into(imageView);
Glide.with(this).load(imageUrl).diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
策略解說:canvas
all:緩存源資源和轉換後的資源api
none:不做任何磁盤緩存緩存
source:緩存源資源網絡
result:緩存轉換後的資源app
api也提供了幾個經常使用的動畫:好比crossFade()
Glide.with(this).load(imageUrl).animate(R.anim.item_alpha_in).into(imageView);
R.anim.item_alpha_in
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:duration="500" android:fromAlpha="0.0" android:toAlpha="1.0"/> </set>
這樣會先加載縮略圖 而後在加載全圖
Glide.with(this).load(imageUrl).thumbnail(0.1f).into(imageView);
Glide.with(this).load(imageUrl).override(800, 800).into(imageView);
Glide.with(this).load(imageUrl).centerCrop().into(imageView);
api提供了好比:centerCrop()、fitCenter()等函數也能夠經過自定義Transformation,舉例說明:好比一我的圓角轉化器
public class GlideRoundTransform extends BitmapTransformation { private float radius = 0f; public GlideRoundTransform(Context context) { this(context, 4); } public GlideRoundTransform(Context context, int dp) { super(context); this.radius = Resources.getSystem().getDisplayMetrics().density * dp; } @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { return roundCrop(pool, toTransform); } private Bitmap roundCrop(BitmapPool pool, Bitmap source) { if (source == null) return null; Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); if (result == null) { result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(result); Paint paint = new Paint(); paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); paint.setAntiAlias(true); RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight()); canvas.drawRoundRect(rectF, radius, radius, paint); return result; } @Override public String getId() { return getClass().getName() + Math.round(radius); } }
具體使用
Glide.with(this).load(imageUrl).transform(new GlideRoundTransform(this)).into(imageView);
項目中有不少須要先下載圖片而後再作一些合成的功能,好比項目中出現的圖文混排,該如何實現目標下
Glide.with(this).load(imageUrl).centerCrop().into(new SimpleTarget<GlideDrawable>() { @Override public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) { imageView.setImageDrawable(resource); } });
Glide.with(this).load(imageUrl).listener(new RequestListener<String, GlideDrawable>() { @Override public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) { return false; } @Override public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) { //imageView.setImageDrawable(resource); return false; } }).into(imageView);
設置監聽的用處 能夠用於監控請求發生錯誤來源,以及圖片來源 是內存仍是磁盤
Glide.with(this).load(imageUrl).asBitmap().into(imageView);//顯示gif靜態圖片 Glide.with(this).load(imageUrl).asGif().into(imageView);//顯示gif動態圖片
Glide.get(this).clearDiskCache();//清理磁盤緩存 須要在子線程中執行 Glide.get(this).clearMemory();//清理內存緩存 能夠在UI主線程中進行
以上是Glide的常規用法,基本上知足開發須要了,而後再去學習一下其餘相關知識。