Created
April 19, 2016 05:41
-
-
Save TheLittleNaruto/318fad6ef31b5c4769a916389e086a32 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.Volley; | |
/** | |
* Created by TheLittleNaruto on 08-08-2014 12:55. | |
*/ | |
public class MyVolleySingleton { | |
private static MyVolleySingleton mInstance; | |
private RequestQueue mRequestQueue; | |
private ImageLoader mImageLoader; | |
private static Context mCtx; | |
private MyVolleySingleton(Context context) { | |
mCtx = context; | |
mRequestQueue = getRequestQueue(); | |
mImageLoader = getImageLoaderRequest(); | |
} | |
private ImageLoader getImageLoaderRequest() { | |
return new ImageLoader(mRequestQueue, | |
new ImageLoader.ImageCache() { | |
private final LruCache<String, Bitmap> | |
cache = new LruCache<String, Bitmap>(20); | |
@Override | |
public Bitmap getBitmap(String url) { | |
return cache.get(url); | |
} | |
@Override | |
public void putBitmap(String url, Bitmap bitmap) { | |
cache.put(url, bitmap); | |
} | |
} | |
); | |
} | |
public static synchronized MyVolleySingleton getInstance(Context context) { | |
if (mInstance == null) { | |
mInstance = new MyVolleySingleton(context); | |
} | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment