package com.xiaozhi.beautygallery.util;
/*
 * 网络访问工具类
 */
public class VolleyUtil {

}

package com.xiaozhi.beautygallery.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageCache;
import com.android.volley.toolbox.Volley;

/*
 * 网络访问工具类
 */
public class VolleyUtil {
	
	private static VolleyUtil mInstance;
	private RequestQueue mRequestQueue;
	private ImageLoader mImageLoader;
	private static Context mCtx;

	private VolleyUtil(Context context) {
		mCtx = context;
		mRequestQueue = getRequestQueue();

		mImageLoader = new ImageLoader(mRequestQueue,
				new BitmapCache());
	}

	public static void init(Context context) {
		if (mInstance == null) {
			mInstance = new VolleyUtil(context);
		}
	}

	public static VolleyUtil getInstance() {
		if (mInstance == null) {
			throw new NullPointerException(
					"please call HttpUtil.init(),before getInstance()");
		}
		return mInstance;
	}

	public RequestQueue getRequestQueue() {
		if (mRequestQueue == null) {
			// getApplicationContext() is key, it keeps you from leaking the
			// Activity or BroadcastReceiver if someone passes one in.
			mRequestQueue = Volley
					.newRequestQueue(mCtx.getApplicationContext());
		}
		return mRequestQueue;
	}

	public <T> void addToRequestQueue(Request<T> req) {
		getRequestQueue().add(req);
	}

	public ImageLoader getImageLoader() {
		return mImageLoader;
	}
	
	private class BitmapCache implements ImageCache{
		
		private int maxSize = 8 * 1024 * 1024;
		private LruCache<String, Bitmap> cache;
		
		public BitmapCache(){
			cache = new LruCache<String, Bitmap>(maxSize){
				@Override
				protected int sizeOf(String key, Bitmap value) {
					return value.getByteCount();
				}
			};
		}

		@Override
		public Bitmap getBitmap(String url) {
			return cache.get(url);
		}

		@Override
		public void putBitmap(String url, Bitmap bitmap) {
			cache.put(url, bitmap);
		}
	
	}
}

add VolleyUtil