第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

一個(gè)菜鳥(niǎo)的圖片三級(jí)緩存筆記

標(biāo)簽:
Android

先附上我的源码Github地址
https://github.com/xiaweizi/ImageCache.git

我的博客地址

图片的缓存虽然现在已经有不少的框架,但是我还是想自己学习一下图片的缓存机制。图片缓存的大概逻辑就是:当需要获取图片的时候,首先判断内存是否有,有---加载,无---从本地中获取图片,有--加载,无--从网络中下载,然后缓存到内存和本地中。大概流程图如下:

图片的三级缓存.png

首先从易到难

1. 内存缓存

1. 新建LruCache对象
private LruCache<String, Bitmap> mMemoryCache;
2. 在构造函数中初始化LruCache
        final long maxMemory = Runtime.getRuntime().maxMemory() / 8;
        mMemoryCache = new LruCache<String, Bitmap>((int) maxMemory){
            @Override
            protected int sizeOf (String key, Bitmap value) {
                int byteCount = value.getByteCount();
                Log.i(TAG, "每个Bitmap大小:" + byteCount + "每个程序最大内存" + maxMemory);
                return byteCount;
            }
        };
3. 利用键值对的方式存到内存中
    public void setBitmapToMemory(Bitmap bitmap, String url){
        Log.i(TAG, "图片已经保存到内存中: ");
        mMemoryCache.put(url, bitmap);
    }
4. 从内存中取出图片
    public Bitmap getBitmapFromMemory(String url){
        return mMemoryCache.get(url);
    }
2.本地缓存
1. 创建要缓存的路径
private static final String CACHE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath();
2. 利用输入流进行文件的本地存储,其中对文件名进行了MD5加密
    try {
        Log.i(TAG, "cachePath: " + CACHE_PATH);
        String filename = MD5Encoder.encode(url);
        File file = new File(CACHE_PATH, filename);

        File parentFile = file.getParentFile();
        if (!parentFile.exists()){
            parentFile.mkdirs();
        }
        Log.i(TAG, "图片存到本地中: ");
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(file));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.i(TAG, "setBitmapToLocal: " + e);
    }
3. 利用输出流进行文件的读取
    try {
        String fileName = MD5Encoder.encode(url);
        File file = new File(CACHE_PATH, fileName);

        Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(file));
        Log.i(TAG, "从本地获取图片: ");
        return bitmap;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return null;
3.从网络中获取图片
1. 利用构造函数获取LocalCacheUtil和MemoryCacheUtil对象
public NetCache (LocalCache mLocache, MemoryCache mMemoryCache) {
    this.mLocache = mLocache;
    this.mMemoryCache = mMemoryCache;
}
2. 新建私有类,从url中获取图片
        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) new URL(url).openConnection();
            conn.setConnectTimeout(4000);
            conn.setReadTimeout(4000);
            conn.setRequestMethod("GET");
            int responseCode = conn.getResponseCode();
            if (responseCode == 200){
                //图片压缩
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 2;//宽高压缩为原来的1/2
                options.inPreferredConfig = Bitmap.Config.ARGB_4444;
                Bitmap bitmap = BitmapFactory.decodeStream(conn.getInputStream(), null, options);
                return bitmap;
            }
        } catch (IOException e) {
            e.printStackTrace();
            Log.i(TAG, "downLoadBitmap: " + e);
        }finally {
            conn.disconnect();
        }
    return null;
    }
3. 利用AsyncTask的方式异步加载数据
class BitmapTask extends AsyncTask<Object, Integer, Bitmap>{

    @Override
    protected void onPreExecute () {
        Log.i(TAG, "onPreExecute: ");
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate (Integer... values) {
        Log.i(TAG, "onProgressUpdate: ");
        super.onProgressUpdate(values);
    }

    @Override
    protected Bitmap doInBackground (Object... params) {
        Log.i(TAG, "doInBackground: ");
        iv = (ImageView) params[0];
        url = (String) params[1];
        Bitmap bitmap = downLoadBitmap(url);
        return bitmap;
    }

    @Override
    protected void onPostExecute (Bitmap bitmap) {
        Log.i(TAG, "onPostExecute: ");
        if (bitmap != null){
            Log.i(TAG, "已经从网络中获取到图片: ");
            iv.setImageBitmap(bitmap);

            Log.i(TAG, "url: " + url + "Bitmap" + bitmap.getByteCount());
            mMemoryCache.setBitmapToMemory(bitmap, url);
            mLocache.setBitmapToLocal(bitmap, url);

        }
    }
4.最后创建一个util进行逻辑的封装,即一开始的流程图
public class MyBitmapUtil {

private static final String TAG = "MyBitmapUtil----->";
private LocalCache mLocal;
private MemoryCache mMemory;
private NetCache mNet;

public MyBitmapUtil () {
    mLocal = new LocalCache();
    mMemory = new MemoryCache();
    mNet = new NetCache(mLocal, mMemory);
}

public void disPlay(ImageView iv, String url){
    iv.setImageResource(R.mipmap.ic_launcher);
    Bitmap bitmap = null;

    bitmap = mMemory.getBitmapFromMemory(url);
    if (bitmap != null){
        iv.setImageBitmap(bitmap);
        return;
    }
    bitmap = mLocal.getBitmapFromLocal(url);
    if (bitmap != null){
        iv.setImageBitmap(bitmap);
        mMemory.setBitmapToMemory(bitmap, url);
        return;
    }

    mNet.getBitmapFromNet(iv, url);
}

}

最后附上我的源码Github地址
https://github.com/xiaweizi/ImageCache.git

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
移動(dòng)開(kāi)發(fā)工程師
手記
粉絲
13
獲贊與收藏
84

關(guān)注作者,訂閱最新文章

閱讀免費(fèi)教程

  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消