简介ImageLoader(二)

来源:互联网 发布:防止sql注入方法 编辑:程序博客网 时间:2024/04/29 11:29

displayImage:

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

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

public void displayImage(String uri, ImageAware imageAware, DisplayImageOptions options,ImageLoadingListener listener, ImageLoadingProgressListener progressListener)

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

if (listener == null) {listener = emptyListener;}
而emptyListener,作为该类的成员属性在开始的时候就已经初始化了:

private final ImageLoadingListener emptyListener = new SimpleImageLoadingListener();

        2.检查图片路径:

TextUtils.isEmpty(uri)
  public static boolean isEmpty(CharSequence str) {        if (str == null || str.length() == 0)            return true;        else            return false;    }
当路径为空或者长度为0的时候,就会执行。


         3.内部缓存:

Bitmap bmp = configuration.memoryCache.get(memoryCacheKey);
public interface MemoryCache extends MemoryCacheAware<String, Bitmap> {}

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

         4.ImageLoadingInfo:任务的信息。

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

@Overridepublic void run() {L.d(LOG_POSTPROCESS_IMAGE, imageLoadingInfo.memoryCacheKey);BitmapProcessor processor = imageLoadingInfo.options.getPostProcessor();Bitmap processedBitmap = processor.process(bitmap);DisplayBitmapTask displayBitmapTask = new DisplayBitmapTask(processedBitmap, imageLoadingInfo, engine,LoadedFrom.MEMORY_CACHE);LoadAndDisplayImageTask.runTask(displayBitmapTask, imageLoadingInfo.options.isSyncLoading(), handler, engine);}

获取缓存文件:

private Bitmap tryLoadBitmap() throws TaskCancelledException {Bitmap bitmap = null;try {File imageFile = configuration.diskCache.get(uri);if (imageFile != null && imageFile.exists()) {L.d(LOG_LOAD_IMAGE_FROM_DISK_CACHE, memoryCacheKey);loadedFrom = LoadedFrom.DISC_CACHE;

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

static void runTask(Runnable r, boolean sync, Handler handler, ImageLoaderEngine engine) {if (sync) {r.run();} else if (handler == null) {engine.fireCallback(r);} else {handler.post(r);}}



        





1 0
原创粉丝点击