/** * Load an image specified by the data parameter into an ImageView (override {@link * ImageWorker#processBitmap(Object)} to define the processing logic). A memory and disk cache * will be used if an {@link ImagesCache} has been added using {@link * ImageWorker#addImageCache(FragmentManager, ImagesCache.ImageCacheParams)}. If the image is * found in the memory cache, it is set immediately, otherwise an {@link AsyncTask} will be * created to asynchronously load the bitmap. * * @param data The URL of the image to download. * @param imageView The ImageView to bind the downloaded image to. */ public void loadImage(Object data, ImageViewWithProcessIndicator imageView) { if (data == null) { return; } BitmapDrawable value = null; if (mImageCache != null) { value = mImageCache.getBitmapFromMemCache(String.valueOf(data)); } if (value != null) { // Bitmap found in memory cache imageView.getImageView().setImageDrawable(value); imageView.showImage(); } else if (cancelPotentialWork(data, imageView)) { final BitmapWorkerTask task = new BitmapWorkerTask(imageView); final AsyncDrawable asyncDrawable = new AsyncDrawable(mResources, mLoadingBitmap, task); imageView.getImageView().setImageDrawable(asyncDrawable); imageView.showAsLoading(); // NOTE: This uses a custom version of AsyncTask that has been pulled from the // framework and slightly modified. Refer to the docs at the top of the class // for more info on what was changed. task.executeOnExecutor(AsyncTask.DUAL_THREAD_EXECUTOR, data); } }
/** * Called when the processing is complete and the final drawable should be set on the ImageView. * * @param imageView * @param drawable */ private void setImageDrawable(ImageViewWithProcessIndicator imageView, Drawable drawable) { if (mFadeInBitmap) { imageView.showImage(); // Transition drawable with a transparent drawable and the final drawable final TransitionDrawable td = new TransitionDrawable( new Drawable[] {new ColorDrawable(android.R.color.transparent), drawable}); // Set background to loading bitmap imageView.setBackgroundDrawable(new BitmapDrawable(mResources, mLoadingBitmap)); imageView.getImageView().setImageDrawable(td); td.startTransition(FADE_IN_TIME); } else { imageView.getImageView().setImageDrawable(drawable); } imageView.onAsyncImageSet(); }
/** * @param imageView Any imageView * @return Retrieve the currently active work task (if any) associated with this imageView. null * if there is no such task. */ private static BitmapWorkerTask getBitmapWorkerTask(ImageViewWithProcessIndicator imageView) { if (imageView != null) { final Drawable drawable = imageView.getImageView().getDrawable(); if (drawable instanceof AsyncDrawable) { final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable; return asyncDrawable.getBitmapWorkerTask(); } } return null; }