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; } } }
生活不应该只有工作,Stay还有更多编程与感悟希望和你分享。如果我的磁场吸引到你,请关注微信:stay4it,QQ:104816053。