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

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

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

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

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

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



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

            }
        });
    }

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

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

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

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