WEB开发网
开发学院手机开发Android 开发 android平台UniversalImageLoader(异步加载大量图... 阅读

android平台UniversalImageLoader(异步加载大量图片)

 2012-10-13 21:10:44 来源:WEB开发网   
核心提示: UniversalImageLoader是实现异步加载大量图片的源码和例子,包括缓存、硬盘缓存、容错机制等技术,android平台UniversalImageLoader(异步加载大量图片), 项目如图:测试效果如图:总共有四种格式效果!第一种ImageList,效果如图:关键测试代码如下: public clas

 UniversalImageLoader是实现异步加载大量图片的源码和例子,包括缓存、硬盘缓存、容错机制等技术。

 项目如图:
测试效果如图:
总共有四种格式效果!
第一种ImageList,效果如图:
关键测试代码如下:
 
 
public class ImageListActivity extends BaseActivity
{

	private String[] imageUrls;

	private DisplayImageOptions options;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_image_list);

		Bundle bundle = getIntent().getExtras();
		imageUrls = bundle.getStringArray(Extra.IMAGES);

		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.stub_image).cacheInMemory()
				.cacheOnDisc().build();

		ListView listView = (ListView) findViewById(android.R.id.list);
		listView.setAdapter(new ItemAdapter());
		listView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id)
			{
				startImageGalleryActivity(position);
			}
		});
	}

	@Override
	protected void onStop()
	{
		imageLoader.stop();
		super.onStop();
	}

	private void startImageGalleryActivity(int position)
	{
		Intent intent = new Intent(this, ImagePagerActivity.class);
		intent.putExtra(Extra.IMAGES, imageUrls);
		intent.putExtra(Extra.IMAGE_POSITION, position);
		startActivity(intent);
	}

	class ItemAdapter extends BaseAdapter
	{

		private class ViewHolder
		{
			public TextView text;
			public ImageView image;
		}

		@Override
		public int getCount()
		{
			return imageUrls.length;
		}

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

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

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent)
		{
			View view = convertView;
			final ViewHolder holder;
			if (convertView == null)
			{
				view = getLayoutInflater().inflate(R.layout.item_list_image,
						null);
				holder = new ViewHolder();
				holder.text = (TextView) view.findViewById(R.id.text);
				holder.image = (ImageView) view.findViewById(R.id.image);
				view.setTag(holder);
			} else
				holder = (ViewHolder) view.getTag();

			holder.text.setText("Item " + position);

			imageLoader
					.displayImage(imageUrls[position], holder.image, options);

			return view;
		}
	}
}

1 2  下一页

Tags:android 平台 UniversalImageLoader

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接