Android的多媒體管理庫Glide的基本使用示例
Glide 是一個(gè)android平臺(tái)上的快速和高效的開源的多媒體資源管理庫, 提供 多媒體文件的壓縮,內(nèi)存和磁盤緩存, 資源池的接口。
Glide 支持獲取,解壓展示視頻, 圖像和GIFs, Glide有一個(gè)可彈性的api可以讓開發(fā)者自定義網(wǎng)絡(luò)棧技術(shù), 默認(rèn)使用HttpUrlConnection , 你可以替換為 Google's Volley或者 OkHttp
Glide 開始的目的是提供一個(gè)快速和平滑展示圖片列表, 但是Glide對你需要拉取, resize 和展示遠(yuǎn)程的圖片這些場景都是很管用的.
Glide最簡單的使用案例就是從遠(yuǎn)程服務(wù)器或者本地文件系統(tǒng)加載圖片,把它們放在磁盤與內(nèi)存緩存中,然后加載到view上。它可以用在全市圖片的app中,Glide為包含圖片的滾動(dòng)列表做了盡可能流暢的優(yōu)化。
對象池
Glide原理的核心是為bitmap維護(hù)一個(gè)對象池。對象池的主要目的是通過減少大對象的分配以重用來提高性能。
Dalvik和ART虛擬機(jī)都沒有使用compacting garbage collector,compacting garbage collector是一種模式,這種模式中GC會(huì)遍歷堆,同時(shí)把活躍對象移到相鄰內(nèi)存區(qū)域,讓更大的內(nèi)存塊可以用在后續(xù)的分配中。因?yàn)榘沧繘]有這種模式,就可能會(huì)出現(xiàn)被分配的對象分散在各處,對象之間只有很小的內(nèi)存可用。如果應(yīng)用試圖分配一個(gè)大于鄰近的閑置內(nèi)存塊空間的對象,就會(huì)導(dǎo)致OutOfMemoryError,然后崩潰,即使總的空余內(nèi)存空間大于對象的大小。
使用對象池還可以幫助提高滾動(dòng)的性能,因?yàn)橹赜胋itmap意味著更少的對象被創(chuàng)建與回收。垃圾回收會(huì)導(dǎo)致“停止一切(Stop The World)”事件,這個(gè)事件指的是回收器執(zhí)行期間,所有線程(包括UI線程)都會(huì)暫停。這個(gè)時(shí)候,圖像幀無法被渲染同時(shí)UI可能會(huì)停滯,這在滾動(dòng)期間尤其明顯。

下載
Glide的GitHub項(xiàng)目地址 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使用起來很簡單,而且不需要任何特別的配置就自動(dòng)包含了bitmap pooling 。
DrawableRequestBuilder requestBuilder = Glide.with(context).load(imageUrl); requestBuilder.into(imageView);
這就是加載一張圖片的全部要求。就像安卓中的很多地方一樣,with() 方法中的context到底是哪種類型是不清楚的。有一點(diǎn)很重要需要記住,就是傳入的context類型影響到Glide加載圖片的優(yōu)化程度,Glide可以監(jiān)視activity的生命周期,在activity銷毀的時(shí)候自動(dòng)取消等待中的請求。但是如果你使用Application context,你就失去了這種優(yōu)化效果。
注:其實(shí)以上的代碼是一種比較規(guī)范的寫法,我們更熟悉的寫法是:
Glide.with(context)
.load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg")
.into(ivImg);
簡單的例子
// For a simple view:
@Override
public 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:
@Override
public 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:
@Override
public 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:
@Override
public 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));
...
}
- Android關(guān)于Glide的使用(高斯模糊、加載監(jiān)聽、圓角圖片)
- Android App中使用Glide加載圖片的教程
- Android使用glide加載gif動(dòng)畫設(shè)置播放次數(shù)
- Android Glide的簡單使用
- Android添加glide庫報(bào)錯(cuò)Error: Failed to resolve: com.android.support:support-annotations:26.0.2的解決
- Android中Glide加載庫的圖片緩存配置究極指南
- 從源碼分析Android的Glide庫的圖片加載流程及特點(diǎn)
- Android的Glide庫加載圖片的用法及其與Picasso的對比
- Android中Glide庫的使用小技巧總結(jié)
相關(guān)文章
Android提高之自定義Menu(TabMenu)實(shí)現(xiàn)方法
這篇文章主要介紹了Android自定義Menu(TabMenu)實(shí)現(xiàn)方法,是非常實(shí)用的功能,需要的朋友可以參考下2014-08-08
Jetpack Compose 雙指拖拽實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Jetpack Compose 雙指拖拽實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android RecyclerView自由拖動(dòng)item的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android RecyclerView自由拖動(dòng)item的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01
利用Android實(shí)現(xiàn)一種點(diǎn)贊動(dòng)畫效果的全過程
最近做項(xiàng)目需要實(shí)現(xiàn)點(diǎn)贊動(dòng)畫,下面這篇文章主要給大家介紹了關(guān)于利用Android實(shí)現(xiàn)一種點(diǎn)贊動(dòng)畫效果的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
Android7.0實(shí)現(xiàn)拍照和相冊選取圖片功能
這篇文章主要為大家詳細(xì)介紹了Android7.0實(shí)現(xiàn)拍照和相冊選取圖片功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android自定義多節(jié)點(diǎn)進(jìn)度條顯示的實(shí)現(xiàn)代碼(附源碼)
這篇文章主要介紹了Android自定義多節(jié)點(diǎn)進(jìn)度條顯示的實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
Android中使用SharedPreferences完成記住賬號(hào)密碼的功能
這篇文章主要介紹了Android中使用SharedPreferences完成記住賬號(hào)密碼的功能,需要的朋友可以參考下2017-08-08
Android TagCloudView云標(biāo)簽的使用方法
這篇文章主要為大家詳細(xì)介紹了Android TagCloudView云標(biāo)簽的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android利用Andserver搭建服務(wù)器的詳細(xì)教程
這篇文章主要介紹了Android利用Andserver搭建服務(wù)器的教程,本文通過圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06

