1, extends ListActivity
2, getListView().setOnScrollListener(mScrollListener);
3,
控制加载图片的情境:
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case OnScrollListener.SCROLL_STATE_IDLE: // stop fling
isFling = false;
mAdapter.isBusy = false;
mAdapter.isThumbnailBusy = false;
break;
case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL: // touch fling
mAdapter.isBusy = false;
if(isFling){
setViewInfo(view);
isFling = false;
}
break;
case OnScrollListener.SCROLL_STATE_FLING: // fast fling
isFling = true;
if (view.getFirstVisiblePosition() != 0 && view.getLastVisiblePosition() != view.getCount() - 1) {
mAdapter.isBusy = true;
mAdapter.isThumbnailBusy = true;
}
break;
}
4, 对bitmap对象做缓存,这里用的软引用。
public class BitmapCacheForLocal
{
static private BitmapCacheForLocal bitmapCacheForLocal;
/** 用于Cache内容的存储 */
private HashMap<String, MySoftRef> mImageCaches =new LinkedHashMap<String, BitmapCacheForLocal.MySoftRef>(){
};
public HashMap<String, MySoftRef> getmImageCaches()
{
return mImageCaches;
}
/** 垃圾Reference的队列(所引用的对象已经被回收,则将该引用存入队列中) */
private ReferenceQueue<Bitmap> queue;
/**
* 继承SoftReference,使得每一个实例都具有可识别的标识。
*/
private class MySoftRef extends SoftReference<Bitmap>
{
private String _key;
public MySoftRef(Bitmap bmp, ReferenceQueue<Bitmap> queue, String key)
{
super(bmp, queue);
_key = key;
}
}
private BitmapCacheForLocal()
{
mImageCaches = new LinkedHashMap<String, MySoftRef>(50, 0.75f, true);
queue = new ReferenceQueue<Bitmap>();
}
/**
* 取得缓存器实例
*/
public static BitmapCacheForLocal getInstance()
{
if (bitmapCacheForLocal == null)
{
bitmapCacheForLocal = new BitmapCacheForLocal();
}
return bitmapCacheForLocal;
}
/**
* 以软引用的方式对一个Bitmap对象的实例进行引用并保存该引用
*/
public void addCacheBitmap(Bitmap bmp, String key)
{
// cleanCache();// 清除垃圾引用
MySoftRef ref = new MySoftRef(bmp, queue, key);
mImageCaches.put(key, ref);
}
public Bitmap getBitmap(String path)
{
Bitmap bmp = null;
// 缓存中是否有该Bitmap实例的软引用,如果有,从软引用中取得。
if (mImageCaches.containsKey(path))
{
MySoftRef ref = (MySoftRef) mImageCaches.get(path);
bmp = (Bitmap) ref.get();
}
return bmp;
}
public void cleanCache()
{
MySoftRef ref = null;
while ((ref = (MySoftRef) queue.poll()) != null)
{
mImageCaches.remove(ref._key);
}
}
/**
* 清除Cache内的全部内容
*/
public void clearCache()
{
cleanCache();
mImageCaches.clear();
System.gc();
System.runFinalization();
}
}