Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 資訊

Android 圖片平滑滾動元件 Glide

編輯:Android 資訊

本文由碼農網 – 小峰原創,轉載請看清文末的轉載要求,歡迎參與我們的付費投稿計劃!

Glide 是一款基於 Android 的圖片載入與圖片快取元件,可以最大性能地在 Android 裝置上讀取、解碼、顯示圖片和影片。Glide 可以把遠端的圖片、影片、動畫圖片等快取在裝置本地,便於提高使用者瀏覽圖片的流暢體驗。

Glide 最核心的功能就是提高滾動圖片列表的性能,並且它還能滿足對遠端圖片的讀取、改變尺寸以及展示的性能要求。

Glide 使用方法

最簡單的範例程式碼如下:

// For a simple view: @Override public void onCreate(Bundle savedInstanceState) { ... ImageView imageView = (ImageView) findViewById(R.id.my_image_view); Glide.with(this).load("https://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; }

在 Glide 上應用 Volley 通信框架

Volley 是 Glide 的可選項,可以支援 http/https 來讀取圖片。

用 Gradle:

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>

然後在 Activity 或 Application 中註冊 Volley 的加載項即可:

public void onCreate() { Glide.get(this).register(GlideUrl.class, InputStream.class, new VolleyUrlLoader.Factory(yourRequestQueue)); ... }

這樣所有的請求就會透過 Volley 了。

在 Glide 中應用 OKHttp 通信框架

除了 Volley,Glide 中還可以使用 OkHttp 通信框架,OkHttp 同樣支援 http/https 來讀取圖片。

用 Gradle:

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>

然後在 Activity 或 Application 中註冊 OkHttp 的加載項即可:

public void onCreate() { Glide.get(this).register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(yourOkHttpClient)); ... }

總結

如果你的 Android 應用中涉及遠端圖片的處理,那麼 Glide 元件可以幫助你在圖片影片方面優化應用程式。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved