图片加载笔记

图片的三级缓存

android默认给每个应用只分配16M的内存,所以如果加载过多的图片,为了防止内存溢出,应该将图片缓存起来。图片的三级缓存分别是:

  • 1、内存缓存 优先加载,速度最快
  • 2、本地缓存 次优先加载 速度稍快
  • 3、网络缓存 最后加载 速度由网络速度决定(浪费流量)
缓存策略:

缓存策略是一个通用的思想,可以用在很多场景中,但实际开发中经常需要用Bitmap做缓存。

目前比较常用的缓存策略是LruCache和DiskLruCache,其中LruCache常被用做内存缓存,而DiskLruCache常被用做存储缓存(磁盘缓存)。Lru是最近最少使用算法。

Android开发艺术探索(别人记)

此算法的核心思想为:当缓存快满了,会淘汰近期最少使用的缓存目标。

LruCache是一个泛型类,它内部采用一个LinkedHashMap以强引用的方式存储外界的缓存对象,其提供了get和put方法来完成缓存的获取和添加操作。

强引用:直接的对象引用;

软引用:当一个对象只有软引用存在时,系统内存不足时此对象会被gc回收;

弱引用当一个对象只有弱引用存在时,此对象会随时被gc回收。

LruCached 的使用

LruCache<String, Bitmap> mMemory;
int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);//当前进程可用内存,单位为KB
int cacheSize = maxMemory / 8;//总容量大小
mMemory = new LruCache<String, Bitmap>(cacheSize){
    @Override
    protected int sizeOf(String key, Bitmap value) {
        return value.getRowBytes() * value.getHeight() / 1024;
    }
};

DiskLruCache的使用

郭霖讲解:点击打开链接


将缓存对象写入文件系统从而实现缓存的效果。

//1、获取缓存地址
public File getDiskCacheDir(Context context, String uniqueName) {
    String cachePath;
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable()) {
        cachePath = context.getExternalCacheDir().getPath();
    } else {
        cachePath = context.getCacheDir().getPath();
    }
    return new File(cachePath + File.separator + uniqueName);
}
//2、应用程序的版本号
public int getAppVersion(Context context) {
    try {
        PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        return info.versionCode;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return 1;
}
//3、DiskLruCache的创建

File cacheDir = getDiskCacheDir(getApplicationContext(), "bitmap");
if (!cacheDir.exists()) {
    cacheDir.mkdirs();
}
try {
    mDiskLruCache = DiskLruCache.open(cacheDir, getAppVersion(getApplicationContext()), 1, DISK_CACHE_SIZE);
} catch (IOException e) {
    e.printStackTrace();
}
//4、写入缓存
private boolean downloadUrlToStream(String urlString, OutputStream outputStream) {
    HttpURLConnection urlConnection = null;
    BufferedOutputStream out = null;
    BufferedInputStream in = null;
    try {
        final URL url = new URL(urlString);
        urlConnection = (HttpURLConnection) url.openConnection();
        in = new BufferedInputStream(urlConnection.getInputStream(), 8 * 1024);
        out = new BufferedOutputStream(outputStream, 8 * 1024);
        int b;
        while ((b = in.read()) != -1) {
            out.write(b);
        }
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
        try {
            if (out != null) {
                out.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return false;
}
//4、将字符串进行MD5编码
public String hashKeyForDisk(String key) {
    String cacheKey;
    try {
        final MessageDigest mDigest = MessageDigest.getInstance("MD5");
        mDigest.update(key.getBytes());
        cacheKey = bytesToHexString(mDigest.digest());
    } catch (NoSuchAlgorithmException e) {
        cacheKey = String.valueOf(key.hashCode());
    }
    return cacheKey;
}

private String bytesToHexString(byte[] bytes) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < bytes.length; i++) {
        String hex = Integer.toHexString(0xFF & bytes[i]);
        if (hex.length() == 1) {
            sb.append("0");
        }
        sb.append(hex);
    }
    return sb.toString();
}
//5、得到一个DiskLruCache.Editor的实例
new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            String imageUrl = "https://img-my.csdn.net/uploads/201309/01/1378037235_7476.jpg";
            String key = hashKeyForDisk(imageUrl);
            DiskLruCache.Editor editor = mDiskLruCache.edit(key);
            if (editor != null) {
                OutputStream outputStream = editor.newOutputStream(0);
                if (downloadUrlToStream(imageUrl, outputStream)) {
                    editor.commit();
                } else {
                    editor.abort();
                }
                mDiskLruCache.flush();//同步到日志文件(journal)最标准的做法是在onPause()方法中调用
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}).start();
//6、读取缓存
try {
    String imageUrl = "https://img-my.csdn.net/uploads/201309/01/1378037235_7476.jpg";
    String key = hashKeyForDisk(imageUrl);
    DiskLruCache.Snapshot snapshot = mDiskLruCache.get(key);
    if (snapshot != null) {
        InputStream is = snapshot.getInputStream(0);
        Bitmap bitmap = BitmapFactory.decodeStream(is);
        //mImage.setImageBitamp(bitmap);
    }
} catch (IOException e) {
    e.printStackTrace();
}
//7、移除缓存
try {
    String imageUrl = "https://img-my.csdn.net/uploads/201309/01/1378037235_7476.jpg";
    String key = hashKeyForDisk(imageUrl);
    mDiskLruCache.remove(key);
} catch (IOException e) {
    e.printStackTrace();
}


Bitmap的高效加载

Bitmap在Android中指的是一张图片。BitmapFactory类提供了四类方法:decodeFile、decodeResource、decodeStream和decodeByteArray,分别用于支持从文件系统、资源、输入流以及字节数组中加载出一个Bitmap对象,其中decodeFile、decodeResource又间接调用了decodeStream方法。
通过采样率即可有效地加载图片。
(1)将BitmapFactory.Option的injustDecodeBounds参数设为true并加载图片。
(2)从BitmapFactory.Option中取出图片的原始宽高信息,它对应于outWidth和outHeight参数。
(3)根据采样率的规则并结合目标View所需大小计算出采样率inSampleSize。
(4)将BitmapFactory.Option的injustDecodeBounds参数设为false,然后重新加载图片。







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值