使用开源imageLoader优化listview加载大量本地图片的demo

package com.example.listviewdemo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.smalt.smaltmap.utils.GlobalConstant;

/**
 * 
 * @Title:
 * @Description:ListView显示大量图片
 * @Copyright: Copyright (c) 2014
 * @Company: 
 * @author: Administrator
 * @version: 1.0.0.0
 * @Date: 2014-2-14
 */
public class MainActivity01 extends AbsListViewBaseActivity {
	DisplayImageOptions options; // 配置图片加载及显示选项
	ListView lvShow;
	/**
	 * 存储图片地址
	 */
	ArrayList listImgPath;
	String[] imageUriArray;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		lvShow = (ListView) findViewById(R.id.lv_show);
		// 扫描内存中图片并存入list

		listImgPath = getImgPathList();
		if (listImgPath.size() < 1) {
			GlobalConstant.i("sd卡中无图片");
		} else {
			// list转成数组
			imageUriArray = (String[]) listImgPath
					.toArray(new String[listImgPath.size()]);
			// 配置图片加载及显示选项(还有一些其他的配置,查阅doc文档吧)
			options = new DisplayImageOptions.Builder()
					.showStubImage(R.drawable.ic_launcher) // 在ImageView加载过程中显示图片
					.showImageForEmptyUri(R.drawable.ic_launcher) // image连接地址为空时
					.showImageOnFail(R.drawable.ic_launcher) // image加载失败
					.cacheInMemory(true) // 加载图片时会在内存中加载缓存
					.cacheOnDisc(true) // 加载图片时会在磁盘中加载缓存
					.displayer(new RoundedBitmapDisplayer(20)) // 设置用户加载图片task(这里是圆角图片显示)
					.build();
			// 加载适配器
			lvShow.setAdapter(new AdapterList());
		}

	}
	

	@Override
	public void onBackPressed() {
		AnimateFirstDisplayListener.displayedImages.clear();
		super.onBackPressed();
	}

	/**
	 * 
	 * 获取图片地址列表
	 * 
	 * @return list
	 */
	private ArrayList getImgPathList() {
		ArrayList list = new ArrayList();
		Cursor cursor = getContentResolver().query(
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
				new String[] { "_id", "_data" }, null, null, null);
		while (cursor.moveToNext()) {
			list.add(cursor.getString(1));// 将图片路径添加到list中
		}
		cursor.close();
		return list;
	}

	/** 图片加载监听事件 **/
	private static class AnimateFirstDisplayListener extends
			SimpleImageLoadingListener {

		static final List displayedImages = Collections
				.synchronizedList(new LinkedList());

		@Override
		public void onLoadingComplete(String imageUri, View view,
				Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500); // 设置image隐藏动画500ms
					displayedImages.add(imageUri); // 将图片uri添加到集合中
				}
			}
		}
	}

	/**
	 * 
	 * @Title:
	 * @Description:listview适配器
	 * @Copyright: Copyright (c) 2014
	 * @Company: 
	 * @author: Administrator
	 * @version: 1.0.0.0
	 * @Date: 2014-2-14
	 */
	class AdapterList extends BaseAdapter {
		private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();

		

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return imageUriArray.length;
		}

		/**
		 * 构造方法
		 */
		public AdapterList() {
			// TODO Auto-generated constructor stub
		}

		
		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.widget.Adapter#getItemId(int)
		 */
		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.widget.Adapter#getView(int, android.view.View,
		 * android.view.ViewGroup)
		 */
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				// 初始化绑定控件
				convertView = getLayoutInflater().inflate(R.layout.layout_item, null);
				holder.imgShow = (ImageView) findViewById(R.id.img_show);
				holder.tvShow = (TextView) findViewById(R.id.tv_show);
				// add to convertView
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			// 设置img,text具体显示的内容

			holder.tvShow.setText("fuck" + (position));

			imageLoader.displayImage(imageUriArray[position], holder.imgShow,
					options, animateFirstListener);
			return convertView;
		}
	}

	static class ViewHolder {
		ImageView imgShow;
		TextView tvShow;
	}
}

demo下载:

https://drive.google.com/file/d/0B3vXJifsRl4bTk8wUFpyNk9lNEU/edit?usp=sharing


你可能感兴趣的:(android,转帖)