Android项目之ImageLoader
来源:互联网 发布:淘宝兼职需要身份证吗 编辑:程序博客网 时间:2024/06/11 12:24
ImageLoader为项目内统管图片资源的类。
项目中的图片分为:缓存图片、磁盘图片、网络图片三类;加载图片的顺序应该为:
先加载内存缓存图片,有则显示,无则加载磁盘缓存图片;磁盘缓存有则显示,无则另启线程下载;下载成功后显示,如果设置了保存则保存至内存和磁盘。
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
- Android项目之ImageLoader
- Android之ImageLoader(demo)
- Android开源项目 Universal imageloader 源码研究之项目框架
- 开源项目之------ImageLoader的使用
- Android universal-imageloader之DisplayImageOptions详细配置
- Android之加载网络图片—ImageLoader
- 【Android 基础】图片加载框架之ImageLoader
- android imageloader
- android imageloader
- (4.3.1.8)【项目一】子fragment之imageloader
- Android 第三方开源强大的图片下载项目ImageLoader的使用方法
- IntelliJ/Android Studio中项目引用ImageLoader资源库的方法
- 教你写Android ImageLoader框架之基本架构
- 教你写Android ImageLoader框架之基本架构
- android图片加载之imageloader的简单使用
- Android Image开源框架之ImageLoader(一)
- Android Image开源框架之ImageLoader(二)
- 初学Android之Tablayout+Fragment+ImageLoader(低仿知乎)
- 图结构练习——BFS——从起始点到目标点的最短步数
- webpack的commonchunkplugin深度解析以及chunk和module内部结构
- python3+PyQt5 自定义窗口部件--创建复合窗口部件
- 4. Redis与Java的使用
- Activity启动过程分析
- Android项目之ImageLoader
- 嵌入式linux 保存参数的方法
- c或c++实现webService
- Project Euler 011 Largest product in a grid
- 第五届蓝桥杯JavaA组第三题
- Problem D. gMatrix Google APAC 2016 University Test Round C
- 学习重要,还是经营人脉重要?
- 2.15
- Android项目之ImageLoader_内存缓存