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)); }}
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","http://list.image.baidu.com/t/image_category/galleryimg/menstar/hk/wu_zun.jpg","http://list.image.baidu.com/t/image_category/galleryimg/menstar/hk/he_run_dong.jpg","http://list.image.baidu.com/t/image_category/galleryimg/menstar/hk/jin_cheng_wu.jpg","http://list.image.baidu.com/t/image_category/galleryimg/menstar/hk/wu_yan_zu.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;}}
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;}}}