Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android中異步加載網絡圖片

android中異步加載網絡圖片

編輯:關於Android編程

思路:先為ListView或者GridView等設置默認圖片,然後啟用異步線程下載網絡圖片然後更新adapter,刷新UI,代碼如下:

MainActivity:

package com.home.asynload;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {
	private List list = new ArrayList();
	private ImageAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initImage();
		// 啟動異步線程加載網絡圖片
		new ImageLoadTask(adapter).execute();
	}

	/**
	 * 設置初始圖片
	 */
	private void initImage() {
		for (int i = 0; i < 10; i++) {
			ImageBean bean = new ImageBean();
			bean.setBitmap(BitmapFactory.decodeResource(getResources(),
					R.drawable.ic_launcher));
			list.add(bean);
		}
		adapter = new ImageAdapter(MainActivity.this, list);
		ListView listView = new ListView(MainActivity.this);
		listView.setAdapter(adapter);
		adapter.notifyDataSetChanged();
		setContentView(listView);
	}

}

ImageAdapter:

package com.home.asynload;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

public class ImageAdapter extends BaseAdapter {
	private Context context;
	private List list = new ArrayList();

	public ImageAdapter(Context context, List list) {
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		LinearLayout layout = new LinearLayout(context);
		ImageView imageView = new ImageView(context);
		layout.addView(imageView);
		ImageBean bean = list.get(position);
		if (bean.getBitmap() != null) {
			imageView.setImageBitmap(bean.getBitmap());
		}
		if (bean.isNeedBar()) {
			ProgressBar bar = new ProgressBar(context);
			layout.addView(bar);
		}
		return layout;
	}

}

ImageLoadTask:

package com.home.asynload;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;

public class ImageLoadTask extends AsyncTask {
	private ImageAdapter adapter;

	public ImageLoadTask(ImageAdapter adapter) {
		this.adapter = adapter;
	}

	@Override
	protected Void doInBackground(Void... params) {
		for (int i = 0; i < adapter.getCount(); i++) {
			ImageBean bean = (ImageBean) adapter.getItem(i);
			Bitmap bitmap = BitmapFactory
					.decodeStream(getStream("http://avatar.profile.csdn.net/A/E/5/2_piaopiaohu123.jpg"));
			bean.setBitmap(bitmap);
			bean.setNeedBar(false);
			publishProgress(); // 通知去更新UI
		}
		return null;
	}

	public void onProgressUpdate(Void... voids) {
		if (isCancelled()) {
			return;
		}
		// 更新UI
		adapter.notifyDataSetChanged();
	}

	public InputStream getStream(String urlStr) {
		InputStream inStream = null;
		try {
			URL url = new URL(urlStr);
			URLConnection conn = url.openConnection();
			conn.setConnectTimeout(10 * 1000);
			inStream = conn.getInputStream();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return inStream;
	}
}

ImageBean:

package com.home.asynload;

import android.graphics.Bitmap;

public class ImageBean {
	private Bitmap bitmap;
	private boolean isNeedBar = true;

	public ImageBean() {
		super();
	}

	public boolean isNeedBar() {
		return isNeedBar;
	}

	public void setNeedBar(boolean isNeedBar) {
		this.isNeedBar = isNeedBar;
	}

	public Bitmap getBitmap() {
		return bitmap;
	}

	public void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}

}





  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved