Android ListView异步加载网络图片

异步加载图片栈类

package com.async.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;


//异步加载图片栈类
public class AsyncImageTask {
	//缓存图片
	private Map<String, Drawable> imageMap;

	public AsyncImageTask() {
		super();
		this.imageMap = new HashMap<String, Drawable>();
	}

	// ID为记录标识,标识是那条记录iamge
	public Drawable loadImage(final int id, final String imageUrl,
			final ImageCallback callback) {
		//查看缓存内是否已经加载过此图片
		if (imageMap.containsKey(imageUrl)) {
			Drawable drawable = imageMap.get(imageUrl);
			if (drawable != null) {
				return drawable;
			}
		}
		//更新图片UI的消息队列
		final Handler handler = new Handler() {
			public void handleMessage(Message message) {
				callback.imageLoaded((Drawable) message.obj, id);
			}
		};
		//加载图片的线程
		new Thread() {
			@Override
			public void run() {
				super.run();
				//加载图片
				Drawable drawable = AsyncImageTask.loadImageByUrl(imageUrl);
				//加入缓存集合中
				imageMap.put(imageUrl, drawable);
				//通知消息队列更新UI
				Message message = handler.obtainMessage(0, drawable);
				handler.sendMessage(message);
			}
		}.start();
		return null;
	}
	//根据图片地址加载图片,并保存为Drawable
	public static Drawable loadImageByUrl(String imageUrl) {

		URL url = null;
		InputStream inputStream = null;
		try {
			url = new URL(imageUrl);
			inputStream = (InputStream) url.getContent();
			return Drawable.createFromStream(inputStream, "src");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (inputStream != null) {
					inputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	//利用借口回调,更新图片UI
	public interface ImageCallback {
		public void imageLoaded(Drawable image, int id);
	}
}

适配器类:

package com.alarm.adapter;

import java.util.List;

import com.alarm.activity.R;
import com.alarm.controls.AsyncImageTask;
import com.alarm.controls.AsyncImageTask.ImageCallback;
import com.alarm.model.Topic;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class TopicsAdapter extends BaseAdapter {
	private List<Topic> list;
	private LayoutInflater inflater;
	private ListView listView;
	private AsyncImageTask imageTask;

	public TopicsAdapter(ListView listView, Context c, List<Topic> list) {
		super();
		this.list = list;
		this.listView = listView;
		inflater = LayoutInflater.from(c);
		imageTask = new AsyncImageTask();
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = new ViewHolder();
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.topic_item, null);
			holder.mainBg = (ImageView) convertView.findViewById(R.id.main_bg);
			holder.note = (TextView) convertView.findViewById(R.id.memo);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// 标记图片视图,注意不能放在上面
		holder.mainBg.setTag(position);
		Topic topic = list.get(position);
		//获取图片
		Drawable drawable = imageTask.loadImage(position, topic.mainBg,
				new ImageCallback() {
					@Override
					public void imageLoaded(Drawable image, int id) {
						if (image != null) {
							 //重点,获取刚才标识的组件,并更新 
							ImageView mainBg = (ImageView) listView
									.findViewWithTag(id);
							if (mainBg != null) {
								mainBg.setImageDrawable(image);
							}
						}
					}
				});
		if (drawable != null) {
			holder.mainBg.setImageDrawable(drawable);
		} else {
			holder.mainBg.setImageResource(R.color.main_bg);
		}
		holder.note.setText(topic.note);
		holder.name.setText(topic.name);
		return convertView;
	}

	class ViewHolder {
		ImageView mainBg;
		TextView note;
		TextView name;
	}

}


ListView异步加载源码


posted @ 2012-09-18 20:42  可可ing  阅读(127)  评论(0编辑  收藏  举报