Skip to content

Commit

Permalink
Issue #149, #239 - Added flag to BitmapDisplayer.display(...)
Browse files Browse the repository at this point in the history
about image source
  • Loading branch information
nostra13 committed Jun 4, 2013
1 parent 74baa70 commit d810a95
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.LoadedFrom;
import com.nostra13.universalimageloader.core.display.BitmapDisplayer;
import com.nostra13.universalimageloader.utils.L;

Expand All @@ -32,7 +33,7 @@
*/
final class DisplayBitmapTask implements Runnable {

private static final String LOG_DISPLAY_IMAGE_IN_IMAGEVIEW = "Display image in ImageView [%s]";
private static final String LOG_DISPLAY_IMAGE_IN_IMAGEVIEW = "Display image in ImageView (loaded from %1$s) [%2$s]";
private static final String LOG_TASK_CANCELLED = "ImageView is reused for another image. Task is cancelled. [%s]";

private final Bitmap bitmap;
Expand All @@ -42,26 +43,28 @@ final class DisplayBitmapTask implements Runnable {
private final BitmapDisplayer displayer;
private final ImageLoadingListener listener;
private final ImageLoaderEngine engine;
private final LoadedFrom loadedFrom;

private boolean loggingEnabled;

public DisplayBitmapTask(Bitmap bitmap, ImageLoadingInfo imageLoadingInfo, ImageLoaderEngine engine) {
public DisplayBitmapTask(Bitmap bitmap, ImageLoadingInfo imageLoadingInfo, ImageLoaderEngine engine, LoadedFrom loadedFrom) {
this.bitmap = bitmap;
imageUri = imageLoadingInfo.uri;
imageView = imageLoadingInfo.imageView;
memoryCacheKey = imageLoadingInfo.memoryCacheKey;
displayer = imageLoadingInfo.options.getDisplayer();
listener = imageLoadingInfo.listener;
this.engine = engine;
this.loadedFrom = loadedFrom;
}

public void run() {
if (isViewWasReused()) {
if (loggingEnabled) L.i(LOG_TASK_CANCELLED, memoryCacheKey);
listener.onLoadingCancelled(imageUri, imageView);
} else {
if (loggingEnabled) L.i(LOG_DISPLAY_IMAGE_IN_IMAGEVIEW, memoryCacheKey);
Bitmap displayedBitmap = displayer.display(bitmap, imageView);
if (loggingEnabled) L.i(LOG_DISPLAY_IMAGE_IN_IMAGEVIEW, loadedFrom, memoryCacheKey);
Bitmap displayedBitmap = displayer.display(bitmap, imageView, loadedFrom);
listener.onLoadingComplete(imageUri, imageView, displayedBitmap);
engine.cancelDisplayTaskFor(imageView);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.nostra13.universalimageloader.core.assist.FlushedInputStream;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.assist.LoadedFrom;
import com.nostra13.universalimageloader.core.assist.MemoryCacheUtil;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.BitmapDisplayer;
Expand Down Expand Up @@ -216,7 +217,7 @@ public void displayImage(String uri, ImageView imageView, DisplayImageOptions op
ProcessAndDisplayImageTask displayTask = new ProcessAndDisplayImageTask(engine, bmp, imageLoadingInfo, options.getHandler());
engine.submit(displayTask);
} else {
options.getDisplayer().display(bmp, imageView);
options.getDisplayer().display(bmp, imageView, LoadedFrom.MEMORY_CACHE);
listener.onLoadingComplete(uri, imageView, bmp);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.assist.LoadedFrom;
import com.nostra13.universalimageloader.core.assist.ViewScaleType;
import com.nostra13.universalimageloader.core.decode.ImageDecoder;
import com.nostra13.universalimageloader.core.decode.ImageDecodingInfo;
Expand Down Expand Up @@ -91,6 +92,8 @@ final class LoadAndDisplayImageTask implements Runnable {
final DisplayImageOptions options;
final ImageLoadingListener listener;

private LoadedFrom loadedFrom = LoadedFrom.NETWORK;

public LoadAndDisplayImageTask(ImageLoaderEngine engine, ImageLoadingInfo imageLoadingInfo, Handler handler) {
this.engine = engine;
this.imageLoadingInfo = imageLoadingInfo;
Expand Down Expand Up @@ -146,6 +149,7 @@ public void run() {
configuration.memoryCache.put(memoryCacheKey, bmp);
}
} else {
loadedFrom = LoadedFrom.MEMORY_CACHE;
log(LOG_GET_IMAGE_FROM_MEMORY_CACHE_AFTER_WAITING);
}

Expand All @@ -162,7 +166,7 @@ public void run() {

if (checkTaskIsNotActual() || checkTaskIsInterrupted()) return;

DisplayBitmapTask displayBitmapTask = new DisplayBitmapTask(bmp, imageLoadingInfo, engine);
DisplayBitmapTask displayBitmapTask = new DisplayBitmapTask(bmp, imageLoadingInfo, engine, loadedFrom);
displayBitmapTask.setLoggingEnabled(loggingEnabled);
handler.post(displayBitmapTask);
}
Expand Down Expand Up @@ -240,11 +244,13 @@ private Bitmap tryLoadBitmap() {
if (imageFile.exists()) {
log(LOG_LOAD_IMAGE_FROM_DISC_CACHE);

loadedFrom = LoadedFrom.DISC_CACHE;
bitmap = decodeImage(Scheme.FILE.wrap(imageFile.getAbsolutePath()));
}
if (bitmap == null || bitmap.getWidth() <= 0 || bitmap.getHeight() <= 0) {
log(LOG_LOAD_IMAGE_FROM_NETWORK);

loadedFrom = LoadedFrom.NETWORK;
String imageUriForDecoding = options.isCacheOnDisc() ? tryCacheImageOnDisc(imageFile) : uri;
if (!checkTaskIsNotActual()) {
bitmap = decodeImage(imageUriForDecoding);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.os.Handler;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.assist.LoadedFrom;
import com.nostra13.universalimageloader.core.process.BitmapProcessor;
import com.nostra13.universalimageloader.utils.L;

Expand Down Expand Up @@ -50,6 +51,6 @@ public void run() {
if (engine.configuration.loggingEnabled) L.i(LOG_POSTPROCESS_IMAGE, imageLoadingInfo.memoryCacheKey);
BitmapProcessor processor = imageLoadingInfo.options.getPostProcessor();
final Bitmap processedBitmap = processor.process(bitmap);
handler.post(new DisplayBitmapTask(processedBitmap, imageLoadingInfo, engine));
handler.post(new DisplayBitmapTask(processedBitmap, imageLoadingInfo, engine, LoadedFrom.MEMORY_CACHE));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.nostra13.universalimageloader.core.assist;

/**
* Source image loaded from.
*
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
*/
public enum LoadedFrom {
NETWORK, DISC_CACHE, MEMORY_CACHE
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*******************************************************************************/
package com.nostra13.universalimageloader.core.display;

import com.nostra13.universalimageloader.core.assist.LoadedFrom;

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

Expand All @@ -33,7 +35,8 @@ public interface BitmapDisplayer {
*
* @param bitmap Source bitmap
* @param imageView {@linkplain ImageView Image view} to display Bitmap
* @param loadedFrom Source of loaded image
* @return Bitmap which was displayed in {@link ImageView}
*/
Bitmap display(Bitmap bitmap, ImageView imageView);
Bitmap display(Bitmap bitmap, ImageView imageView, LoadedFrom loadedFrom);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*******************************************************************************/
package com.nostra13.universalimageloader.core.display;

import com.nostra13.universalimageloader.core.assist.LoadedFrom;

import android.graphics.Bitmap;
import android.view.animation.AlphaAnimation;
import android.view.animation.DecelerateInterpolator;
Expand All @@ -35,7 +37,7 @@ public FadeInBitmapDisplayer(int durationMillis) {
}

@Override
public Bitmap display(Bitmap bitmap, ImageView imageView) {
public Bitmap display(Bitmap bitmap, ImageView imageView, LoadedFrom loadedFrom) {
imageView.setImageBitmap(bitmap);

animate(imageView, durationMillis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

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

/**
* Fake displayer which doesn't display Bitmap in ImageView. Should be used in {@linkplain DisplayImageOptions display
Expand All @@ -32,7 +33,7 @@
*/
public final class FakeBitmapDisplayer implements BitmapDisplayer {
@Override
public Bitmap display(Bitmap bitmap, ImageView imageView) {
public Bitmap display(Bitmap bitmap, ImageView imageView, LoadedFrom loadedFrom) {
// Do nothing
return bitmap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.graphics.RectF;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.assist.LoadedFrom;
import com.nostra13.universalimageloader.utils.L;

/**
Expand All @@ -46,7 +47,7 @@ public RoundedBitmapDisplayer(int roundPixels) {
}

@Override
public Bitmap display(Bitmap bitmap, ImageView imageView) {
public Bitmap display(Bitmap bitmap, ImageView imageView, LoadedFrom loadedFrom) {
Bitmap roundedBitmap = roundCorners(bitmap, imageView, roundPixels);
imageView.setImageBitmap(roundedBitmap);
return roundedBitmap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*******************************************************************************/
package com.nostra13.universalimageloader.core.display;

import com.nostra13.universalimageloader.core.assist.LoadedFrom;

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

Expand All @@ -26,7 +28,7 @@
*/
public final class SimpleBitmapDisplayer implements BitmapDisplayer {
@Override
public Bitmap display(Bitmap bitmap, ImageView imageView) {
public Bitmap display(Bitmap bitmap, ImageView imageView, LoadedFrom loadedFrom) {
imageView.setImageBitmap(bitmap);
return bitmap;
}
Expand Down

0 comments on commit d810a95

Please sign in to comment.