Permalink
Browse files

Fix bug

  • Loading branch information...
1 parent 6b099d2 commit 8a476a262f4faa466ac2ecc250f37550e832854b TangKe committed Aug 29, 2012
Showing with 72 additions and 11 deletions.
  1. +19 −11 Aretha/src/com/aretha/content/AsyncImageLoader.java
  2. +53 −0 Aretha/src/com/aretha/widget/WebImageView.java
View
30 Aretha/src/com/aretha/content/AsyncImageLoader.java
@@ -88,6 +88,9 @@ public void loadImage(URI uri, OnImageLoadListener listener) {
* @param listener
*/
public void loadImage(String url, OnImageLoadListener listener) {
+ if (null == url) {
+ return;
+ }
loadImage(URI.create(url), listener);
}
@@ -122,6 +125,9 @@ public void cancel(String url) {
*/
private ImageLoadingTask obtainImageLoadingTask(URI uri,
OnImageLoadListener listener) {
+ if (null == uri || null == listener) {
+ return null;
+ }
ImageLoadingTask task = new ImageLoadingTask();
task.listener = listener;
task.uri = uri;
@@ -136,10 +142,6 @@ private void doLoadImage(ImageLoadingTask imageLoadingTask) {
return;
}
- if (null == imageLoadingTask.uri || null == imageLoadingTask.listener) {
- return;
- }
-
mTaskList.add(imageLoadingTask);
mExecutor.execute(imageLoadingTask);
}
@@ -207,21 +209,27 @@ public Bitmap readCachedBitmap(String imageIdentifier, int sampleSize) {
* Invoked when the image has been loaded
*
* @param bitmap
- * @param loadedImageUrl
+ * @param imageUrl
* @param fromCache
*/
- public void onLoaded(Bitmap bitmap, String loadedImageUrl,
+ public void onLoadSuccess(Bitmap bitmap, String imageUrl,
boolean fromCache);
/**
+ *
+ * @param imageUrl
+ */
+ public void onLoadError(String imageUrl);
+
+ /**
* You can decide whether to intercept the image load request in this
* method. Such as Wi-Fi only or this image is with the extension name
* 'png'
*
* @param imageUrl
* @return true to cancel this request, otherwise.
*/
- public boolean preImageLoad(String imageUrl);
+ public boolean preLoad(String imageUrl);
}
private class ImageLoadingTask implements Runnable {
@@ -243,7 +251,7 @@ public boolean equals(Object o) {
public void run() {
Message message = mImageLoadedHandler.obtainMessage(STATUS_SUCCESS,
this);
- if (listener != null && listener.preImageLoad(uri.toString())) {
+ if (listener != null && listener.preLoad(uri.toString())) {
message.what = STATUS_CANCEL;
message.sendToTarget();
return;
@@ -310,12 +318,12 @@ public void handleMessage(Message msg) {
// if this ImageLoadingTask has been canceled before it done. we
// can not invoke the callback.
if (isRemove) {
- task.listener.onLoaded(task.bitmap, task.uri.toString(),
- task.isLoadFromCache);
+ task.listener.onLoadSuccess(task.bitmap,
+ task.uri.toString(), task.isLoadFromCache);
}
break;
case STATUS_ERROR:
-
+ task.listener.onLoadError(task.uri.toString());
break;
case STATUS_CANCEL:
break;
View
53 Aretha/src/com/aretha/widget/WebImageView.java
@@ -0,0 +1,53 @@
+package com.aretha.widget;
+
+import com.aretha.content.AsyncImageLoader;
+import com.aretha.content.AsyncImageLoader.OnImageLoadListener;
+
+import android.content.Context;
+import android.graphics.Bitmap;
+import android.util.AttributeSet;
+import android.widget.ImageView;
+
+public class WebImageView extends ImageView implements OnImageLoadListener {
+ private String mImageUrl;
+
+ public WebImageView(Context context, AttributeSet attrs, int defStyle) {
+ super(context, attrs, defStyle);
+ }
+
+ public WebImageView(Context context, AttributeSet attrs) {
+ super(context, attrs);
+ }
+
+ public WebImageView(Context context) {
+ super(context);
+ }
+
+ public void setImageUrl(String url) {
+ mImageUrl = url;
+ AsyncImageLoader.getInstance(getContext()).loadImage(url, this);
+ }
+
+ public void setImageUrl(String url, Bitmap defaultImage) {
+ setImageBitmap(defaultImage);
+ setImageUrl(url);
+ }
+
+ @Override
+ public void onLoadSuccess(Bitmap bitmap, String loadedImageUrl,
+ boolean fromCache) {
+ if (mImageUrl.equals(loadedImageUrl)) {
+ setImageBitmap(bitmap);
+ }
+ }
+
+ @Override
+ public boolean preLoad(String imageUrl) {
+ return false;
+ }
+
+ @Override
+ public void onLoadError(String imageUrl) {
+
+ }
+}

0 comments on commit 8a476a2

Please sign in to comment.