单一职责之ImageLoader(四)增加sd卡缓存

前端之家收集整理的这篇文章主要介绍了单一职责之ImageLoader(四)增加sd卡缓存前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
public class ImageCache {
    //图片缓存
    LruCache<String,Bitmap> mImageCache;
    public ImageCache(){

    }
    private void initImageCache(){
        //计算可用的最大内存
        final int maxMemory = (int)(Runtime.getRuntime().maxMemory()/1024);
        //取四分之一内存作为缓存
        final int cacheSize = maxMemory/4;
        mImageCache = new LruCache<String,Bitmap>(cacheSize){
            @Override
            protected int sizeOf(String key,Bitmap value) {
                return value.getRowBytes()*value.getHeight()/1024;
            }
        };
    }
    public void put(String url,Bitmap bitmap){
        mImageCache.put(url,bitmap);
    }

    public Bitmap get(String url){
        return mImageCache.get(url);
    }
}
public class DiskCache {
    static String cacheDir = "sdcard/cache/";
    //从缓存中获取图片
    public Bitmap get(String url){
        return BitmapFactory.decodeFile(cacheDir+url);
    }
    //将图片缓存到内存中
    public void put(String url,Bitmap bmp){
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(cacheDir + url);
            bmp.compress(Bitmap.CompressFormat.PNG,100,fileOutputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
public class ImageLoader {
    //图片内存缓存
    ImageCache mImageCache = new ImageCache();
    //图片sd卡缓存
    DiskCache mDiskCache = new DiskCache();
    //是否使用sd卡缓存
    boolean isUseDiskCache = false;
    //线程池 线程数量cpu数量

    //线程池
    ExecutorService mExecutorService = Executors.newFixedThreadPool(Runtime.getRuntime().
            availableProcessors());



    //显示网络加载的图片 调用一次都会开启线程去下载 并且显示
    public void displayImage(final String url,final ImageView imageView){
        //判断使用哪种缓存
        Bitmap bitmap = isUseDiskCache?mDiskCache.get(url):mImageCache.get(url);
        if(bitmap != null){
            imageView.setImageBitmap(bitmap);
            return;
        }
        //没有缓存则给线程池下载
        imageView.setTag(url);
        mExecutorService.submit(new Runnable() {
            @Override
            public void run() {
                Bitmap bitmap = downloadImage(url);
                if(bitmap == null){
                    return ;
                }
                if(imageView.getTag().equals(url)){
                    imageView.setImageBitmap(bitmap);
                }
                //使用内存缓存 或者sd卡缓存
                if(isUseDiskCache){
                    mDiskCache.put(url,bitmap);
                }else{
                    mImageCache.put(url,bitmap);
                }

            }
        });
    }

    public Bitmap downloadImage(String imageUrl){
        Bitmap bitmap = null;
        try {
            URL url = new URL(imageUrl);
            final HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            bitmap = BitmapFactory.decodeStream(conn.getInputStream());
            conn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }
}
e

缺陷sd卡缓存和内存缓存只能选择其中一个,应该设计成双缓冲策略。

原文链接:https://www.f2er.com/javaschema/283659.html

猜你在找的设计模式相关文章