Code example for ImageView

Methods: getDrawablesetImageBitmapsetImageDrawable, setBackgroundDrawable

0
         * @param imageView The {@link ImageView} to use. 
         * @param imageType The type of image URL to fetch for. 
         */ 
        @SuppressWarnings("deprecation") 
        public BitmapWorkerTask(final ImageView imageView, final ImageType imageType) {
            imageView.setBackgroundDrawable(mDefaultArtwork);
            mImageReference = new WeakReference<ImageView>(imageView);
            mImageType = imageType;
        } 
 
        /** 
         * {@inheritDoc} 
         */ 
        @Override 
        protected TransitionDrawable doInBackground(final String... params) {
            // Define the key 
            mKey = params[0];
 
            // The result 
            Bitmap bitmap = null;
 
            // Wait here if work is paused and the task is not cancelled. This 
            // shouldn't even occur because this isn't executing while the user 
            // is scrolling, but just in case. 
            while (isScrolling() && !isCancelled()) {
                cancel(true);
            } 
 
            // First, check the disk cache for the image 
            if (mKey != null && mImageCache != null && !isCancelled()
                    && getAttachedImageView() != null) { 
                bitmap = mImageCache.getCachedBitmap(mKey);
            } 
 
            // Define the album id now 
            mAlbumId = params[3];
 
            // Second, if we're fetching artwork, check the device for the image 
            if (bitmap == null && mImageType.equals(ImageType.ALBUM) && mAlbumId != null
                    && mKey != null && !isCancelled() && getAttachedImageView() != null
                    && mImageCache != null) {
                bitmap = mImageCache.getCachedArtwork(mContext, mKey, mAlbumId);
            } 
 
            // Third, by now we need to download the image 
            if (bitmap == null && ApolloUtils.isOnline(mContext) && !isCancelled()
                    && getAttachedImageView() != null) { 
                // Now define what the artist name, album name, and url are. 
                mArtistName = params[1];
                mAlbumName = params[2];
                mUrl = processImageUrl(mArtistName, mAlbumName, mImageType);
                if (mUrl != null) {
                    bitmap = processBitmap(mUrl);
                } 
            } 
 
            // Fourth, add the new image to the cache 
            if (bitmap != null && mKey != null && mImageCache != null) {
                addBitmapToCache(mKey, bitmap);
            } 
 
            // Add the second layer to the transiation drawable 
            if (bitmap != null) {
                final BitmapDrawable layerTwo = new BitmapDrawable(mResources, bitmap);
                layerTwo.setFilterBitmap(false);
                layerTwo.setDither(false);
                mArrayDrawable[1] = layerTwo;
 
                // Finally, return the image 
                final TransitionDrawable result = new TransitionDrawable(mArrayDrawable);
                result.setCrossFadeEnabled(true);
                result.startTransition(FADE_IN_TIME);
                return result;
            } 
            return null; 
        } 
 
        /** 
         * {@inheritDoc} 
         */ 
        @Override 
        protected void onPostExecute(TransitionDrawable result) {
            if (isCancelled()) {
                result = null;
            } 
            final ImageView imageView = getAttachedImageView();
            if (result != null && imageView != null) {
                imageView.setImageDrawable(result);
            } 
        } 
 
        /** 
         * @return The {@link ImageView} associated with this task as long as 
         *         the ImageView's task still points to this task as well. 
         *         Returns null otherwise. 
         */ 
        private final ImageView getAttachedImageView() {
            final ImageView imageView = mImageReference.get();
            final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
            if (this == bitmapWorkerTask) {
                return imageView;
            } 
            return null; 
        } 
    } 
 
    /** 
     * Calls {@code cancel()} in the worker task 
     *  
     * @param imageView the {@link ImageView} to use 
     */ 
    public static final void cancelWork(final ImageView imageView) {
        final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
        if (bitmapWorkerTask != null) {
            bitmapWorkerTask.cancel(true);
        } 
    } 
 
    /** 
     * 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 final boolean executePotentialWork(final Object data, final ImageView imageView) {
        final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
        if (bitmapWorkerTask != null) {
            final Object bitmapData = bitmapWorkerTask.mKey;
            if (bitmapData == null || !bitmapData.equals(data)) {
                bitmapWorkerTask.cancel(true);
            } else { 
                // The same work is already in progress 
                return false; 
            } 
        } 
        return true; 
    } 
 
    /** 
     * Used to determine if the current image drawable has an instance of 
     * {@link BitmapWorkerTask} 
     *  
     * @param imageView Any {@link ImageView}. 
     * @return Retrieve the currently active work task (if any) associated with 
     *         this {@link ImageView}. null if there is no such task. 
     */ 
    private static final BitmapWorkerTask getBitmapWorkerTask(final ImageView imageView) {
        if (imageView != null) {
            final Drawable drawable = imageView.getDrawable();
            if (drawable instanceof AsyncDrawable) {
                final AsyncDrawable asyncDrawable = (AsyncDrawable)drawable;
                return asyncDrawable.getBitmapWorkerTask();
            } 
        } 
        return null; 
    } 
 
    /** 
     * A custom {@link BitmapDrawable} that will be attached to the 
     * {@link ImageView} while the work is in progress. Contains a reference to 
     * the actual worker task, so that it can be stopped if a new binding is 
     * required, and makes sure that only the last started worker process can 
     * bind its result, independently of the finish order. 
     */ 
    private static final class AsyncDrawable extends ColorDrawable {
 
        private final WeakReference<BitmapWorkerTask> mBitmapWorkerTaskReference;
 
        /** 
         * Constructor of <code>AsyncDrawable</code> 
         */ 
        public AsyncDrawable(final Resources res, final Bitmap bitmap,
                final BitmapWorkerTask mBitmapWorkerTask) {
            super(Color.TRANSPARENT);
            mBitmapWorkerTaskReference = new WeakReference<BitmapWorkerTask>(mBitmapWorkerTask);
        } 
 
        /** 
         * @return The {@link BitmapWorkerTask} associated with this drawable 
         */ 
        public BitmapWorkerTask getBitmapWorkerTask() { 
            return mBitmapWorkerTaskReference.get();
        } 
    } 
 
    /** 
     * Called to fetch the artist or ablum art. 
     *  
     * @param key The unique identifier for the image. 
     * @param artistName The artist name for the Last.fm API. 
     * @param albumName The album name for the Last.fm API. 
     * @param albumId The album art index, to check for missing artwork. 
     * @param imageView The {@link ImageView} used to set the cached 
     *            {@link Bitmap}. 
     * @param imageType The type of image URL to fetch for. 
     */ 
    protected void loadImage(final String key, final String artistName, final String albumName,
            final String albumId, final ImageView imageView, final ImageType imageType) {
        if (key == null || mImageCache == null || imageView == null) {
            return; 
        } 
        // First, check the memory for the image 
        final Bitmap lruBitmap = mImageCache.getBitmapFromMemCache(key);
        if (lruBitmap != null && imageView != null) {
            // Bitmap found in memory cache 
            imageView.setImageBitmap(lruBitmap);
        } else if (executePotentialWork(key, imageView) && imageView != null && !isScrolling()) {
            // Otherwise run the worker task 
            final BitmapWorkerTask bitmapWorkerTask = new BitmapWorkerTask(imageView, imageType);
            final AsyncDrawable asyncDrawable = new AsyncDrawable(mResources, mDefault,
                    bitmapWorkerTask);
            imageView.setImageDrawable(asyncDrawable);
            // Don't execute the BitmapWorkerTask while scrolling 
            if (isScrolling()) { 
                cancelWork(imageView);
            } else { 
                ApolloUtils.execute(false, bitmapWorkerTask, key, artistName, albumName, albumId);