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异步加载源码


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值