第12章 Bitmap的加载和Cache

高效加载
BitmapFactory类提供四种方法:
decodeFile:从文件,间接调用decodeStream
decodeResource:从资源,间接调用decodeStream
decodeStream:输入流
decodeByteArray:字节数组中
 
使用BitmapFactory.options按一定采样率来加载缩小后图片来避免OOM。
publicclassImageResizer{
  privatestaticfinalString TAG ="ImageResizer";
  publicImageResizer(){
  }
  publicBitmap decodeSampledBitmapFromResource(Resources res,
    int resId,int reqWidth,int reqHeight){
    //1、 将BitmapFactory.Options的inJustDecodeBounds设置为true并加载图片
    finalBitmapFactory.Options options =newBitmapFactory.Options();
    options.inJustDecodeBounds =true;
    BitmapFactory.decodeResource(res, resId, options);
    // 2、从BitmapFactory.Options取出图片的原始宽高信息
    options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);
    // 4、设置injustDecodBounds为false然后重新加载图片
    options.inJustDecodeBounds =false;
    returnBitmapFactory.decodeResource(res, resId, options);
  }
  publicint calculateInSampleSize(BitmapFactory.Options options,
    int reqWidth,int reqHeight){
    if(reqWidth ==0|| reqHeight ==0){
      return1;
    }
    // Raw height and width of image
    finalint height = options.outHeight;
    finalint width = options.outWidth;
    Log.d(TAG,"origin, w= "+ width +" h="+ height);
    int inSampleSize =1;
    if(height > reqHeight || width > reqWidth){
    finalint halfHeight = height /2;
    finalint halfWidth = width /2;
    // Calculate the largest inSampleSize value that is a power of 2 and
    // keeps both
    // height and width larger than the requested height and width.
    //3、结合采样率规则并结合View的所需大小计算采样率inSampleSize
    while((halfHeight / inSampleSize)>= reqHeight&&(halfWidth / inSampleSize)>= reqWidth){
        inSampleSize *=2;
      }
    }
    Log.d(TAG,"sampleSize:"+ inSampleSize);
    return inSampleSize;
  }
}

 

缓存策略

LruCache(内存缓存)、DiskLruCache(存储缓存)。Lru即Least Recently Used。
核心思想为:当缓存快满时,会淘汰近期最少使用的缓存目标。
 
LruCache
泛型类,内部采用一个LinkedHashMap以强引用方式存储外界的缓存对象,其提供get和put方法来完成缓存的获取和添加操作;
强引用:直接对象引用;
软引用:当系统内存不足此对象会被gc回收;
弱引用:此对象会随时被gc回收         
初始化过程:
int maxMemory =(int)(Runtime.getRuntime().maxMemory()/1024);
int cacheSize = maxMemory/8;
mMemory =newLruCache<String,Bitmap>(cacheSize){
@override
protectedint sizeOf(String key,Bitmap bitmap){
    return bitmap.getRowBytes().bitmap.getHeight()/1024;
  }
}
DiskLruCache
不属于Android SDK的一部分,但得到了Android官方文档的推荐。
缓存创建、添加、查找、remove、delete。
创建:
privatestaticfinallong DISK_CACHE_SIZE =1024*1024*50;//50MB
File diskCacheDir = getDiskCacheDir(mContext,"bitmap");
if(!diskCacheDir.exits()){
diskCacheDir.mkdirs();
}
mDiskLruCache =DisLruCache.open(diskCacheDir,1,1,DISK_CACHE_SIZE);//存储路径,可以放在SD卡的存储目录,也可以选择SD卡的其他目录
//版本号,一般为1
//单个节点对应数据的个数,一般1
//缓存的总大小
添加:
步骤:以图片缓存为例子,获取图片的url对应的key(采用url的md5值作为key),然后根据key就可以通过edit()来获取Edit对象来得到一个文件输出流。
            把文件输出流写入到文件系统上,最后通过Editor的commit()来提交写入操作。
 
将url转换对应的key
//将url转换成key
privateString hashKeyFormUrl(String url){
String cacheKey;
try{
  finalMessageDigest mDigest =MessageDigest.getInstance("MD5");
  mDigest.update(url.getBytes());
  cacheKey = bytesToHexString(mDigest.digest());
  }catch(NoSuchAlgorithmException e){
    cacheKey =String.valueOf(url.hashCode());
  }
  return cacheKey;
}
privateString bytesToHexString(byte[] bytes){
StringBuilder sb =newStringBuilder();
for(int i =0;i<byte.length;i++){
  String hex =Integer.toHexString(0xFF& bytes[i]);
  if(hex.length()==1){
  sb.append('0');
  }
  sb.append(hex);
}
return sb.toString();
}
获取文件输出流
String key = hashKeyFormUrl(url);
DiskLruCache.Editor editor = mDiskLruCache.edit(key);
if(edit !=null){
  OutputStream outputStream = editor.newOutputStream(DISK_CACHE_INDEX)
}
查找:
将url转换成key,然后通过DiskLruCache的get方法得到一个SnapShot对象,接着再通过Snapshot对象即可得到缓存的文件输入流。
 
ImageLoader
同步加载;
异步加载;线程池解决并发性问题。
图片压缩;
内存缓存;
磁盘缓存;
网络拉取;
listview或gridView错位问题;在设置图片前检查url是否改变。
 
优化列表卡顿现象:
1、getView里面通过ImageLoader异步去加载图片;
2、listview滑动的时候不加载,停止的时候去加载,在OnScrollListener的onScrollStateChanged方法中判读是否处于滑动状态;
3、开启所在Activity的硬件加速;
 
 

 

posted @ 2016-03-01 20:29  咖啡馆的水果拼盘  阅读(180)  评论(0编辑  收藏  举报