/** * Once the image is processed, associates it to the imageView */ @Override protected void onPostExecute(Bitmap bitmap) { if (progressBar != null) { progressBar.setVisibility(View.GONE); } // if cancel was called on this task or the "exit early" flag is set then we're done if (isCancelled()) { bitmap = null; } final ImageView imageView = getAttachedImageView(); if (bitmap != null && imageView != null) { if (BuildConfig.DEBUG) { Log.d(TAG, "onPostExecute - setting bitmap"); } if (textView != null) { textView.setVisibility(View.GONE); } imageView.setVisibility(View.VISIBLE); setImageBitmap(imageView, bitmap); } else if (imageView != null && textView != null) { imageView.setVisibility(View.GONE); textView.setVisibility(View.VISIBLE); } }
while (mPauseWork && !isCancelled()) { try { mPauseWorkLock.wait(); if (!isCancelled() && getAttachedImageView() != null) { bitmap = processBitmap(params[0]);
public void loadImage(Object data, ImageView imageView, ProgressBar progressBar, TextView textView) { if (data == null) { imageView.setImageBitmap(mLoadingBitmap); return; } Bitmap bitmap = null; if (mImageCache != null) { bitmap = mImageCache.getBitmapFromMemCache(String.valueOf(data)); } if (bitmap != null) { // Bitmap found in memory cache imageView.setImageBitmap(bitmap); if (progressBar != null) { progressBar.setVisibility(View.GONE); } } else if (cancelPotentialWork(data, imageView)) { final BitmapWorkerTask task = new BitmapWorkerTask(imageView, progressBar, textView); final AsyncDrawable asyncDrawable = new AsyncDrawable(mResources, mLoadingBitmap, task); imageView.setImageDrawable(asyncDrawable); try { task.execute(data); } catch (Exception ex) { Log.e(TAG, "Exception while processing images: " + ex.getMessage()); } } }
/** * Returns true if the current work has been canceled or if there was no work in * progress on this image view. * Returns false if the work in progress deals with the same data. The work is not * stopped in that case. */ public static boolean cancelPotentialWork(Object data, ImageView imageView) { final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); if (bitmapWorkerTask != null) { final Object bitmapData = bitmapWorkerTask.data; if (bitmapData == null || !bitmapData.equals(data)) { bitmapWorkerTask.cancel(true); if (BuildConfig.DEBUG) { Log.d(TAG, "cancelPotentialWork - cancelled work for " + data); } } else { // The same work is already in progress. return false; } } return true; }
@Override protected void onPreExecute() { super.onPreExecute(); if (progressBar != null) { progressBar.setVisibility(View.VISIBLE); } final ImageView imageView = getAttachedImageView(); if (imageView != null && textView != null) { textView.setVisibility(View.VISIBLE); imageView.setVisibility(View.GONE); } }
/** * Cancels any pending work attached to the provided ImageView. */ public static void cancelWork(ImageView imageView) { final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); if (bitmapWorkerTask != null) { bitmapWorkerTask.cancel(true); if (BuildConfig.DEBUG) { final Object bitmapData = bitmapWorkerTask.data; Log.d(TAG, "cancelWork - cancelled work for " + bitmapData); } } }