最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

Glide 是一個android平臺上的快速和高效的開源的多媒體資源管理庫, 提供 多媒體文件的壓縮,內(nèi)存和磁盤緩存, 資源池的接口。
Glide 支持獲取,解壓展示視頻, 圖像和GIFs,  Glide有一個可彈性的api可以讓開發(fā)者自定義網(wǎng)絡(luò)棧技術(shù), 默認使用HttpUrlConnection , 你可以替換為  Google's Volley或者 OkHttp

Glide 開始的目的是提供一個快速和平滑展示圖片列表, 但是Glide對你需要拉取, resize 和展示遠程的圖片這些場景都是很管用的.

Glide最簡單的使用案例就是從遠程服務器或者本地文件系統(tǒng)加載圖片,把它們放在磁盤與內(nèi)存緩存中,然后加載到view上。它可以用在全市圖片的app中,Glide為包含圖片的滾動列表做了盡可能流暢的優(yōu)化。

對象池
Glide原理的核心是為bitmap維護一個對象池。對象池的主要目的是通過減少大對象的分配以重用來提高性能。

Dalvik和ART虛擬機都沒有使用compacting garbage collector,compacting garbage collector是一種模式,這種模式中GC會遍歷堆,同時把活躍對象移到相鄰內(nèi)存區(qū)域,讓更大的內(nèi)存塊可以用在后續(xù)的分配中。因為安卓沒有這種模式,就可能會出現(xiàn)被分配的對象分散在各處,對象之間只有很小的內(nèi)存可用。如果應用試圖分配一個大于鄰近的閑置內(nèi)存塊空間的對象,就會導致OutOfMemoryError,然后崩潰,即使總的空余內(nèi)存空間大于對象的大小。

使用對象池還可以幫助提高滾動的性能,因為重用bitmap意味著更少的對象被創(chuàng)建與回收。垃圾回收會導致“停止一切(Stop The World)”事件,這個事件指的是回收器執(zhí)行期間,所有線程(包括UI線程)都會暫停。這個時候,圖像幀無法被渲染同時UI可能會停滯,這在滾動期間尤其明顯。

2016427141956445.gif (480×360)

下載
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加載圖片的優(yōu)化程度,Glide可以監(jiān)視activity的生命周期,在activity銷毀的時候自動取消等待中的請求。但是如果你使用Application context,你就失去了這種優(yōu)化效果。

注:其實以上的代碼是一種比較規(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));
 ...
}

相關(guān)文章

最新評論

石河子市| 宣武区| 阿克苏市| 株洲市| 万载县| 望奎县| 怀化市| 晋中市| 吉安县| 天祝| 长春市| 浮梁县| 增城市| 简阳市| 江华| 嵊州市| 海口市| 咸丰县| 新邵县| 曲阜市| 丽江市| 宁明县| 淄博市| 文登市| 新河县| 勐海县| 永城市| 手游| 泽普县| 涪陵区| 唐河县| 黑龙江省| 白沙| 新安县| 临漳县| 东莞市| 饶河县| 南部县| 莱西市| 衡水市| 确山县|