Android项目之ImageLoader

本文介绍了一个项目内的图片资源管理类ImageLoader的工作原理及流程。详细解释了如何按顺序加载缓存图片、磁盘图片及网络图片,并通过具体代码展示了不同加载状态的处理过程。

ImageLoader为项目内统管图片资源的类。
项目中的图片分为:缓存图片、磁盘图片、网络图片三类;加载图片的顺序应该为:

Created with Raphaël 2.1.0加载图片取内存内存存在显示图片取磁盘磁盘存在下载是否保存存内存存磁盘yesnoyesnoyesno

先加载内存缓存图片,有则显示,无则加载磁盘缓存图片;磁盘缓存有则显示,无则另启线程下载;下载成功后显示,如果设置了保存则保存至内存和磁盘。

public class ImageLoadTask {

    private ImageLoadOptions imageLoadOptions;
    private ImageLoadListener imageLoadListener;

    /** 内存缓存 */
    private BitmapMemoryCache bitmapMemoryCache;
    /** 磁盘缓存 */
    private DiskCacheImageLoader diskCacheManager;

    /** 磁盘图片加载器 */
    private DiskImageLoader diskBitmapLoader;

    /** 图片下载器 */
    private ImageDownloader imageDownloader;

    /**
     * 当前的加载状态
     */
    private LoadState loadState = LoadState.未开始;
    private DiskImageLoader.LoadTask loadTask;
    private ImageDownloader.ImageDownLoadTask imageDownLoadTask;

    public ImageLoadTask(ImageLoadOptions imageLoadOptions, ImageLoadListener imageLoadListener,
                         BitmapMemoryCache bitmapMemoryCache, DiskCacheImageLoader diskCacheManager,
                         DiskImageLoader diskBitmapLoader, ImageDownloader imageDownloader) {
        this.imageLoadOptions = imageLoadOptions;
        this.imageLoadListener = imageLoadListener;
        this.bitmapMemoryCache = bitmapMemoryCache;
        this.diskCacheManager = diskCacheManager;
        this.diskBitmapLoader = diskBitmapLoader;
        this.imageDownloader = imageDownloader;
        loadState = LoadState.未开始;
    }

    /**
     * 开始加载
     */
    void start() {
        if (loadState != LoadState.未开始) {
            throw new IllegalStateException("该方法只能调用一次");
        }


        if (imageLoadOptions.isMemoryCacheEnable()) {
            Bitmap memoryBitmap = bitmapMemoryCache.get(imageLoadOptions.getKey());
            if (memoryBitmap != null) {
                imageLoadListener.onLoadSuccessful(ImageLoadListener.BitmapSource.MEMORY_CACHE, memoryBitmap);
                loadState = LoadState.加载完成;
                return;
            }
        }

        if (imageLoadOptions.isDiskCacheEnable()) {
            loadState = LoadState.加载磁盘缓存中;
            diskCacheManager.getCache(imageLoadOptions.getKey(), new DiskCacheImageLoader.OnGetDiskCacheListener() {
                @Override
                public void onGetDiskCache(String key, Bitmap bitmap) {
                    if (loadState == LoadState.加载取消) {
                        return;
                    }

                    if (bitmap != null) {
                        if (imageLoadOptions.isMemoryCacheEnable()) {
                            bitmapMemoryCache.put(imageLoadOptions.getKey(), bitmap);
                        }
                        imageLoadListener.onLoadSuccessful(ImageLoadListener.BitmapSource.DISK_CACHE, bitmap);
                        loadState = LoadState.加载完成;
                    } else {
                        loadSource();
                    }
                }
            });
        } else {
            loadSource();
        }

    }

    private void loadSource() {
        if (imageLoadOptions.hasUrl() && imageDownloader.isDownloading(imageLoadOptions.getFilePath())) {
            loadBitmapFromNet();
        } else {
            loadBitmapFromDisk(false);
        }
    }

    private void loadBitmapFromDisk(final boolean ifFromNet) {
        loadState = LoadState.加载磁盘中;
        loadTask = diskBitmapLoader.loadBitmap(imageLoadOptions, new DiskImageLoader.OnLoadResultListener() {
            @Override
            public void onLoadResult(int result, Bitmap bitmap) {
                if (loadState == LoadState.加载取消)
                    return;

                loadTask = null;
                if (result == RESULT_LOAD_SUCCESSFUL) {
                    String key = imageLoadOptions.getKey();
                    if (imageLoadOptions.isDiskCacheEnable()) {
                        diskCacheManager.putCache(key, bitmap);
                    }

                    if (imageLoadOptions.isMemoryCacheEnable()) {
                        bitmapMemoryCache.put(key, bitmap);
                    }
                    if (ifFromNet)
                        imageLoadListener.onLoadSuccessful(ImageLoadListener.BitmapSource.NETWORK, bitmap);
                    else
                        imageLoadListener.onLoadSuccessful(ImageLoadListener.BitmapSource.FILE, bitmap);
                    loadState = LoadState.加载完成;
                } else if (result == RESULT_FILE_NOT_EXISTS && imageLoadOptions.hasUrl()
                        && !ifFromNet) {
                    // 加载网络图片
                    loadBitmapFromNet();
                } else {
                    imageLoadListener.onLoadFailed("本地图片文件加载失败!");
                    loadState = LoadState.加载失败;
                }
            }
        });
    }

    private void loadBitmapFromNet() {
        loadState = LoadState.加载网络中;
        // 开始网络加载
        ImageDownloadListener imageDownloadListener = new ImageDownloadListener() {
            @Override
            public void onProgressChange(String filePath, int totalSize, int currentSize) {
                if (loadState == LoadState.加载取消)
                    return;

                imageLoadListener.onLoadProgressChange(totalSize, currentSize);
            }

            @Override
            public void onDownloadFinish(String filePath, DownloadResult downloadResult) {
                if (loadState == LoadState.加载取消)
                    return;

                imageDownLoadTask = null;
                if (downloadResult == DownloadResult.SUCCESSFUL || downloadResult == DownloadResult.FILE_EXISTS) {
                    loadBitmapFromDisk(true);
                } else {
                    imageLoadListener.onLoadFailed("网络图片加载失败");
                    loadState = LoadState.加载失败;
                }
            }
        };

        String filePath = imageLoadOptions.getFilePath();
        String url = imageLoadOptions.getUrl();
        imageDownLoadTask = imageDownloader.download(filePath, url, imageDownloadListener);
        imageLoadListener.onStartDownload(); // 通知, 开始下载了
    }

    public void cancle() {
        if (loadState != LoadState.加载取消 && loadState != LoadState.加载完成 && loadState != LoadState.加载失败) {
            if (loadState == LoadState.加载磁盘中) {
                loadTask.cancel();
                loadTask = null;
            } else if (loadState == LoadState.加载网络中) {
                imageDownLoadTask.cancel();
                imageDownLoadTask = null;
            }
            loadState = LoadState.加载取消;
        }
    }

    /**
     * 获取加载状态
     */
    public LoadState getLoadState() {
        return loadState;
    }

    /** 当前状态 汉字枚举,我喜欢。。。。 */
    public enum LoadState {
        加载完成, 加载磁盘缓存中, 加载磁盘中, 加载网络中, 未开始, 加载取消, 加载失败;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值