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對(duì)你需要拉取, resize 和展示遠(yuǎn)程的圖片這些場(chǎng)景都是很管用的.
Glide最簡(jiǎn)單的使用案例就是從遠(yuǎn)程服務(wù)器或者本地文件系統(tǒng)加載圖片,把它們放在磁盤與內(nèi)存緩存中,然后加載到view上。它可以用在全市圖片的app中,Glide為包含圖片的滾動(dòng)列表做了盡可能流暢的優(yōu)化。
對(duì)象池
Glide原理的核心是為bitmap維護(hù)一個(gè)對(duì)象池。對(duì)象池的主要目的是通過減少大對(duì)象的分配以重用來提高性能。
Dalvik和ART虛擬機(jī)都沒有使用compacting garbage collector,compacting garbage collector是一種模式,這種模式中GC會(huì)遍歷堆,同時(shí)把活躍對(duì)象移到相鄰內(nèi)存區(qū)域,讓更大的內(nèi)存塊可以用在后續(xù)的分配中。因?yàn)榘沧繘]有這種模式,就可能會(huì)出現(xiàn)被分配的對(duì)象分散在各處,對(duì)象之間只有很小的內(nèi)存可用。如果應(yīng)用試圖分配一個(gè)大于鄰近的閑置內(nèi)存塊空間的對(duì)象,就會(huì)導(dǎo)致OutOfMemoryError,然后崩潰,即使總的空余內(nèi)存空間大于對(duì)象的大小。
使用對(duì)象池還可以幫助提高滾動(dòng)的性能,因?yàn)橹赜胋itmap意味著更少的對(duì)象被創(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使用起來很簡(jiǎn)單,而且不需要任何特別的配置就自動(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)取消等待中的請(qǐng)求。但是如果你使用Application context,你就失去了這種優(yōu)化效果。
注:其實(shí)以上的代碼是一種比較規(guī)范的寫法,我們更熟悉的寫法是:
Glide.with(context) .load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg") .into(ivImg);
簡(jiǎn)單的例子
// 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或者程序中,注冊(cè)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或者程序中,注冊(cè)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的簡(jiǎn)單使用
- 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的對(duì)比
- Android中Glide庫的使用小技巧總結(jié)
相關(guān)文章
Android編程中Activity的四種啟動(dòng)模式
這篇文章主要介紹了Android編程中Activity的四種啟動(dòng)模式,較為詳細(xì)的分析了Activity四種啟動(dòng)模式的原理與功能,需要的朋友可以參考下2016-04-04Flutter封裝組動(dòng)畫混合動(dòng)畫AnimatedGroup示例詳解
這篇文章主要為大家介紹了Flutter封裝組動(dòng)畫混合動(dòng)畫AnimatedGroup示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01深入解讀Android的Volley庫的功能結(jié)構(gòu)
這篇文章主要介紹了Android的Volley開發(fā)框架的功能結(jié)構(gòu),Volley是Android開發(fā)中網(wǎng)絡(luò)部分的一大利器,包含很多HTTP協(xié)議通信的相關(guān)操作,需要的朋友可以參考下2016-05-05android自定義view實(shí)現(xiàn)圓周運(yùn)動(dòng)
這篇文章主要為大家詳細(xì)介紹了android自定義view實(shí)現(xiàn)逆時(shí)針和順時(shí)針轉(zhuǎn)動(dòng)的圓周運(yùn)動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03Android6.0獲取動(dòng)態(tài)權(quán)限代碼示例
這篇文章主要介紹了Android6.0以上獲取動(dòng)態(tài)權(quán)限代碼示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11Android中ExpandableListView使用示例詳解
這篇文章主要為大家詳細(xì)介紹了Android中ExpandableListView使用示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10