奔跑的肥猪

导航

Android如何异步获取图片

在页面跳转过程中,如果target activity中包含有图片,可以在target activty的oncreate中启动一个线程去拿取图片,在target activity onstop

是,可以结束该线程。笔者曾经做过测试,如果在跳转之前那图片的话,时间是跳转之后拿图片的1.5倍。推荐使用android中内置线程AsyncTask,因为该线程和thread最大的不同是可以随时stop该线程,而不像thread一旦run起来,

内置方法里面根本没有办法取消它。

if (updateImageTask != null) {
				if (updateImageTask.getStatus() == AsyncTask.Status.RUNNING || updateImageTask.getStatus() == AsyncTask.Status.PENDING) {
					Log.d(TAG, "~~~!!!!! Canceled update task in back event......");
					updateImageTask.cancel(true);
				}
}

效果如下:

下面将该方法贴出来:

1.首先在oncreate 中声明一下:

updateImageTask = new UpdateImageAsyncTask();
updateImageTask.execute(null);

2. doInBackground,onProgressUpdate,首先是从队列中拿出对用的图片url,从网络中拿到bitmap,把该信息放在list中,最后传给根据index

传给相应的view

在具体代码如下:

	private class UpdateImageAsyncTask extends AsyncTask<Object, Object, Object> {
		@Override
		protected Object doInBackground(Object... params) {
			ArrayList<Object[]> resultList = new ArrayList<Object[]>();
			for (int i = 0; i < 4; i++) {
				resultList.add(null);
			}
			GetCatalogHomePageBlock catalogEditorsBlock = null;
			if (Constant.isTrueDrivce)
				catalogEditorsBlock = (GetCatalogHomePageBlock) getCatalogHomePageparsing.getBlockObjectByBlockPosition(getCatalogHomePageRsp, "B");
			else
				catalogEditorsBlock = (GetCatalogHomePageBlock) getCatalogHomePageparsing.getBlockObjectByBlockPosition(getCatalogHomePageRsp, "A");

			if (catalogEditorsBlock != null) {
				ArrayList catalogEditorsBlockContents = (ArrayList) getCatalogHomePageparsing.getCatalogEditorsBlockContentFromBlockObj(catalogEditorsBlock);
				if (catalogEditorsBlockContents != null) {
					for (int x = 0; x < catalogEditorsBlockContents.size(); x++) {
						CatalogEditorsBlockContent catalogEditorsBlockContent = (CatalogEditorsBlockContent) catalogEditorsBlockContents.get(x);
						String contentName = catalogEditorsBlockContent.getContentName();
						String smallLogo = catalogEditorsBlockContent.getSmallLogo() == null ? "" : catalogEditorsBlockContent.getSmallLogo();
						String contentID = catalogEditorsBlockContent.getContentID();
						if (x > 3) {
							break;
						}
						Object[] obj;
						switch (x) {
						case 0:
							bitmap0 = displayImage(IMAGEHOSTURL + smallLogo, contentID);
							obj = new Object[] { contentName, contentID, bitmap0, smallLogo };
							resultList.add(x, obj);
							break;
						case 1:
							bitmap1 = displayImage(IMAGEHOSTURL + smallLogo, contentID);
							obj = new Object[] { contentName, contentID, bitmap1, smallLogo };
							resultList.add(x, obj);
							break;
						case 2:
							bitmap2 = displayImage(IMAGEHOSTURL + smallLogo, contentID);
							obj = new Object[] { contentName, contentID, bitmap2, smallLogo };
							resultList.add(x, obj);
							break;
						case 3:
							bitmap3 = displayImage(IMAGEHOSTURL + smallLogo, contentID);
							obj = new Object[] { contentName, contentID, bitmap3, smallLogo };
							resultList.add(x, obj);
							break;

						}

					}
					publishProgress(resultList.toArray());
				}
				catalogEditorsBlockContents = null;
			}
			return null;
		}

		protected void onProgressUpdate(Object... values) {
			showImages(values);
		}

	}

3.最后在onkey事件back cancel当前获取图片线程

	if (updateImageTask != null) {
				if (updateImageTask.getStatus() == AsyncTask.Status.RUNNING || updateImageTask.getStatus() == AsyncTask.Status.PENDING) {
					Log.d(TAG, "~~~!!!!! Canceled update task in back event......");
					updateImageTask.cancel(true);
				}
	}

posted on 2011-07-13 10:41  布兜兜  阅读(919)  评论(0编辑  收藏  举报