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