編輯:Android 資訊
本文由碼農網 – 小峰原創,轉載請看清文末的轉載要求,歡迎參與我們的付費投稿計劃!
Glide 是一款基於 Android 的圖片載入與圖片快取元件,可以最大性能地在 Android 裝置上讀取、解碼、顯示圖片和影片。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; }
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 了。
除了 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 元件可以幫助你在圖片影片方面優化應用程式。
Android 注解入門:從 Override 到編譯期處理
注解是 Android 開發中常見的工具,最典型的例子就是 @Override。它會在編譯期檢查方法是否正確重寫父類,也能提升程式碼可讀性。注解的價值除了標記用途,注解還
Android UI 元件系列:LinearLayout 線性版面配置
LinearLayout 是一個以線性方向排列子 View 的 ViewGroup,方向可以是水平,也可以是垂直。 你可以重複巢狀使用多層 LinearLayout;不過
Android View 佈局定位流程解析
在 Android 的 View 繪製流程中,測量完成之後,下一步就是佈局定位。這個階段決定每個 View 在父容器中的位置與大小。本文將跟蹤 performLayout
Messenger 完全解析:不寫 aidl 也能做進程間通信
一、概述 談到 Android 進程間通信,多數人第一反應是寫 aidl 文件,再靠 aapt 生成的類把服務端與客戶端代碼補完。如果你對這個流程不熟,可以看看 Andr