Skip to content

Commit

Permalink
Issue #51 - Added ImageLoader.loadImage(...).
Browse files Browse the repository at this point in the history
Added FakeBitmapDisplayer.
  • Loading branch information
nostra13 committed Sep 29, 2012
1 parent 710da6b commit 786f45a
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.os.Handler;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.nostra13.universalimageloader.cache.disc.DiscCacheAware;
import com.nostra13.universalimageloader.cache.memory.MemoryCacheAware;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.assist.MemoryCacheKeyUtil;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.BitmapDisplayer;
import com.nostra13.universalimageloader.core.display.FakeBitmapDisplayer;

/**
* Singletone for image loading and displaying at {@link ImageView ImageViews}<br />
Expand All @@ -41,6 +45,7 @@ public class ImageLoader {
private ExecutorService imageLoadingExecutor;
private ExecutorService cachedImageLoadingExecutor;
private ImageLoadingListener emptyListener;
private BitmapDisplayer fakeBitmapDisplayer;

private Map<ImageView, String> cacheKeyForImageView = Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
private Map<String, ReentrantLock> uriLocks = Collections.synchronizedMap(new WeakHashMap<String, ReentrantLock>());
Expand Down Expand Up @@ -78,6 +83,7 @@ public synchronized void init(ImageLoaderConfiguration configuration) {
if (this.configuration == null) {
this.configuration = configuration;
emptyListener = new SimpleImageLoadingListener();
fakeBitmapDisplayer = new FakeBitmapDisplayer();
}
}

Expand Down Expand Up @@ -129,9 +135,7 @@ public void displayImage(String uri, ImageView imageView, DisplayImageOptions op
* @param imageView
* {@link ImageView} which should display image
* @param listener
* {@linkplain ImageLoadingListener Listener} for image loading process. Listener fires events only if
* there is no image for loading in memory cache. If there is image for loading in memory cache then
* image is displayed at ImageView but listener does not fire any event. Listener fires events on UI
* {@linkplain ImageLoadingListener Listener} for image loading process. Listener fires events on UI
* thread.
* @throws RuntimeException
* if {@link #init(ImageLoaderConfiguration)} method wasn't called before
Expand All @@ -154,9 +158,7 @@ public void displayImage(String uri, ImageView imageView, ImageLoadingListener l
* {@linkplain ImageLoaderConfiguration.Builder#defaultDisplayImageOptions(DisplayImageOptions) from
* configuration} will be used.
* @param listener
* {@linkplain ImageLoadingListener Listener} for image loading process. Listener fires events only if
* there is no image for loading in memory cache. If there is image for loading in memory cache then
* image is displayed at ImageView but listener does not fire any event. Listener fires events on UI
* {@linkplain ImageLoadingListener Listener} for image loading process. Listener fires events on UI
* thread.
* @throws RuntimeException
* if {@link #init(ImageLoaderConfiguration)} method wasn't called before
Expand Down Expand Up @@ -222,6 +224,122 @@ public void displayImage(String uri, ImageView imageView, DisplayImageOptions op
}
}

/**
* Adds load image task to execution pool. Image will be returned with
* {@link ImageLoadingListener#onLoadingComplete(Bitmap) callback}.<br />
* <b>NOTE:</b> {@link #init(ImageLoaderConfiguration)} method must be called before this method call
*
* @param context
* Application context (used for creation of fake {@link ImageView})
* @param uri
* Image URI (i.e. "http://site.com/image.png", "file:///mnt/sdcard/image.png")
* @param listener
* {@linkplain ImageLoadingListener Listener} for image loading process. Listener fires events on UI
* thread.
*/
public void loadImage(Context context, String uri, ImageLoadingListener listener) {
loadImage(context, uri, null, null, listener);
}

/**
* Adds load image task to execution pool. Image will be returned with
* {@link ImageLoadingListener#onLoadingComplete(Bitmap) callback}.<br />
* <b>NOTE:</b> {@link #init(ImageLoaderConfiguration)} method must be called before this method call
*
* @param context
* Application context (used for creation of fake {@link ImageView})
* @param uri
* Image URI (i.e. "http://site.com/image.png", "file:///mnt/sdcard/image.png")
* @param minImageSize
* Minimal size for {@link Bitmap} which will be returned in
* {@linkplain ImageLoadingListener#onLoadingComplete(Bitmap) callback}. Downloaded image will be decoded
* and scaled to {@link Bitmap} of the size which is <b>equal or larger</b> (usually a bit larger) than
* incoming minImageSize .
* @param listener
* {@linkplain ImageLoadingListener Listener} for image loading process. Listener fires events on UI
* thread.
*/
public void loadImage(Context context, String uri, ImageSize minImageSize, ImageLoadingListener listener) {
loadImage(context, uri, minImageSize, null, listener);
}

