码迷,mamicode.com
首页 > 系统相关 > 详细

第12章 Bitmap的加载和Cache

时间:2016-03-01 22:16:49      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:

高效加载
BitmapFactory类提供四种方法:
decodeFile:从文件,间接调用decodeStream
decodeResource:从资源,间接调用decodeStream
decodeStream:输入流
decodeByteArray:字节数组中

使用BitmapFactory.options按一定采样率来加载缩小后图片来避免OOM。
  1. public class ImageResizer {
  2. private static final String TAG = "ImageResizer";
  3. public ImageResizer() {
  4. }
  5. public Bitmap decodeSampledBitmapFromResource(Resources res,
  6. int resId, int reqWidth, int reqHeight) {
  7. //1、 将BitmapFactory.Options的inJustDecodeBounds设置为true并加载图片
  8. final BitmapFactory.Options options = new BitmapFactory.Options();
  9. options.inJustDecodeBounds = true;
  10. BitmapFactory.decodeResource(res, resId, options);
  11. // 2、从BitmapFactory.Options取出图片的原始宽高信息
  12. options.inSampleSize = calculateInSampleSize(options, reqWidth,
  13. reqHeight);
  14. // 4、设置injustDecodBounds为false然后重新加载图片
  15. options.inJustDecodeBounds = false;
  16. return BitmapFactory.decodeResource(res, resId, options);
  17. }
  18. public int calculateInSampleSize(BitmapFactory.Options options,
  19. int reqWidth, int reqHeight) {
  20. if (reqWidth == 0 || reqHeight == 0) {
  21. return 1;
  22. }
  23. // Raw height and width of image
  24. final int height = options.outHeight;
  25. final int width = options.outWidth;
  26. Log.d(TAG, "origin, w= " + width + " h=" + height);
  27. int inSampleSize = 1;
  28. if (height > reqHeight || width > reqWidth) {
  29. final int halfHeight = height / 2;
  30. final int halfWidth = width / 2;
  31. // Calculate the largest inSampleSize value that is a power of 2 and
  32. // keeps both
  33. // height and width larger than the requested height and width.
  34. //3、结合采样率规则并结合View的所需大小计算采样率inSampleSize
  35. while ((halfHeight / inSampleSize) >= reqHeight
  36. && (halfWidth / inSampleSize) >= reqWidth) {
  37. inSampleSize *= 2;
  38. }
  39. }
  40. Log.d(TAG, "sampleSize:" + inSampleSize);
  41. return inSampleSize;
  42. }
  43. }

缓存策略
LruCache(内存缓存)、DiskLruCache(存储缓存)。Lru即Least Recently Used。
核心思想为:当缓存快满时,会淘汰近期最少使用的缓存目标。

LruCache
泛型类,内部采用一个LinkedHashMap以强引用方式存储外界的缓存对象,其提供get和put方法来完成缓存的获取和添加操作;
强引用:直接对象引用;
软引用:当系统内存不足此对象会被gc回收;
弱引用:此对象会随时被gc回收         
初始化过程:
  1. int maxMemory = (int)(Runtime.getRuntime().maxMemory()/1024);
  2. int cacheSize = maxMemory/8;
  3. mMemory = new LruCache<String,Bitmap>(cacheSize){
  4. @override
  5. protected int sizeOf(String key,Bitmap bitmap){
  6. return bitmap.getRowBytes().bitmap.getHeight()/1024;
  7. }
  8. }

DiskLruCache
不属于Android SDK的一部分,但得到了Android官方文档的推荐。
缓存创建、添加、查找、remove、delete。
创建:
  1. private static final long DISK_CACHE_SIZE = 1024*1024*50;//50MB
  2. File diskCacheDir = getDiskCacheDir(mContext,"bitmap");
  3. if(!diskCacheDir.exits()){
  4. diskCacheDir.mkdirs();
  5. }
  6. mDiskLruCache = DisLruCache.open(diskCacheDir,1,1,DISK_CACHE_SIZE);//存储路径,可以放在SD卡的存储目录,也可以选择SD卡的其他目录
  7. //版本号,一般为1
  8. //单个节点对应数据的个数,一般1
  9. //缓存的总大小
添加:
步骤:以图片缓存为例子,获取图片的url对应的key(采用url的md5值作为key),然后根据key就可以通过edit()来获取Edit对象来得到一个文件输出流。
            把文件输出流写入到文件系统上,最后通过Editor的commit()来提交写入操作。
将url转换对应的key
  1. //将url转换成key
  2. private String hashKeyFormUrl(String url){
  3. String cacheKey;
  4. try{
  5. final MessageDigest mDigest = MessageDigest.getInstance("MD5");
  6. mDigest.update(url.getBytes());
  7. cacheKey = bytesToHexString(mDigest.digest());
  8. }catch(NoSuchAlgorithmException e){
  9. cacheKey = String.valueOf(url.hashCode());
  10. }
  11. return cacheKey;
  12. }
  13. private String bytesToHexString(byte[] bytes){
  14. StringBuilder sb = new StringBuilder();
  15. for(int i = 0;i<byte.length;i++){
  16. String hex = Integer.toHexString(0xFF & bytes[i]);
  17. if(hex.length()==1){
  18. sb.append(‘0‘);
  19. }
  20. sb.append(hex);
  21. }
  22. return sb.toString();
  23. }
获取文件输出流
  1. String key = hashKeyFormUrl(url);
  2. DiskLruCache.Editor editor = mDiskLruCache.edit(key);
  3. if(edit !=null){
  4. OutputStream outputStream = editor.newOutputStream(DISK_CACHE_INDEX)
  5. }

查找:
将url转换成key,然后通过DiskLruCache的get方法得到一个SnapShot对象,接着再通过Snapshot对象即可得到缓存的文件输入流。

ImageLoader
同步加载;
异步加载;线程池解决并发性问题。
图片压缩;
内存缓存;
磁盘缓存;
网络拉取;
listview或gridView错位问题;在设置图片前检查url是否改变。

优化列表卡顿现象:
1、getView里面通过ImageLoader异步去加载图片;
2、listview滑动的时候不加载,停止的时候去加载,在OnScrollListener的onScrollStateChanged方法中判读是否处于滑动状态;
3、开启所在Activity的硬件加速;












第12章 Bitmap的加载和Cache

标签:

原文地址:http://www.cnblogs.com/fruitbolgs/p/5232414.html

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