public class ImageLoader
extends java.lang.Object
get(String, com.android.volley.toolbox.ImageLoader.ImageListener)
and to pass in the default image listener provided by
ImageLoader#getImageListener(android.widget.ImageView, int, int)
. Note that all function calls to
this class must be made from the main thead, and all responses will be delivered to the main
thread as well.Modifier and Type | Class and Description |
---|---|
private class |
ImageLoader.BatchedImageRequest
Wrapper class used to map a Request to the set of active ImageContainer objects that are
interested in its results.
|
static interface |
ImageLoader.ImageCache
Simple cache adapter interface.
|
class |
ImageLoader.ImageContainer
Container object for all of the data surrounding an image request.
|
static interface |
ImageLoader.ImageListener
Interface for the response handlers on image requests.
|
Modifier and Type | Field and Description |
---|---|
private java.util.HashMap<java.lang.String,ImageLoader.BatchedImageRequest> |
mBatchedResponses
HashMap of the currently pending responses (waiting to be delivered).
|
private int |
mBatchResponseDelayMs
Amount of time to wait after first response arrives before delivering all responses.
|
private ImageLoader.ImageCache |
mCache
The cache implementation to be used as an L1 cache before calling into volley.
|
private Handler |
mHandler
Handler to the main thread.
|
private java.util.HashMap<java.lang.String,ImageLoader.BatchedImageRequest> |
mInFlightRequests
HashMap of Cache keys -> BatchedImageRequest used to track in-flight requests so
that we can coalesce multiple requests to the same URL into a single network request.
|
private RequestQueue |
mRequestQueue
RequestQueue for dispatching ImageRequests onto.
|
private java.lang.Runnable |
mRunnable
Runnable for in-flight response delivery.
|
Constructor and Description |
---|
ImageLoader(RequestQueue queue,
ImageLoader.ImageCache imageCache)
Constructs a new ImageLoader.
|
Modifier and Type | Method and Description |
---|---|
private void |
batchResponse(java.lang.String cacheKey,
ImageLoader.BatchedImageRequest request)
Starts the runnable for batched delivery of responses if it is not already started.
|
ImageLoader.ImageContainer |
get(java.lang.String requestUrl,
ImageLoader.ImageListener listener)
Returns an ImageContainer for the requested URL.
|
ImageLoader.ImageContainer |
get(java.lang.String requestUrl,
ImageLoader.ImageListener imageListener,
int maxWidth,
int maxHeight)
Issues a bitmap request with the given URL if that image is not available
in the cache, and returns a bitmap container that contains all of the data
relating to the request (as well as the default image if the requested
image is not available).
|
private static java.lang.String |
getCacheKey(java.lang.String url,
int maxWidth,
int maxHeight)
Creates a cache key for use with the L1 cache.
|
static ImageLoader.ImageListener |
getImageListener(ImageView view,
int defaultImageResId,
int errorImageResId)
The default implementation of ImageListener which handles basic functionality
of showing a default image until the network response is received, at which point
it will switch to either the actual image or the error image.
|
boolean |
isCached(java.lang.String requestUrl,
int maxWidth,
int maxHeight)
Checks if the item is available in the cache.
|
private void |
onGetImageError(java.lang.String cacheKey,
VolleyError error)
Handler for when an image failed to load.
|
private void |
onGetImageSuccess(java.lang.String cacheKey,
Bitmap response)
Handler for when an image was successfully loaded.
|
void |
setBatchedResponseDelay(int newBatchedResponseDelayMs)
Sets the amount of time to wait after the first response arrives before delivering all
responses.
|
private void |
throwIfNotOnMainThread() |
private final RequestQueue mRequestQueue
private int mBatchResponseDelayMs
private final ImageLoader.ImageCache mCache
private final java.util.HashMap<java.lang.String,ImageLoader.BatchedImageRequest> mInFlightRequests
private final java.util.HashMap<java.lang.String,ImageLoader.BatchedImageRequest> mBatchedResponses
private final Handler mHandler
private java.lang.Runnable mRunnable
public ImageLoader(RequestQueue queue, ImageLoader.ImageCache imageCache)
queue
- The RequestQueue to use for making image requests.imageCache
- The cache to use as an L1 cache.public static ImageLoader.ImageListener getImageListener(ImageView view, int defaultImageResId, int errorImageResId)
view
- The imageView that the listener is associated with.defaultImageResId
- Default image resource ID to use, or 0 if it doesn't exist.errorImageResId
- Error image resource ID to use, or 0 if it doesn't exist.public boolean isCached(java.lang.String requestUrl, int maxWidth, int maxHeight)
requestUrl
- The url of the remote imagemaxWidth
- The maximum width of the returned image.maxHeight
- The maximum height of the returned image.public ImageLoader.ImageContainer get(java.lang.String requestUrl, ImageLoader.ImageListener listener)
ImageLoader
will be invoked when the
request is fulfilled.requestUrl
- The URL of the image to be loaded.public ImageLoader.ImageContainer get(java.lang.String requestUrl, ImageLoader.ImageListener imageListener, int maxWidth, int maxHeight)
requestUrl
- The url of the remote imageimageListener
- The listener to call when the remote image is loadedmaxWidth
- The maximum width of the returned image.maxHeight
- The maximum height of the returned image.public void setBatchedResponseDelay(int newBatchedResponseDelayMs)
newBatchedResponseDelayMs
- The time in milliseconds to wait.private void onGetImageSuccess(java.lang.String cacheKey, Bitmap response)
cacheKey
- The cache key that is associated with the image request.response
- The bitmap that was returned from the network.private void onGetImageError(java.lang.String cacheKey, VolleyError error)
cacheKey
- The cache key that is associated with the image request.private void batchResponse(java.lang.String cacheKey, ImageLoader.BatchedImageRequest request)
cacheKey
- The cacheKey of the response being delivered.request
- The BatchedImageRequest to be delivered.private void throwIfNotOnMainThread()
private static java.lang.String getCacheKey(java.lang.String url, int maxWidth, int maxHeight)
url
- The URL of the request.maxWidth
- The max-width of the output.maxHeight
- The max-height of the output.