Android Open Source - webimageloader Image Helper






From Project

Back to project page webimageloader.

License

The source code is released under:

Apache License

If you think the Android project webimageloader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.webimageloader.ext;
//from www  . ja va 2s  .  co m
import android.view.View;
import android.widget.ProgressBar;
import com.webimageloader.ImageLoader;
import com.webimageloader.ImageLoader.Listener;
import com.webimageloader.Request;
import com.webimageloader.transformation.Transformation;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.util.Log;
import android.widget.ImageView;

/**
 * Helper class for loading images into a {@link ImageView}
 *
 * @author Alexander Blom <alexanderblom.se>
 */
public class ImageHelper {
    /**
     * Interface for drawable creators
     */
    public interface DrawableCreator {
        /**
         * Create a drawable from this bitmap
         */
        Drawable createDrawable(Context context, Bitmap b);
    }

    private static final String TAG = "ImageHelper";

    private static final int DEFAULT_FADE_DURATION = 300;

    private static final DefaultDrawableCreator DEFAULT_CREATOR = new DefaultDrawableCreator();

    private Context context;
    private ImageLoader loader;

    private int loadingResource;
    private int errorResource;
    private boolean fadeIn = false;
    private int fadeDuration = DEFAULT_FADE_DURATION;

    private LoadingListener listener;
    private DrawableCreator drawableCreator = DEFAULT_CREATOR;


    /**
     * Create a new {@link ImageHelper} using the specified loader
     *
     * @param context the context when getting resources
     * @param loader the load to use
     */
    public ImageHelper(Context context, ImageLoader loader) {
        this.context = context;
        this.loader = loader;

        listener = new LoadingListener();
    }

    /**
     * Set an image to be displayed when an image is loading
     *
     * @param loadingResource the resource to use
     *
     * @return this helper
     */
    public ImageHelper setLoadingResource(int loadingResource) {
        this.loadingResource = loadingResource;

        return this;
    }

    /**
     * Set an image to be displayed if an error occurred
     *
     * @param errorResource the resource to use
     * @return this helper
     */
    public ImageHelper setErrorResource(int errorResource) {
        this.errorResource = errorResource;

        return this;
    }

    /**
     * Set whether to fade in after loading
     *
     * @param fadeIn true or false
     * @return this helper
     */
    public ImageHelper setFadeIn(boolean fadeIn) {
        this.fadeIn = fadeIn;

        return this;
    }

    /**
     * Enable and set the fade in duration.
     *
     * @param duration transition duration in milliseconds.
     * @return this helper
     */
    public ImageHelper setFadeIn(int duration) {
        this.fadeIn = true;
        this.fadeDuration = duration;

        return this;
    }

    /**
     * Load the specified url into this {@link ImageView}.
     *
     * @param v the target view
     * @param url the url to load
     * @return this helper
     *
     * @see #load(ImageView, String, Transformation)
     */
    public ImageHelper load(ImageView v, String url) {
        return load(v, url, null);
    }

    /**
     * Load the specified url into this {@link ImageView}.
     *
     * @param v the target view
     * @param url the url to load
     * @param transformation transformation to apply, can be null
     * @return this helper
     */
    public ImageHelper load(ImageView v, String url, Transformation transformation) {
        return load(v, new Request(url, transformation));
    }

    /**
     * Load the specified url into this {@link ImageView}.
     *
     * @param v the target view
     * @param request the request to load
     * @return this helper
     */
    public ImageHelper load(ImageView v, Request request) {
        loadInternal(v, request, listener, null);

        return this;
    }

    /**
     * Load the specified url into this {@link ImageView}.
     *
     * @param v the target view
     * @param progress {@link ProgressBar} to be updated when loading
     * @param url the url to load
     * @return this helper
     */
    public ImageHelper load(ImageView v, ProgressBar progress, String url) {
        return load(v, progress, url, null);
    }

    /**
     * Load the specified url into this {@link ImageView}.
     *
     * @param v the target view
     * @param progress {@link ProgressBar} to be updated when loading
     * @param url the url to load
     * @param transformation transformation to apply, can be null
     * @return this helper
     */
    public ImageHelper load(ImageView v, ProgressBar progress, String url, Transformation transformation) {
        return load(v, progress, new Request(url, transformation));
    }

    /**
     * Load the specified url into this {@link ImageView}.
     *
     * @param v the target view
     * @param progress {@link ProgressBar} to be updated when loading
     * @param request the request to load
     * @return this helper
     */
    public ImageHelper load(ImageView v, ProgressBar progress, Request request) {
        RequestProgressListener listener = new RequestProgressListener(progress);
        if (loadInternal(v, request, listener, listener) == null) {
            listener.showProgress(v);
        } else {
            listener.showImage(v);
        }

        return this;
    }

