com.github.droidfu.imageloader
Class ImageLoader

java.lang.Object
  extended by com.github.droidfu.imageloader.ImageLoader
All Implemented Interfaces:
Runnable

public class ImageLoader
extends Object
implements Runnable

Realizes an background image loader backed by a two-level FIFO cache. If the image to be loaded is present in the cache, it is set immediately on the given view. Otherwise, a thread from a thread pool will be used to download the image in the background and set the image on the view as soon as it completes.

Author:
Matthias Kaeppler

Field Summary
static String BITMAP_EXTRA
           
static int HANDLER_MESSAGE_ID
           
static String IMAGE_URL_EXTRA
           
 
Method Summary
static void clearCache()
          Clears the 1st-level cache (in-memory cache).
protected  android.graphics.Bitmap downloadImage()
           
static ImageCache getImageCache()
          Returns the image cache backing this image loader.
static void initialize(android.content.Context context)
          This method must be called before any other method is invoked on this class.
static void initialize(android.content.Context context, long expirationInMinutes)
           
 void notifyImageLoaded(String url, android.graphics.Bitmap bitmap)
           
protected  byte[] retrieveImageData()
           
 void run()
          The job method run on a worker thread.
static void setMaxDownloadAttempts(int numAttempts)
           
static void setThreadPoolSize(int numThreads)
           
static void start(String imageUrl, ImageLoaderHandler handler)
          Triggers the image loader for the given image and handler.
static void start(String imageUrl, ImageLoaderHandler handler, android.graphics.drawable.Drawable dummyDrawable, android.graphics.drawable.Drawable errorDrawable)
          Triggers the image loader for the given image and handler.
static void start(String imageUrl, android.widget.ImageView imageView)
          Triggers the image loader for the given image and view.
static void start(String imageUrl, android.widget.ImageView imageView, android.graphics.drawable.Drawable dummyDrawable, android.graphics.drawable.Drawable errorDrawable)
          Triggers the image loader for the given image and view and sets a dummy image while waiting for the download to finish.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HANDLER_MESSAGE_ID

public static final int HANDLER_MESSAGE_ID
See Also:
Constant Field Values

BITMAP_EXTRA

public static final String BITMAP_EXTRA
See Also:
Constant Field Values

IMAGE_URL_EXTRA

public static final String IMAGE_URL_EXTRA
See Also:
Constant Field Values
Method Detail

setThreadPoolSize

public static void setThreadPoolSize(int numThreads)
Parameters:
numThreads - the maximum number of threads that will be started to download images in parallel

setMaxDownloadAttempts

public static void setMaxDownloadAttempts(int numAttempts)
Parameters:
numAttempts - how often the image loader should retry the image download if network connection fails

initialize

public static void initialize(android.content.Context context)
This method must be called before any other method is invoked on this class. Please note that when using ImageLoader as part of WebImageView or WebGalleryAdapter, then there is no need to call this method, since those classes will already do that for you. This method is idempotent. You may call it multiple times without any side effects.

Parameters:
context - the current context

initialize

public static void initialize(android.content.Context context,
                              long expirationInMinutes)

start

public static void start(String imageUrl,
                         android.widget.ImageView imageView)
Triggers the image loader for the given image and view. The image loading will be performed concurrently to the UI main thread, using a fixed size thread pool. The loaded image will be posted back to the given ImageView upon completion.

Parameters:
imageUrl - the URL of the image to download
imageView - the ImageView which should be updated with the new image

start

public static void start(String imageUrl,
                         android.widget.ImageView imageView,
                         android.graphics.drawable.Drawable dummyDrawable,
                         android.graphics.drawable.Drawable errorDrawable)
Triggers the image loader for the given image and view and sets a dummy image while waiting for the download to finish. The image loading will be performed concurrently to the UI main thread, using a fixed size thread pool. The loaded image will be posted back to the given ImageView upon completion.

Parameters:
imageUrl - the URL of the image to download
imageView - the ImageView which should be updated with the new image
dummyDrawable - the Drawable set to the ImageView while waiting for the image to be downloaded
errorDrawable - the Drawable set to the ImageView if a download error occurs

start

public static void start(String imageUrl,
                         ImageLoaderHandler handler)
Triggers the image loader for the given image and handler. The image loading will be performed concurrently to the UI main thread, using a fixed size thread pool. The loaded image will not be automatically posted to an ImageView; instead, you can pass a custom ImageLoaderHandler and handle the loaded image yourself (e.g. cache it for later use).

Parameters:
imageUrl - the URL of the image to download
handler - the handler which is used to handle the downloaded image

start

public static void start(String imageUrl,
                         ImageLoaderHandler handler,
                         android.graphics.drawable.Drawable dummyDrawable,
                         android.graphics.drawable.Drawable errorDrawable)
Triggers the image loader for the given image and handler. The image loading will be performed concurrently to the UI main thread, using a fixed size thread pool. The loaded image will not be automatically posted to an ImageView; instead, you can pass a custom ImageLoaderHandler and handle the loaded image yourself (e.g. cache it for later use).

Parameters:
imageUrl - the URL of the image to download
handler - the handler which is used to handle the downloaded image
dummyDrawable - the Drawable set to the ImageView while waiting for the image to be downloaded
errorDrawable - the Drawable set to the ImageView if a download error occurs

clearCache

public static void clearCache()
Clears the 1st-level cache (in-memory cache). A good candidate for calling in Application.onLowMemory().


getImageCache

public static ImageCache getImageCache()
Returns the image cache backing this image loader.

Returns:
the ImageCache

run

public void run()
The job method run on a worker thread. It will first query the image cache, and on a miss, download the image from the Web.

Specified by:
run in interface Runnable

downloadImage

protected android.graphics.Bitmap downloadImage()

retrieveImageData

protected byte[] retrieveImageData()
                            throws IOException
Throws:
IOException

notifyImageLoaded

public void notifyImageLoaded(String url,
                              android.graphics.Bitmap bitmap)


Copyright © 2011. All Rights Reserved.