編輯: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("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是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 Studio ndk-Jni開發詳解
Java Native Interface (JNI)標准是java平台的一部分,它允許Java代碼和其他語言寫的代碼進行交互。JNI 是本地編程接口,它使得在
給初學者的 6 個 Android 加密工具
越來越多的黑客盯上了移動應用,每天都會增加,因為移動應用中有黑客感興趣的東西,如用戶數據。硬編碼(Hard-coded,注,固定寫死,不能修改的)安全秘鑰,SD
關於Android開發的40條優化建議
以下是開始Android編程的好方法: 1、找一些與你想開發的功能類似的代碼; 2、調整它,嘗試讓它變成你想要的; 3、回顧開發中遇到的問題 4、使用StackO
Android ViewPager 指示器控件的最佳實現
為什麼我說它是最實用的 ViewPager 指示器控件呢?它有以下幾個特點: 1、通過自定義 View 來實現,代碼簡單易懂 2、使用起來非常方便 3、通用性高