简介ImageLoader(二)

来源:互联网 发布:无法打开数据库msdb 编辑:程序博客网 时间:2024/05/16 00:50
displayImage:

添加显示图片的任务到执行池中,当轮到它执行的时候就会显示图片。

该方法的重载方法很多,其原型方法为:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public void displayImage(String uri, ImageAware imageAware, DisplayImageOptions options,  
  2.             ImageLoadingListener listener, ImageLoadingProgressListener progressListener)  

        1.我们必须填写的参数至少为这两个:uri ,imageAware(ImageView)。其他的参数,如果你不填写的话,会采用默认的设置,比如:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. if (listener == null) {  
  2.         listener = emptyListener;  
  3.     }  
而emptyListener,作为该类的成员属性在开始的时候就已经初始化了:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. private final ImageLoadingListener emptyListener = new SimpleImageLoadingListener();  

        2.检查图片路径:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. TextUtils.isEmpty(uri)  
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public static boolean isEmpty(CharSequence str) {  
  2.       if (str == null || str.length() == 0)  
  3.           return true;  
  4.       else  
  5.           return false;  
  6.   }  
当路径为空或者长度为0的时候,就会执行。


         3.内部缓存:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. Bitmap bmp = configuration.memoryCache.get(memoryCacheKey);  
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public interface MemoryCache extends MemoryCacheAware<String, Bitmap> {  
  2. }  

键值对通过地址,映射该位图。

         4.ImageLoadingInfo:任务的信息。

         5.ProcessAndDisplayImageTask:当前进行的线程任务。

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. @Override  
  2. public void run() {  
  3.     L.d(LOG_POSTPROCESS_IMAGE, imageLoadingInfo.memoryCacheKey);  
  4.   
  5.     BitmapProcessor processor = imageLoadingInfo.options.getPostProcessor();  
  6.     Bitmap processedBitmap = processor.process(bitmap);  
  7.     DisplayBitmapTask displayBitmapTask = new DisplayBitmapTask(processedBitmap, imageLoadingInfo, engine,  
  8.             LoadedFrom.MEMORY_CACHE);  
  9.     LoadAndDisplayImageTask.runTask(displayBitmapTask, imageLoadingInfo.options.isSyncLoading(), handler, engine);  
  10. }  

获取缓存文件:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. private Bitmap tryLoadBitmap() throws TaskCancelledException {  
  2.         Bitmap bitmap = null;  
  3.         try {  
  4.             File imageFile = configuration.diskCache.get(uri);  
  5.             if (imageFile != null && imageFile.exists()) {  
  6.                 L.d(LOG_LOAD_IMAGE_FROM_DISK_CACHE, memoryCacheKey);  
  7.                 loadedFrom = LoadedFrom.DISC_CACHE;  

多线程处理了位图,使用handler将消息发送给UI线程。

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. static void runTask(Runnable r, boolean sync, Handler handler, ImageLoaderEngine engine) {  
  2.     if (sync) {  
  3.         r.run();  
  4.     } else if (handler == null) {  
  5.         engine.fireCallback(r);  
  6.     } else {  
  7.         handler.post(r);  
  8.     }  
  9. }  
0 0
原创粉丝点击