Glide 是一個android平台上的快速和高效的開源的多媒體資源管理庫, 提供 多媒體檔案的壓縮,記憶體和磁碟緩衝, 資源集區的介面。
Glide 支援擷取,解壓展示視頻, 映像和GIFs, Glide有一個可彈性的api可以讓開發人員自訂網路棧技術, 預設使用HttpUrlConnection , 你可以替換為 Google's Volley或者 OkHttp
Glide 開始的目的是提供一個快速和平滑展示圖片列表, 但是Glide對你需要拉取, resize 和展示遠端圖片這些情境都是很管用的.
Glide最簡單的使用案例就是從遠程伺服器或者本地檔案系統載入圖片,把它們放在磁碟與記憶體緩衝中,然後載入到view上。它可以用在全市圖片的app中,Glide為包含圖片的滾動列表做了儘可能流暢的最佳化。
對象池
Glide原理的核心是為bitmap維護一個對象池。對象池的主要目的是通過減少大對象的分配以重用來提高效能。
Dalvik和ART虛擬機器都沒有使用compacting garbage collector,compacting garbage collector是一種模式,這種模式中GC會遍曆堆,同時把活躍對象移到相鄰記憶體地區,讓更大的記憶體塊可以用在後續的分配中。因為安卓沒有這種模式,就可能會出現被分配的對象分散在各處,對象之間只有很小的記憶體可用。如果應用試圖分配一個大於鄰近的閑置記憶體塊空間的對象,就會導致OutOfMemoryError,然後崩潰,即使總的空餘記憶體空間大於對象的大小。
使用對象池還可以協助提高滾動的效能,因為重用bitmap意味著更少的對象被建立與回收。記憶體回收會導致“停止一切(Stop The World)”事件,這個事件指的是回收器執行期間,所有線程(包括UI線程)都會暫停。這個時候,映像幀無法被渲染同時UI可能會停滯,這在滾動期間尤其明顯。
下載
Glide的GitHub項目地址 https://github.com/bumptech/glide
可以在github上下載 release page.
或者使用Gradle:
repositories { mavenCentral()}dependencies { compile 'com.github.bumptech.glide:glide:3.3.+' compile 'com.android.support:support-v4:19.1.0'}repositories { mavenCentral()} dependencies { compile 'com.github.bumptech.glide:glide:3.3.+' compile 'com.android.support:support-v4:19.1.0'}
Maven
<dependency> <groupId>com.github.bumptech.glide</groupId> <artifactId>glide</artifactId> <version>3.3.1</version> <type>aar</type></dependency><dependency> <groupId>com.google.android</groupId> <artifactId>support-v4</artifactId> <version>r7</version></dependency><dependency> <groupId>com.github.bumptech.glide</groupId> <artifactId>glide</artifactId> <version>3.3.1</version> <type>aar</type></dependency><dependency> <groupId>com.google.android</groupId> <artifactId>support-v4</artifactId> <version>r7</version></dependency>
使用
Glide使用起來很簡單,而且不需要任何特別的配置就自動包含了bitmap pooling 。
DrawableRequestBuilder requestBuilder = Glide.with(context).load(imageUrl);requestBuilder.into(imageView);
這就是載入一張圖片的全部要求。就像安卓中的很多地方一樣,with() 方法中的context到底是哪種類型是不清楚的。有一點很重要需要記住,就是傳入的context類型影響到Glide載入圖片的最佳化程度,Glide可以監視activity的生命週期,在activity銷毀的時候自動取消等待中的請求。但是如果你使用Application context,你就失去了這種最佳化效果。
註:其實以上的代碼是一種比較規範的寫法,我們更熟悉的寫法是:
Glide.with(context) .load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg") .into(ivImg);
簡單的例子
// For a simple view:@Overridepublic void onCreate(Bundle savedInstanceState) { ... ImageView imageView = (ImageView) findViewById(R.id.my_image_view); Glide.with(this).load("http://goo.gl/h8qOq7").into(imageView);}// For a list:@Overridepublic View getView(int position, View recycled, ViewGroup container) { final ImageView myImageView; if (recycled == null) { myImageView = (ImageView) inflater.inflate(R.layout.my_image_view, container, false); } else { myImageView = (ImageView) recycled; } String url = myUrls.get(position); Glide.with(myFragment) .load(url) .centerCrop() .placeholder(R.drawable.loading_spinner) .crossFade() .into(myImageView); return myImageView;}// For a simple view:@Overridepublic void onCreate(Bundle savedInstanceState) { ... ImageView imageView = (ImageView) findViewById(R.id.my_image_view); Glide.with(this).load("http://goo.gl/h8qOq7").into(imageView);} // For a list:@Overridepublic View getView(int position, View recycled, ViewGroup container) { final ImageView myImageView; if (recycled == null) { myImageView = (ImageView) inflater.inflate(R.layout.my_image_view, container, false); } else { myImageView = (ImageView) recycled; } String url = myUrls.get(position); Glide.with(myFragment) .load(url) .centerCrop() .placeholder(R.drawable.loading_spinner) .crossFade() .into(myImageView); return myImageView;}
Volley
如果你想使用Volley:
Gradle
dependencies { compile 'com.github.bumptech.glide:volley-integration:1.0.+' compile 'com.mcxiaoke.volley:library:1.0.+'}dependencies { compile 'com.github.bumptech.glide:volley-integration:1.0.+' compile 'com.mcxiaoke.volley:library:1.0.+'}
Maven:
<dependency> <groupId>com.github.bumptech.glide</groupId> <artifactId>volley-integration</artifactId> <version>1.0.1</version> <type>jar</type></dependency><dependency> <groupId>com.mcxiaoke.volley</groupId> <artifactId>library</artifactId> <version>1.0.5</version> <type>aar</type></dependency><dependency> <groupId>com.github.bumptech.glide</groupId> <artifactId>volley-integration</artifactId> <version>1.0.1</version> <type>jar</type></dependency><dependency> <groupId>com.mcxiaoke.volley</groupId> <artifactId>library</artifactId> <version>1.0.5</version> <type>aar</type></dependency>
然後在你的Activity或者程式中,註冊Volley為基本模組
public void onCreate() { Glide.get(this).register(GlideUrl.class, InputStream.class, new VolleyUrlLoader.Factory(yourRequestQueue)); ...}public void onCreate() { Glide.get(this).register(GlideUrl.class, InputStream.class, new VolleyUrlLoader.Factory(yourRequestQueue)); ...}
OkHttp
Gradle:
dependencies { compile 'com.github.bumptech.glide:okhttp-integration:1.0.+' compile 'com.squareup.okhttp:okhttp:2.0.+'}dependencies { compile 'com.github.bumptech.glide:okhttp-integration:1.0.+' compile 'com.squareup.okhttp:okhttp:2.0.+'}
Maven:
<dependency> <groupId>com.github.bumptech.glide</groupId> <artifactId>okhttp-integration</artifactId> <version>1.0.1</version> <type>jar</type></dependency><dependency> <groupId>com.squareup.okhttp</groupId> <artifactId>okhttp</artifactId> <version>2.0.0</version> <type>jar</type></dependency><dependency> <groupId>com.github.bumptech.glide</groupId> <artifactId>okhttp-integration</artifactId> <version>1.0.1</version> <type>jar</type></dependency><dependency> <groupId>com.squareup.okhttp</groupId> <artifactId>okhttp</artifactId> <version>2.0.0</version> <type>jar</type></dependency>
然後在你的Activity或者程式中,註冊Volley為基本模組
public void onCreate() { Glide.get(this).register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(yourOkHttpClient)); ...}public void onCreate() { Glide.get(this).register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(yourOkHttpClient)); ...}