簡介
Volley是在2013年Google I/O大會(huì)上推出了一個(gè)新的網(wǎng)絡(luò)通信框架匣距,內(nèi)部封裝了HttpURLConnection和HttpClient, 解決了網(wǎng)絡(luò)數(shù)據(jù)解析和線程切換的問題。今天不詳解Volley的具體原理,只是簡單地講下怎么利用Volley獲取網(wǎng)絡(luò)圖片和加載它.
步驟如下:
1.添加volley依賴
compile 'eu.the4thfloor.volley:com.android.volley:2015.05.28'
2.采用Volley框架自定義的掛載圖片控件NetWorkImageView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.jasonwang.loadernetpicture.MainActivity">
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/niv_pic"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
3.請求網(wǎng)絡(luò)圖片的主要方法
package com.example.jasonwang.loadernetpicture;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.LruCache;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
public class MainActivity extends AppCompatActivity {
private NetworkImageView nivImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nivImageView = (NetworkImageView) findViewById(R.id.niv_pic);
getPicFromNetWork(); //獲取網(wǎng)絡(luò)圖片
}
int maxSize = 5 * 1024 * 1024; //定義緩存的大小:5M
public void getPicFromNetWork() {
final String url = "http://imgsrc.baidu.com/baike/pic/item/b3fb43166d224f4aa657f6540af790529922d1af.jpg";
//創(chuàng)建請求隊(duì)列對象
RequestQueue requestQueue = Volley.newRequestQueue(this);
///創(chuàng)建圖片緩存區(qū)對象
ImageLoader.ImageCache imageCache = new MyLruCache(maxSize);
//創(chuàng)建圖片加載器對象
final ImageLoader imageLoader =new ImageLoader(requestQueue,imageCache);
//將網(wǎng)絡(luò)圖片加載都控件里
nivImageView.setImageUrl(url,imageLoader);
}
/**
* 定義自己的圖片緩存區(qū),這里的緩存只是一級緩存(也就是內(nèi)存緩
*存)
*/
class MyLruCache extends LruCache<String,Bitmap> implements ImageLoader.ImageCache{
/**
* @param maxSize for caches that do not override {@link #sizeOf}, this is
* the maximum number of entries in the cache. For all other caches,
* this is the maximum sum of the sizes of the entries in this cache.
*/
public MyLruCache(int maxSize) { //定義緩存的大小
super(maxSize);
}
/**
*獲取要緩存的圖片的大小,檢查是否會(huì)爆倉
* @param key
* @param value
* @return
*/
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}
/**
* 從緩存中獲取圖片
* @param url
* @return
*/
@Override
public Bitmap getBitmap(String url) {
return get(url);
}
/**
* 將圖片存入緩存中
* @param url
* @param bitmap
*/
@Override
public void putBitmap(String url, Bitmap bitmap) {
put(url, bitmap);
}
}
}
注意:Volley獲取網(wǎng)絡(luò)圖片跟流行的Picasso,Glide開源框架不太一樣,Volley底層是沒有封裝好圖片緩存區(qū),需要自己定義肯定會(huì)有小伙伴問我,明明有其他那么多封裝好的框架不用,還要用這個(gè).因?yàn)槲蚁敫嬖V大家如果用Volley獲取網(wǎng)絡(luò)圖片的話該如何定義圖片緩存
4.添加網(wǎng)絡(luò)權(quán)限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
好了,看下我?guī)洑獾呐枷窈璋?/p>