java 异步加载图片_android图片异步加载 列表

mTaskMap;// 需要加载图片并显示的图片视图对象任务链

083 private boolean mIsWait;// 标识是线程是否处于等待状态

084

085 public LoaderThread(ImageView imageView, String url) {

086 mTaskMap = new LinkedHashMap();

087 mTaskMap.put(url, imageView);

088 }

089

090 /**

091 * 处理某个视图的更新显示

092 *

093 * @param imageView

094 */

095 public void load(ImageView imageView, String url) {

096 mTaskMap.remove(imageView);// 任务链中可能有,得先删除

097 mTaskMap.put(url, imageView);// 将其添加到任务中

098 if (mIsWait) {// 如果线程此时处于等待得唤醒线程去处理任务队列中待处理的任务

099 synchronized (this) {// 调用对象的notify()时必须同步

100 this.notify();

101 }

102 }

103 }

104

105 @Override

106 public void run() {

107 while (mTaskMap.size() > 0) {// 当队列中有数据时线程就要一直运行,一旦进入就要保证其不会跳出循环

108 mIsWait = false;

109 final String url = mTaskMap.keySet().iterator().next();

110 final ImageView imageView = mTaskMap.remove(url);

111 if (imageView.getTag() == url) {// 判断视图有没有复用(一旦ImageView被复用,其tag值就会修改变)

112 final Bitmap bitmap = MyConnection.getBitmapByUrl(url);// 此方法应该是从网络或sd卡中加载

113 try {

114 Thread.sleep(1000);// 模拟网络加载数据时间

115 } catch (InterruptedException e1) {

116 e1.printStackTrace();

117 }

118 // 将加载的图片放入缓存map中

119 imageCache.put(url, new SoftReference(bitmap));

120 if (url == imageView.getTag()) {// 再次判断视图有没有复用

121 handler.post(new Runnable() {// 通过消息机制在主线程中更新UI

122 @Override

123 public void run() {

124 imageView.setImageBitmap(bitmap);

125 }

126 });

127 }

128 }

129 if (mTaskMap.isEmpty()) {// 当任务队列中没有待处理的任务时,线程进入等待状态

130 try {

131 mIsWait = true;// 标识线程的状态,必须在wait()方法之前

132 synchronized (this) {

133 this.wait();// 保用线程进入等待状态,直到有新的任务被加入时通知唤醒

134 }

135 } catch (InterruptedException e) {

136 e.printStackTrace();

137 }

138 }

139 }

140 }

141 }

142 }

查看源码打印?01 private class ProductListAdapter extends BaseAdapter {

02

03 private AsynImageLoader mImageAsynLoader;

04

05 public ProductListAdapter() {

06 mImageAsynLoader = new AsynImageLoader(mHandler);

07 }

08

09 @Override

10 public int getCount() {

11 int size = Math.min(mLastItemViewIndex + 1, mDataList.size());

12 mLastItemViewIndex = size - 1;

13 return size;

14 }

15

16 @Override

17 public Object getItem(int position) {

18 return mDataList.get(position);

19 }

20

21 @Override

22 public long getItemId(int position) {

23 return position;

24 }

25

26 @Override

27 public View getView(int position, View convertView, ViewGroup parent) {

28 if (convertView == null) {

29 convertView = getLayoutInflater().inflate(R.layout.product_list_item,

30 null);

31 }

32 ImageView imageView = (ImageView) convertView

33 .findViewById(R.id.iv_item_product_image);

34 Mapmap = mDataList.get(position);

35 //存放图片所对应的url

36 imageView.setTag(map.get("product_pic_address"));

37 mImageAsynLoader.loadBitmap(imageView, mDefautBitmap);

38 return convertView;

39 }

40 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值