图片处理

2015-11-03  本文已影响0人  我欲举头望明月

图片优化

使用BitmapFactory.Options优化


使用LruCache缓存图片

lru全称为Least Recently Used,最近最少使用。
该类指定一个最大内存,如果其中保存的对象超过最大内存,则移除最近使用的对象。
该类是线程安全的,在重写该类的方法时,为了保证线程安全,需要做一些同步工作。该类,内部使用的同步对象为this。

扩展LruCache
public LruCache (int maxSize)
// 不需要重写。
// Runtime.getRuntime().maxMemory()方法获取的是可用内存的值
// maxSize参数的值可以根据实际情况参考上面的值来计算

protected V create (K key)
// 创建key对应的对象
protected void entryRemoved (boolean evicted, K key, V oldValue, V newValue)
// 当key对应的对象被移除时,掉哟个该方法。
// 此时应该释放所占用的内存。
protected int sizeOf (K key, V value)
// 返回指定对象所占用的内存空间

DiskLruCache

public static DiskLruCache open(File directory, int appVersion, int valueCount, long maxSize)
由于DiskLruCache的构造方法是私有的,所以必须调用该方法来获取实例。

public Editor edit(String key)
调用该方法写入缓存。基本流程为:

public synchronized Snapshot get(String key)
调用该方法得到缓存。

其他方法
public static String readFully(Reader reader)
public static String readAsciiLine(InputStream in)
public static void closeQuietly(Closeable closeable)
public static void deleteContents(File dir) 


public File getDirectory()
public synchronized long size() 
// 返回当前大小
public long maxSize()
public synchronized boolean remove(String key) 
public void delete() 
// 关闭,并删除该目录下的所有文件
public synchronized void close()
public boolean isClosed() 
public synchronized void flush()
// 把操作同步到日志文件中

// Snapshot类
public Editor edit()
public InputStream getInputStream(int index)
public String getString(int index)
public void close() 

// Editor类
public InputStream newInputStream(int index)
public OutputStream newOutputStream(int index)
public String getString(int index)
public void set(int index, String value) 
public void commit() 
public void abort()

参考
http://blog.csdn.net/guolin_blog/article/details/28863651
http://blog.csdn.net/guolin_blog/article/details/34093441
http://blog.csdn.net/guolin_blog/article/details/28863651

上一篇 下一篇

猜你喜欢

热点阅读