public class ImageLoader
extends java.lang.Object
The simple way to use this class is to call get(String, ImageListener)
and
to pass in the default image listener provided by getImageListener(ImageView,
int, int)
. Note that all function calls to this class must be made from the main thread, and all
responses will be delivered to the main thread as well. Custom ResponseDelivery
s which
don't use the main thread are not supported.
Modifier and Type | Class and Description |
---|---|
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.
|
Constructor and Description |
---|
ImageLoader(RequestQueue queue,
ImageLoader.ImageCache imageCache)
Constructs a new ImageLoader.
|
Modifier and Type | Method and Description |
---|---|
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)
Equivalent to calling
#get(String, ImageListener, int, int, ScaleType) with Scaletype == ScaleType.CENTER_INSIDE . |
ImageLoader.ImageContainer |
get(java.lang.String requestUrl,
ImageLoader.ImageListener imageListener,
int maxWidth,
int maxHeight,
android.widget.ImageView.ScaleType scaleType)
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).
|
static ImageLoader.ImageListener |
getImageListener(android.widget.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.
|
boolean |
isCached(java.lang.String requestUrl,
int maxWidth,
int maxHeight,
android.widget.ImageView.ScaleType scaleType)
Checks if the item is available in the cache.
|
protected Request<android.graphics.Bitmap> |
makeImageRequest(java.lang.String requestUrl,
int maxWidth,
int maxHeight,
android.widget.ImageView.ScaleType scaleType,
java.lang.String cacheKey) |
protected void |
onGetImageError(java.lang.String cacheKey,
VolleyError error)
Handler for when an image failed to load.
|
protected void |
onGetImageSuccess(java.lang.String cacheKey,
android.graphics.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.
|
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(android.widget.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 boolean isCached(java.lang.String requestUrl, int maxWidth, int maxHeight, android.widget.ImageView.ScaleType scaleType)
Must be called from the main thread.
requestUrl
- The url of the remote imagemaxWidth
- The maximum width of the returned image.maxHeight
- The maximum height of the returned image.scaleType
- The scaleType of the imageView.public ImageLoader.ImageContainer get(java.lang.String requestUrl, ImageLoader.ImageListener listener)
The ImageContainer will contain either the specified default bitmap or the loaded bitmap.
If the default was returned, the 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)
#get(String, ImageListener, int, int, ScaleType)
with Scaletype == ScaleType.CENTER_INSIDE
.public ImageLoader.ImageContainer get(java.lang.String requestUrl, ImageLoader.ImageListener imageListener, int maxWidth, int maxHeight, android.widget.ImageView.ScaleType scaleType)
Must be called from the main thread.
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.scaleType
- The ImageViews ScaleType used to calculate the needed image size.protected Request<android.graphics.Bitmap> makeImageRequest(java.lang.String requestUrl, int maxWidth, int maxHeight, android.widget.ImageView.ScaleType scaleType, java.lang.String cacheKey)
public void setBatchedResponseDelay(int newBatchedResponseDelayMs)
newBatchedResponseDelayMs
- The time in milliseconds to wait.protected void onGetImageSuccess(java.lang.String cacheKey, android.graphics.Bitmap response)
cacheKey
- The cache key that is associated with the image request.response
- The bitmap that was returned from the network.protected void onGetImageError(java.lang.String cacheKey, VolleyError error)
cacheKey
- The cache key that is associated with the image request.