/**
* Adds load image task to execution pool. Image will be returned with
* {@link ImageLoadingListener#onLoadingComplete(Bitmap) callback}.<br />
* <b>NOTE:</b> {@link #init(ImageLoaderConfiguration)} method must be called before this method call
*
* @param context
* Application context (used for creation of fake {@link ImageView})
* @param uri
* Image URI (i.e. "http://site.com/image.png", "file:///mnt/sdcard/image.png")
* @param options
* {@linkplain DisplayImageOptions Display image options} for image displaying. If <b>null</b> - default
* display image options
* {@linkplain ImageLoaderConfiguration.Builder#defaultDisplayImageOptions(DisplayImageOptions) from
* configuration} will be used.<br />
* Incoming options should contain {@link FakeBitmapDisplayer} as displayer.
* @param listener
* {@linkplain ImageLoadingListener Listener} for image loading process. Listener fires events on UI
* thread.
*/
public void loadImage(Context context, String uri, DisplayImageOptions options, ImageLoadingListener listener) {
loadImage(context, uri, null, options, listener);
}

/**
* Adds load image task to execution pool. Image will be returned with
* {@link ImageLoadingListener#onLoadingComplete(Bitmap) callback}.<br />
* <b>NOTE:</b> {@link #init(ImageLoaderConfiguration)} method must be called before this method call
*
* @param context
* Application context (used for creation of fake {@link ImageView})
* @param uri
* Image URI (i.e. "http://site.com/image.png", "file:///mnt/sdcard/image.png")
* @param minImageSize
* Minimal size for {@link Bitmap} which will be returned in
* {@linkplain ImageLoadingListener#onLoadingComplete(Bitmap) callback}. Downloaded image will be decoded
* and scaled to {@link Bitmap} of the size which is <b>equal or larger</b> (usually a bit larger) than
* incoming minImageSize .
* @param options
* {@linkplain DisplayImageOptions Display image options} for image displaying. If <b>null</b> - default
* display image options
* {@linkplain ImageLoaderConfiguration.Builder#defaultDisplayImageOptions(DisplayImageOptions) from
* configuration} will be used.<br />
* Incoming options should contain {@link FakeBitmapDisplayer} as displayer.
* @param listener
* {@linkplain ImageLoadingListener Listener} for image loading process. Listener fires events on UI
* thread.
*/
public void loadImage(Context context, String uri, ImageSize minImageSize, DisplayImageOptions options, ImageLoadingListener listener) {
if (minImageSize == null) {
minImageSize = new ImageSize(configuration.maxImageWidthForMemoryCache, configuration.maxImageHeightForMemoryCache);
}
if (options == null) {
options = configuration.defaultDisplayImageOptions;
}

DisplayImageOptions optionsWithFakeDisplayer;
if (options.getDisplayer() instanceof FakeBitmapDisplayer) {
optionsWithFakeDisplayer = options;
} else {
DisplayImageOptions.Builder newOptionsBuilder = new DisplayImageOptions.Builder();
if (options.isCacheInMemory()) newOptionsBuilder.cacheInMemory();
if (options.isCacheOnDisc()) newOptionsBuilder.cacheOnDisc();
if (options.isResetViewBeforeLoading()) newOptionsBuilder.resetViewBeforeLoading();
if (options.isShowImageForEmptyUri()) newOptionsBuilder.showImageForEmptyUri(options.getImageForEmptyUri());
if (options.isShowStubImage()) newOptionsBuilder.showStubImage(options.getStubImage());
newOptionsBuilder.imageScaleType(options.getImageScaleType());
newOptionsBuilder.displayer(fakeBitmapDisplayer);
optionsWithFakeDisplayer = newOptionsBuilder.build();
}

ImageView fakeImage = new ImageView(context);
fakeImage.setLayoutParams(new LayoutParams(minImageSize.getWidth(), minImageSize.getHeight()));
fakeImage.setScaleType(ScaleType.CENTER_CROP);

displayImage(uri, fakeImage, optionsWithFakeDisplayer, listener);
}

private void checkExecutors() {
if (imageLoadingExecutor == null || imageLoadingExecutor.isShutdown()) {
imageLoadingExecutor = Executors.newFixedThreadPool(configuration.threadPoolSize, configuration.displayImageThreadFactory);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.nostra13.universalimageloader.core.display;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.graphics.Bitmap;
import android.widget.ImageView;

/**
* Fake displayer which doesn't display Bitmap in ImageView. Should be used for in {@linkplain DisplayImageOptions
* display options} for
* {@link ImageLoader#loadImage(String, int, int, com.nostra13.universalimageloader.core.DisplayImageOptions, com.nostra13.universalimageloader.core.assist.ImageLoadingListener)
* ImageLoader.loadImage()}
*
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
*/
public final class FakeBitmapDisplayer implements BitmapDisplayer {
@Override
public Bitmap display(Bitmap bitmap, ImageView imageView) {
// Do nothing
return bitmap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
*/
public class SimpleBitmapDisplayer implements BitmapDisplayer {
public final class SimpleBitmapDisplayer implements BitmapDisplayer {
@Override
public Bitmap display(Bitmap bitmap, ImageView imageView) {
imageView.setImageBitmap(bitmap);
Expand Down

0 comments on commit 786f45a

Please sign in to comment.