亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android的多媒體管理庫Glide的基本使用示例

 更新時(shí)間:2016年04月27日 14:19:52   作者:sunbiaobiao  
這篇文章主要介紹了Android的多媒體管理庫Glide的基本使用示例,Glide在圖片App中的表現(xiàn)非常好,Google旗下的Yelp也在使用,需要的朋友可以參考下

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)期間尤其明顯。

2016427141956445.gif (480×360)

下載
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));
 ...
}

相關(guān)文章

  • Android編程中Activity的四種啟動(dòng)模式

    Android編程中Activity的四種啟動(dòng)模式

    這篇文章主要介紹了Android編程中Activity的四種啟動(dòng)模式,較為詳細(xì)的分析了Activity四種啟動(dòng)模式的原理與功能,需要的朋友可以參考下
    2016-04-04
  • RN在Android打包發(fā)布App(詳解)

    RN在Android打包發(fā)布App(詳解)

    下面小編就為大家?guī)硪黄猂N在Android打包發(fā)布App(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • Flutter封裝組動(dòng)畫混合動(dòng)畫AnimatedGroup示例詳解

    Flutter封裝組動(dòng)畫混合動(dòng)畫AnimatedGroup示例詳解

    這篇文章主要為大家介紹了Flutter封裝組動(dòng)畫混合動(dòng)畫AnimatedGroup示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Android 文件操作方法

    Android 文件操作方法

    本篇文章小編為大家介紹,Android 文件操作方法。需要的朋友參考下
    2013-04-04
  • 深入解讀Android的Volley庫的功能結(jié)構(gòu)

    深入解讀Android的Volley庫的功能結(jié)構(gòu)

    這篇文章主要介紹了Android的Volley開發(fā)框架的功能結(jié)構(gòu),Volley是Android開發(fā)中網(wǎng)絡(luò)部分的一大利器,包含很多HTTP協(xié)議通信的相關(guān)操作,需要的朋友可以參考下
    2016-05-05
  • android自定義倒計(jì)時(shí)控件示例

    android自定義倒計(jì)時(shí)控件示例

    這篇文章主要介紹了Android秒殺倒計(jì)時(shí)自定義TextView示例,大家參考使用吧
    2014-01-01
  • 淺談Volley加載不出圖片的問題

    淺談Volley加載不出圖片的問題

    下面小編就為大家?guī)硪黄獪\談Volley加載不出圖片的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • android自定義view實(shí)現(xiàn)圓周運(yùn)動(dòng)

    android自定義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-03
  • Android6.0獲取動(dòng)態(tài)權(quán)限代碼示例

    Android6.0獲取動(dòng)態(tài)權(quán)限代碼示例

    這篇文章主要介紹了Android6.0以上獲取動(dòng)態(tài)權(quán)限代碼示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Android中ExpandableListView使用示例詳解

    Android中ExpandableListView使用示例詳解

    這篇文章主要為大家詳細(xì)介紹了Android中ExpandableListView使用示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10

最新評(píng)論