http://www.cnblogs.com/stay/articles/1900012.html
01 | package cn.riddles.activity; |
03 | import android.app.Activity; |
04 | import android.os.Bundle; |
05 | import android.widget.ListView; |
07 | public class MainActivity extends Activity { |
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 )); |
01 | package cn.riddles.activity; |
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; |
12 | * @author riddlezhang 歌曲条目适配器 |
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; |
27 | public void setmContext(Context mContext) { |
28 | this .mContext = mContext; |
31 | public int getCount() { |
35 | public Object getItem( int position) { |
39 | public long getItemId( int position) { |
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]); |
01 | <div class = "cnblogs_Highlighter" ><pre class = "brush:java;gutter:false;" > package cn.riddles.activity; |
03 | import java.io.InputStream; |
04 | import java.lang.ref.SoftReference; |
05 | import java.net.HttpURLConnection; |
07 | import java.util.HashMap; |
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; |
17 | * @author riddlezhang 异步加载图片 |
19 | public class AsyncViewTask extends AsyncTask<View, Void, Drawable> { |
21 | private HashMap<String, SoftReference<Drawable>> imageCache; |
23 | public AsyncViewTask(Context mContext, String mobileinfo) { |
24 | imageCache = new HashMap<String, SoftReference<Drawable>>(); |
27 | protected Drawable doInBackground(View... views) { |
28 | Drawable drawable = null ; |
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 ) { |
39 | if (URLUtil.isHttpUrl(view.getTag().toString())) { |
40 | URL url = new URL(view.getTag().toString()); |
41 | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
42 | conn.setDoInput( true ); |
44 | InputStream stream = conn.getInputStream(); |
45 | drawable = Drawable.createFromStream(stream, "src" ); |
48 | drawable = Drawable.createFromPath(view.getTag().toString()); |
50 | } catch (Exception e) { |
51 | Log.v( "img" , e.getMessage()); |
59 | protected void onPostExecute(Drawable drawable) { |
60 | if (drawable != null ) { |
61 | this .mView.setBackgroundDrawable(drawable); |