用Listview显示图片,然后不停滑动会OOM的解决方法:

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();
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值