Android项目之ImageLoader

来源:互联网 发布:淘宝兼职需要身份证吗 编辑:程序博客网 时间:2024/06/11 12:24

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 {        加载完成, 加载磁盘缓存中, 加载磁盘中, 加载网络中, 未开始, 加载取消, 加载失败;    }}
0 0
原创粉丝点击