    private Bitmap loadInternal(ImageView v, Request request, Listener<ImageView> listener, ImageLoader.ProgressListener progressListener) {
        Bitmap b = loader.load(v, request, listener, progressListener);

        if (b != null) {
            v.setImageDrawable(drawableCreator.createDrawable(context, b));
        } else if (loadingResource != 0) {
            v.setImageResource(loadingResource);
        } else {
            v.setImageDrawable(null);
        }

        return b;
    }

    /**
     * Set a drawable creator
     * @param drawableCreator the drawable creator
     * @return this helper
     */
    public ImageHelper setDrawableCreator(DrawableCreator drawableCreator) {
        this.drawableCreator = drawableCreator;

        return this;
    }

    private class LoadingListener implements Listener<ImageView> {
        @Override
        public void onSuccess(ImageView v, Bitmap b) {
            if (!fadeIn) {
                v.setImageBitmap(b);
            } else {
                Drawable old = v.getDrawable();
                if (old == null) {
                    old = new ColorDrawable(android.R.color.transparent);
                }

                TransitionDrawable d = new TransitionDrawable(new Drawable[] {
                        old,
                        drawableCreator.createDrawable(context, b)
                });

                v.setImageDrawable(d);
                d.startTransition(fadeDuration);
            }
        }

        @Override
        public void onError(ImageView v, Throwable t) {
            Log.d(TAG, "Error loading bitmap", t);
            if (errorResource > 0) {
                v.setImageResource(errorResource);
            }
        }
    }

    private static class DefaultDrawableCreator implements DrawableCreator {
        @Override
        public Drawable createDrawable(Context context, Bitmap b) {
            return new BitmapDrawable(context.getResources(), b);
        }
    }

    private class RequestProgressListener extends LoadingListener implements ImageLoader.ProgressListener {
        private ProgressBar progressBar;

        public RequestProgressListener(ProgressBar progressBar) {
            this.progressBar = progressBar;
        }

        @Override
        public void onProgress(float value) {
            progressBar.setProgress((int) (value * progressBar.getMax()));
        }

        @Override
        public void onSuccess(ImageView v, Bitmap b) {
            super.onSuccess(v, b);

            showImage(v);
        }

        public void showProgress(ImageView v) {
            v.setVisibility(View.INVISIBLE);
            progressBar.setVisibility(View.VISIBLE);
            progressBar.setProgress(0);
        }

        public void showImage(ImageView v) {
            v.setVisibility(View.VISIBLE);
            progressBar.setVisibility(View.INVISIBLE);
        }
    }
}




Java Source Code List

com.webimageloader.ConnectionFactory.java
com.webimageloader.ConnectionHandler.java
com.webimageloader.Constants.java
com.webimageloader.ImageLoaderImpl.java
com.webimageloader.ImageLoader.java
com.webimageloader.Request.java
com.webimageloader.content.ContentURLConnection.java
com.webimageloader.content.ContentURLStreamHandler.java
com.webimageloader.ext.ImageHelper.java
com.webimageloader.ext.ImageLoaderApplication.java
com.webimageloader.loader.BackgroundLoader.java
com.webimageloader.loader.DiskLoader.java
com.webimageloader.loader.LoaderManager.java
com.webimageloader.loader.LoaderRequest.java
com.webimageloader.loader.LoaderWork.java
com.webimageloader.loader.Loader.java
com.webimageloader.loader.MemoryCache.java
com.webimageloader.loader.MemoryLoader.java
com.webimageloader.loader.Metadata.java
com.webimageloader.loader.NetworkLoader.java
com.webimageloader.loader.PendingRequests.java
com.webimageloader.loader.SimpleBackgroundLoader.java
com.webimageloader.loader.TransformingLoader.java
com.webimageloader.sample.AsyncLoader.java
com.webimageloader.sample.ExampleApplication.java
com.webimageloader.sample.FastImageView.java
com.webimageloader.sample.MainActivity.java
com.webimageloader.sample.numbers.NumberDetailsActivity.java
com.webimageloader.sample.numbers.NumbersActivity.java
com.webimageloader.sample.patterns.PatternDetailsActivity.java
com.webimageloader.sample.patterns.PatternsActivity.java
com.webimageloader.sample.patterns.PatternsListFragment.java
com.webimageloader.sample.progress.ProgressActivity.java
com.webimageloader.transformation.ScaleTransformation.java
com.webimageloader.transformation.SimpleTransformation.java
com.webimageloader.transformation.Transformation.java
com.webimageloader.util.AbstractImageLoader.java
com.webimageloader.util.Android.java
com.webimageloader.util.BitmapUtils.java
com.webimageloader.util.FlushedInputStream.java
com.webimageloader.util.Hasher.java
com.webimageloader.util.HeaderParser.java
com.webimageloader.util.IOUtil.java
com.webimageloader.util.InputSupplier.java
com.webimageloader.util.ListenerFuture.java
com.webimageloader.util.LruCache.java
com.webimageloader.util.PriorityThreadFactory.java
com.webimageloader.util.WaitFuture.java