zhangzengfei
2022-01-10 4496b59ab27d569df1da7ef634e02273b3a9618a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.basic.security.utils.bitmap;
 
import android.graphics.Bitmap;
import android.widget.ImageView;
 
//import com.itheima.zhbj52.R;
 
/**
 * 自定义图片加载工具
 *
 * @author Kevin
 */
public class MyBitmapUtils {
 
    NetCacheUtils mNetCacheUtils;
    LocalCacheUtils mLocalCacheUtils;
    MemoryCacheUtils mMemoryCacheUtils;
 
    public MyBitmapUtils() {
        mMemoryCacheUtils = new MemoryCacheUtils();
        mLocalCacheUtils = new LocalCacheUtils();
        mNetCacheUtils = new NetCacheUtils(mLocalCacheUtils, mMemoryCacheUtils);
    }
 
    public void display(ImageView ivPic, String url) {
        //ivPic.setImageResource(R.drawable.news_pic_default);// 设置默认加载图片
 
        Bitmap bitmap = null;
        // 从内存读
        bitmap = mMemoryCacheUtils.getBitmapFromMemory(url);
        if (bitmap != null) {
            ivPic.setImageBitmap(bitmap);
            return;
        }
 
        // 从本地读
        bitmap = mLocalCacheUtils.getBitmapFromLocal(url);
        if (bitmap != null) {
            ivPic.setImageBitmap(bitmap);
            mMemoryCacheUtils.setBitmapToMemory(url, bitmap);// 将图片保存在内存
            return;
        }
 
        // 从网络读
        mNetCacheUtils.getBitmapFromNet(ivPic, url);
    }
 
}