码迷,mamicode.com
首页 > 其他好文 > 详细

ImageLoader学习笔记

时间:2015-05-01 17:26:13      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:多线程   imageloader   

这是根据ImageLoader画的一张类图,可以帮助我们更好地理解这个开源库。技术分享


这个开源库的优点:1、支持多线程下载图片。2、实现图片的两级缓存。
3、可以根据控件大小对Bitmap进行裁剪,减少Bitmap占用过多的内存
4、提供在较慢的网络对图片进行加载
5、较好的控制图片的加载过程,例如,滑动过程暂停加载图片,停止滑动的时候
去加载图片。


ImageLoader里面的getInstance()方法里面用到了单例设计模式。通过双层是否为null判断提高性能。
ImageLoaderConfigurationFactory用到了工厂模式
LimitedAgeMemoryCache是MemoryCache的装饰者,相当于为MemoryCache添加了一个特性。
ContentLengthInputStream是InputStream的装饰者,可以通过available()函数得到InputStream对应数据源的长度


ImageLoaderConfiguration里面的threadPollSize设置的是线程池的大小,这里设置的是3

在项目中用到了ListView显示图片的模块,平常加载图片都是通过HttpUrlConnection去进行网络请求,然后下载图片并显示在Activity上,但这种方法对于
ListView来说显然是不现实的,因此在网络找到了一个开源的库ImageLoader,里面除了能够实现基本功能之外,还实现了图片缓存(三级缓存,内存、sdcard,
网络(其实真正意义上只是两级)),读了其他人对ImageLoader的解析,对ImageLoader的实现原理也理解了很多,做个笔记,可以留作以后复习,顺便巩固下
学到的知识。


最少被访问的实现利用的是两个泛型,一个存储key和Bitmap类型的value,另外一个存储key还有long类型的value,当需要删除的时候就进行迭代,获得最少
被使用的key然后去删除对应的value。LimitedAgeMemoryCache的实现也是同样的道理。


这个库加载图片也是利用HttpClient,网络延迟的时候抛出异常,网络比较慢的时候调用SlowNetworkImageDownloader,它实现了ImageDownloader接口,


之前不是一直想搞明白是怎样实现缓存的?怎想实现LRU和FIFO,后来才知道原来是利用了LinkList和LinkHashMap,LinkList可以当成队列那样子用,
因此很容易就可以实现先来先删除的功能,而LinkHashMap有迭代排序的功能,默认的是插入排序,还有另外一种是访问排序,就是被访问到的那个图片
会将它的Key存放在表尾,并从原来位置删除,这样子每次都删除第一个的话就是删了那个最久没被访问到的图片。

public class LruMemoryCache implements MemoryCacheAware<String, Bitmap> {

	private final LinkedHashMap<String, Bitmap> map;
	//利用LinkHashMap来存储对应的数据
	private final int maxSize;
	/** Size of this cache in bytes */
	private int size;

	/** @param maxSize Maximum sum of the sizes of the Bitmaps in this cache */
	public LruMemoryCache(int maxSize) {
		if (maxSize <= 0) {
			throw new IllegalArgumentException("maxSize <= 0");
		}
		this.maxSize = maxSize;
		this.map = new LinkedHashMap<String, Bitmap>(0, 0.75f, true);
	}

	/**
	 * Returns the Bitmap for {@code key} if it exists in the cache. If a Bitmap was returned, it is moved to the head
	 * of the queue. This returns null if a Bitmap is not cached.
	 */
	@Override
	public final Bitmap get(String key) {
		if (key == null) {
			throw new NullPointerException("key == null");
		}

		synchronized (this) {
			return map.get(key);
		}
	}

	/** Caches {@code Bitmap} for {@code key}. The Bitmap is moved to the head of the queue. */
	@Override
	public final boolean put(String key, Bitmap value) {
		//把key和value存进去
		if (key == null || value == null) {
			throw new NullPointerException("key == null || value == null");
		}

		synchronized (this) {
			size += sizeOf(key, value);
			Bitmap previous = map.put(key, value);
			if (previous != null) {
				size -= sizeOf(key, previous);
			}
		}

		trimToSize(maxSize);
		return true;
	}

	/**
	 * Remove the eldest entries until the total of remaining entries is at or below the requested size.
	 *
	 * @param maxSize the maximum size of the cache before returning. May be -1 to evict even 0-sized elements.
	 */
	private void trimToSize(int maxSize) {
		while (true) {
			String key;
			Bitmap value;
			synchronized (this) {
				if (size < 0 || (map.isEmpty() && size != 0)) {
					throw new IllegalStateException(getClass().getName() + ".sizeOf() is reporting inconsistent results!");
				}

				if (size <= maxSize || map.isEmpty()) {
					break;
				}

				Map.Entry<String, Bitmap> toEvict = map.entrySet().iterator().next();
				if (toEvict == null) {
					break;
				}
				key = toEvict.getKey();
				value = toEvict.getValue();
				map.remove(key);
				size -= sizeOf(key, value);
			}
		}
	}

	/** Removes the entry for {@code key} if it exists. */
	//将对应的key(其实就是第一个元素)移除
	@Override
	public final void remove(String key) {
		if (key == null) {
			throw new NullPointerException("key == null");
		}

		synchronized (this) {
			Bitmap previous = map.remove(key);
			if (previous != null) {
				size -= sizeOf(key, previous);
			}
		}
	}

	@Override
	public Collection<String> keys() {
		synchronized (this) {
			return new HashSet<String>(map.keySet());
		}
	}

	@Override
	public void clear() {
		trimToSize(-1); // -1 will evict 0-sized elements
	}

	/**
	 * Returns the size {@code Bitmap} in bytes.
	 * <p/>
	 * An entry's size must not change while it is in the cache.
	 */
	private int sizeOf(String key, Bitmap value) {
		return value.getRowBytes() * value.getHeight();
	}

	@Override
	public synchronized final String toString() {
		return String.format("LruCache[maxSize=%d]", maxSize);
	}
}


ImageLoader学习笔记

标签:多线程   imageloader   

原文地址:http://blog.csdn.net/chencangui/article/details/45420237

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!