AysncTask异步加载图片

package com.lilin.async;

import cn.riddles.activity.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Main extends Activity {
	public ListView listView;
	public static String[] strings = { "王力宏", "吴尊", "何润东" };
	public static 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" };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		listView = (ListView) this.findViewById(R.id.test_lv);
		listView.setAdapter(new MyAdapter(this));
	}

}

class MyAdapter extends BaseAdapter {
	private Context context;

	public MyAdapter(Context context) {
		this.context = context;
	}

	public void setcontext(Context context) {
		this.context = context;
	}

	public int getCount() {
		return Main.paths.length;
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = LayoutInflater.from(context).inflate(R.layout.listview,
				null);
		ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
		TextView songer_tv = (TextView) convertView.findViewById(R.id.songer);
		imageView.setTag(Main.paths[position]);
		songer_tv.setText(Main.strings[position]);
		new ImageTask().execute(imageView);
		return convertView;
	}

}
package com.lilin.async;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.webkit.URLUtil;
import android.widget.ImageView;

/**
 * 通过AsyncTask实现图片的异步加载
 * 
 * @author lilin
 * @date 2011-9-5 上午08:45:12
 * @ClassName: ImageTask
 * @Description: 特点:任务不在主线程中执行,而回调方法在主线程中执行,比Handler更轻量级些,适用于简单的异步处理
 */
public class ImageTask extends AsyncTask<ImageView, Void, Bitmap> {
	private ImageView imageView;

	protected Bitmap doInBackground(ImageView... views) {
		// 后台线程执行,完成任务的主要操作
		Bitmap bitmap = null;
		ImageView imageView = views[0];
		if (imageView.getTag() != null) {
			try {
				if (URLUtil.isHttpUrl(imageView.getTag().toString())) {// 首先判断是不是url
					System.out.println(imageView.getTag().toString());
					URL url = new URL(imageView.getTag().toString());
					HttpURLConnection conn = (HttpURLConnection) url
							.openConnection();
					conn.setDoInput(true);
					conn.connect();
					InputStream stream = conn.getInputStream();
					bitmap = BitmapFactory.decodeStream(stream);// 将stream流转化成bitmap
					stream.close();
				} else {
					bitmap = BitmapFactory.decodeFile(imageView.getTag()
							.toString());
				}
			} catch (Exception e) {
				return null;
			}
		}
		this.imageView = imageView;
		return bitmap;
	}

	protected void onPostExecute(Bitmap bitmap) {
		// 主线程中执行
		if (bitmap != null) {
			this.imageView.setImageBitmap(bitmap);
			this.imageView = null;
		}
	}

}

相关推荐