基于comeGetSome的答案,我创建了自己的实现,使用普通的ImageView类而非创建新的UrlImageView类,还提供了新的选项,例如加载完成或取消时要执行何种操作。
现在可以根据需要加载图像,可以使用任意三个loadImage方法中的任一个,就像这样:
UrlImageLoader urlImageLoader=new UrlImageLoader();
ImageView imageView=new ImageView(context);
urlImageLoader.loadImage(imageView, "http://www......com/.....jpg");
urlImageLoader.loadImage("http://www......com/.....jpg", new UrlImageLoader.OnLoadingCompleteListener() {
@Override
public void onComplete(ImageView imageView, Bitmap bmp) {
}
@Override
public void onCancel(ImageView imageView) {
}
});
urlImageLoader.loadImage(imageView, "http://www......com/.....jpg", new UrlImageLoader.OnLoadingCompleteListener() {
@Override
public void onComplete(ImageView imageView, Bitmap bmp) {
imageView.setImageBitmap(bmp);
}
@Override
public void onCancel(ImageView imageView) {
}
});
创建这个类来加载UrlImageLoader。
package com.GameG.SealTheBox;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;
public class UrlImageLoader {
public static interface OnLoadingCompleteListener {
public void onComplete(ImageView imageView, Bitmap bmp);
public void onCancel(ImageView imageView);
}
ArrayList<UrlImageLoadingTask> loadingList=new ArrayList<UrlImageLoadingTask>();
public void loadImage(ImageView imageView, String url, OnLoadingCompleteListener onComplete){
try {
URL url2=new URL(url);
if(imageView!=null){
for(int i=0;i<loadingList.size();i++){
UrlImageLoadingTask tmptask=loadingList.get(i);
if(tmptask.updateView!=null && tmptask.updateView.equals(imageView)){
tmptask.cancel(true);
break;
}
}
}
UrlImageLoadingTask loadtask=new UrlImageLoadingTask(imageView,onComplete,url);
loadtask.execute(url2);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public void loadImage(String url, OnLoadingCompleteListener onComplete){
loadImage(null,url,onComplete);
}
public void loadImage(ImageView imageView, String url){
loadImage(imageView,url,null);
}
public void cancel(ImageView imageView){
for(int i=0;i<loadingList.size();i++){
UrlImageLoadingTask tmptask=loadingList.get(i);
if(tmptask.updateView.equals(imageView)){
loadingList.remove(i);
tmptask.cancel(true);
break;
}
}
}
public void cancel(String url){
for(int i=0;i<loadingList.size();i++){
UrlImageLoadingTask tmptask=loadingList.get(i);
if(tmptask.url.equals(url)){
loadingList.remove(i);
tmptask.cancel(true);
break;
}
}
}
public void cancelAll(){
while(loadingList.size()>0){
UrlImageLoadingTask tmptask=loadingList.get(0);
loadingList.remove(tmptask);
tmptask.cancel(true);
}
}
private class UrlImageLoadingTask extends AsyncTask<URL, Void, Bitmap> {
public ImageView updateView=null;
public String url;
private boolean isCancelled = false;
private InputStream urlInputStream;
private OnLoadingCompleteListener onComplete=null;
private UrlImageLoadingTask(ImageView updateView, OnLoadingCompleteListener onComplete, String url) {
this.updateView=updateView;
this.onComplete=onComplete;
this.url=url;
}
@Override
protected Bitmap doInBackground(URL... params) {
try {
URLConnection con = params[0].openConnection();
con.setUseCaches(true);
this.urlInputStream = con.getInputStream();
return BitmapFactory.decodeStream(urlInputStream);
} catch (IOException e) {
Log.w(UrlImageView.class.getName(), "failed to load image from " + params[0], e);
return null;
} finally {
if (this.urlInputStream != null) {
try {
this.urlInputStream.close();
} catch (IOException e) {
;
} finally {
this.urlInputStream = null;
}
}
}
}
@Override
protected void onPostExecute(Bitmap result) {
if (!this.isCancelled) {
if(onComplete==null){
if(updateView!=null)
this.updateView.setImageBitmap(result);
}else{
onComplete.onComplete(updateView, result);
}
}
loadingList.remove(this);
}
@Override
protected void onCancelled() {
this.isCancelled = true;
try {
if (this.urlInputStream != null) {
try {
this.urlInputStream.close();
} catch (IOException e) {
;
} finally {
this.urlInputStream = null;
}
}
} finally {
super.onCancelled();
if(onComplete!=null)
onComplete.onCancel(updateView);
loadingList.remove(this);
}
}
}
}