Android ListView异步加载图片

http://www.cnblogs.com/stay/articles/1900012.html


01 package cn.riddles.activity;
02  
03 import android.app.Activity;
04 import android.os.Bundle;
05 import android.widget.ListView;
06  
07 public class MainActivity extends Activity {
08     private ListView lv;
09     @Override
10     public void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.main);
13         lv = (ListView) this.findViewById(R.id.test_lv);
14         lv.setAdapter(new SongListAdapter(this));
15     }
16 }
01 package cn.riddles.activity;
02  
03 import android.content.Context;
04 import android.util.Log;
05 import android.view.LayoutInflater;
06 import android.view.View;
07 import android.view.ViewGroup;
08 import android.widget.BaseAdapter;
09 import android.widget.ImageView;
10 import android.widget.TextView;
11 /**
12  * @author riddlezhang 歌曲条目适配器
13  */
14 public class SongListAdapter extends BaseAdapter {
15     private static final String TAG = "SongListAdapter";
16     private Context mContext;
17     private String[] strings = {"王力宏","吴尊","何润东","金城武","吴彦祖"};
23     public SongListAdapter(Context mContext) {
24         this.mContext = mContext;
25     }
26  
27     public void setmContext(Context mContext) {
28         this.mContext = mContext;
29     }
30  
31     public int getCount() {
32         return paths.length;
33     }
34  
35     public Object getItem(int position) {
36         return position;
37     }
38  
39     public long getItemId(int position) {
40         return position;
41     }
42  
43     public View getView(int position, View convertView, ViewGroup parent) {
44         convertView = LayoutInflater.from(mContext).inflate(R.layout.lv_adapter, null);
45         ImageView image = (ImageView) convertView.findViewById(R.id.image);
46         TextView songer = (TextView) convertView.findViewById(R.id.songer);
47         image.setTag(paths[position]);
48         songer.setText(strings[position]);
49         new CanvasImageTask().execute(image);//异步加载图片
50         Log.i(TAG, "execute:"+strings[position]);
51         return convertView;
52     }
53      
54 }
1  
1  
01 <div class="cnblogs_Highlighter"><pre class="brush:java;gutter:false;">package cn.riddles.activity;
02  
03 import java.io.InputStream;
04 import java.lang.ref.SoftReference;
05 import java.net.HttpURLConnection;
06 import java.net.URL;
07 import java.util.HashMap;
08  
09 import android.content.Context;
10 import android.graphics.drawable.Drawable;
11 import android.os.AsyncTask;
12 import android.util.Log;
13 import android.view.View;
14 import android.webkit.URLUtil;
15  
16 /**
17  * @author riddlezhang 异步加载图片
18  */
19 public class AsyncViewTask extends AsyncTask<View, Void, Drawable> {
20     private View mView;
21     private HashMap<String, SoftReference<Drawable>> imageCache;
22  
23     public AsyncViewTask(Context mContext, String mobileinfo) {
24         imageCache = new HashMap<String, SoftReference<Drawable>>();
25     }
26  
27     protected Drawable doInBackground(View... views) {
28         Drawable drawable = null;
29         View view = views[0];
30         if (view.getTag() != null) {
31             if (imageCache.containsKey(view.getTag())) {
32                 SoftReference<Drawable> cache = imageCache.get(view.getTag().toString());
33                 drawable = cache.get();
34                 if (drawable != null) {
35                     return drawable;
36                 }
37             }
38             try {
39                 if (URLUtil.isHttpUrl(view.getTag().toString())) {// 如果为网络地址。则连接url下载图片
40                     URL url = new URL(view.getTag().toString());
41                     HttpURLConnection conn = (HttpURLConnection) url.openConnection();
42                     conn.setDoInput(true);
43                     conn.connect();
44                     InputStream stream = conn.getInputStream();
45                     drawable = Drawable.createFromStream(stream, "src");
46                     stream.close();
47                 else {// 如果为本地数据,直接解析
48                     drawable = Drawable.createFromPath(view.getTag().toString());
49                 }
50             catch (Exception e) {
51                 Log.v("img", e.getMessage());
52                 return null;
53             }
54         }
55         this.mView = view;
56         return drawable;
57     }
58  
59     protected void onPostExecute(Drawable drawable) {
60         if (drawable != null) {
61             this.mView.setBackgroundDrawable(drawable);
62             this.mView = null;
63         }
64     }
65  
66 }
67 </pre>
68 </div>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值