Android Open Source - cube-sdk Default Image Load Handler






From Project

Back to project page cube-sdk.

License

The source code is released under:

Apache License

If you think the Android project cube-sdk 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 in.srain.cube.image.impl;
//from   w w  w .  j  a  va 2s .  c o m
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
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.view.ViewGroup;
import in.srain.cube.image.CubeImageView;
import in.srain.cube.image.ImageTask;
import in.srain.cube.image.drawable.RoundedDrawable;
import in.srain.cube.image.iface.ImageLoadHandler;
import in.srain.cube.util.Debug;
import in.srain.cube.util.Version;

/**
 * A simple implementation of {@link ImageLoadHandler}.
 * <p/>
 * This loader will put a background to ImageView when the image is loading.
 *
 * @author http://www.liaohuqiu.net
 */
public class DefaultImageLoadHandler implements ImageLoadHandler {

    private final static boolean DEBUG = Debug.DEBUG_IMAGE;
    private final static String LOG_TAG = Debug.DEBUG_IMAGE_LOG_TAG;
    private final static String MSG_LOADING = "%s onLoading";
    private final static String MSG_LOAD_ERROR = "%s load error";
    private final static String MSG_LOAD_FINISH = "%s onLoadFinish %s %s %s %s";

    private Context mContext;
    private final static int DISPLAY_FADE_IN = 0x01;
    private final static int DISPLAY_ROUNDED = 0x02;

    private int mDisplayTag = DISPLAY_FADE_IN;

    private Drawable mLoadingDrawable;
    private Drawable mErrorDrawable;

    private int mLoadingColor = -1;
    private float mCornerRadius = 10;

    private boolean mResizeImageViewAfterLoad = false;

    public DefaultImageLoadHandler(Context context) {
        mContext = context;
        if (Version.hasHoneycomb()) {
            int color = Color.parseColor("#fafafa");
            mLoadingDrawable = new ColorDrawable(color);
        }
    }

    /**
     * If set to true, the image will fade-in once it has been loaded by the background thread.
     */
    public void setImageFadeIn(boolean fadeIn) {
        if (fadeIn) {
            mDisplayTag |= DISPLAY_FADE_IN;
        } else {
            mDisplayTag &= ~DISPLAY_FADE_IN;
        }
    }

    /**
     *
     */
    public void setImageRounded(boolean rounded, float cornerRadius) {
        if (rounded) {
            mDisplayTag |= DISPLAY_ROUNDED;
            mCornerRadius = cornerRadius;
        } else {
            mDisplayTag &= ~DISPLAY_ROUNDED;
        }
    }

    public void setResizeImageViewAfterLoad(boolean resize) {
        mResizeImageViewAfterLoad = resize;
    }

    /**
     * set the placeholder bitmap
     */
    public void setLoadingBitmap(Bitmap loadingBitmap) {
        if (Version.hasHoneycomb()) {
            mLoadingDrawable = new BitmapDrawable(mContext.getResources(), loadingBitmap);
        }
    }

    /**
     * set the placeholder Image
     *
     * @param loadingBitmap the resources id
     */
    public void setLoadingResources(int loadingBitmap) {
        setLoadingBitmap(BitmapFactory.decodeResource(mContext.getResources(), loadingBitmap));
    }

    public void setErrorBitmap(Bitmap bitmap) {
        if (Version.hasHoneycomb()) {
            mErrorDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
        }
    }

    /**
     * set the error Image
     *
     * @param resId
     */
    public void setErrorResources(int resId) {
        setErrorBitmap(BitmapFactory.decodeResource(mContext.getResources(), resId));
    }

    /**
     * set the placeholder by color
     *
     * @param color
     */
    public void setLoadingImageColor(int color) {
        mLoadingColor = color;
        mLoadingDrawable = new ColorDrawable(color);
    }

    /**
     * set the placeholder image by color string like: #ffffff
     *
     * @param colorString
     */
    public void setLoadingImageColor(String colorString) {
        setLoadingImageColor(Color.parseColor(colorString));
    }

    @Override
    public void onLoading(ImageTask imageTask, CubeImageView imageView) {
        if (imageView == null) {
            return;
        }
        if (DEBUG) {
            Log.d(LOG_TAG, String.format(MSG_LOADING, imageTask));
        }
        if (Version.hasHoneycomb()) {
            if (mLoadingDrawable != null && imageView != null && imageView.getDrawable() != mLoadingDrawable) {
                imageView.setImageDrawable(mLoadingDrawable);
            }
        } else {
            imageView.setImageDrawable(null);
        }
    }

    @Override
    public void onLoadError(ImageTask imageTask, CubeImageView imageView, int errorCode) {
        if (DEBUG) {
            Log.d(LOG_TAG, String.format(MSG_LOAD_ERROR, imageTask));
        }
        if (Version.hasHoneycomb()) {
            if (mErrorDrawable != null && imageView != null && imageView.getDrawable() != mErrorDrawable) {
                imageView.setImageDrawable(mErrorDrawable);
            }
        } else {
            imageView.setImageDrawable(null);
        }
    }

    @Override
    public void onLoadFinish(ImageTask imageTask, CubeImageView imageView, BitmapDrawable drawable) {

        if (imageView == null) {
            return;
        }

        Drawable d = drawable;
        if (drawable != null) {

            if (mResizeImageViewAfterLoad) {
                int w = drawable.getBitmap().getWidth();
                int h = drawable.getBitmap().getHeight();

                if (w > 0 && h > 0) {

                    ViewGroup.LayoutParams lyp = imageView.getLayoutParams();
                    if (lyp != null) {
                        lyp.width = w;
                        lyp.height = h;
                        imageView.setLayoutParams(lyp);
                    }
                }
            }

            // RoundedDrawable will not recycle automatically when API level is lower than 11
            if ((mDisplayTag & DISPLAY_ROUNDED) == DISPLAY_ROUNDED && Version.hasHoneycomb()) {
                d = new RoundedDrawable(drawable.getBitmap(), mCornerRadius, 0);
            }
            if ((mDisplayTag & DISPLAY_FADE_IN) == DISPLAY_FADE_IN) {
                int loadingColor = android.R.color.transparent;
                if (mLoadingColor != -1) {
                    loadingColor = mLoadingColor;
                }
                final TransitionDrawable td = new TransitionDrawable(new Drawable[]{new ColorDrawable(loadingColor), d});
                imageView.setImageDrawable(td);
                td.startTransition(200);
            } else {

                if (DEBUG) {
                    Drawable oldDrawable = imageView.getDrawable();
                    int w = 0, h = 0;
                    if (oldDrawable != null) {
                        w = oldDrawable.getIntrinsicWidth();
                        h = oldDrawable.getIntrinsicHeight();
                    }
                    Log.d(LOG_TAG, String.format(MSG_LOAD_FINISH, imageTask, w, h, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));
                }
                imageView.setImageDrawable(drawable);
            }
        }
    }
}




Java Source Code List

in.srain.cube.Cube.java
in.srain.cube.app.CubeFragmentActivity.java
in.srain.cube.app.CubeFragment.java
in.srain.cube.app.FragmentParam.java
in.srain.cube.app.ICubeFragment.java
in.srain.cube.app.XActivity.java
in.srain.cube.app.lifecycle.IComponentContainer.java
in.srain.cube.app.lifecycle.LifeCycleComponentManager.java
in.srain.cube.app.lifecycle.LifeCycleComponent.java
in.srain.cube.cache.CacheInfo.java
in.srain.cube.cache.CacheManager.java
in.srain.cube.cache.CacheResultType.java
in.srain.cube.cache.ICacheAble.java
in.srain.cube.cache.IFileCache.java
in.srain.cube.cache.QueryHandler.java
in.srain.cube.cache.Query.java
in.srain.cube.concurrent.BlockingDeque.java
in.srain.cube.concurrent.Deque.java
in.srain.cube.concurrent.LinkedBlockingDeque.java
in.srain.cube.concurrent.SimpleExecutor.java
in.srain.cube.concurrent.SimpleTask.java
in.srain.cube.file.DiskLruCache.java
in.srain.cube.file.FileUtil.java
in.srain.cube.file.LruFileCache.java
in.srain.cube.image.CubeImageView.java
in.srain.cube.image.ImageLoaderFactory.java
in.srain.cube.image.ImageLoader.java
in.srain.cube.image.ImagePerformanceStatistics.java
in.srain.cube.image.ImageProvider.java
in.srain.cube.image.ImageReuseInfoManger.java
in.srain.cube.image.ImageReuseInfo.java
in.srain.cube.image.ImageTaskStatistics.java
in.srain.cube.image.ImageTask.java
in.srain.cube.image.drawable.RecyclingBitmapDrawable.java
in.srain.cube.image.drawable.RoundedDrawable.java
in.srain.cube.image.iface.ImageFileProvider.java
in.srain.cube.image.iface.ImageLoadHandler.java
in.srain.cube.image.iface.ImageLoadProgressHandler.java
in.srain.cube.image.iface.ImageMemoryCache.java
in.srain.cube.image.iface.ImageResizer.java
in.srain.cube.image.iface.ImageTaskExecutor.java
in.srain.cube.image.impl.DefaultImageLoadHandler.java
in.srain.cube.image.impl.DefaultImageResizer.java
in.srain.cube.image.impl.DefaultImageTaskExecutor.java
in.srain.cube.image.impl.DefaultMemoryCache.java
in.srain.cube.image.impl.LruImageFileProvider.java
in.srain.cube.image.impl.SimpleDownloader.java
in.srain.cube.mints.base.BlockMenuFragment.java
in.srain.cube.mints.base.DemoBaseActivity.java
in.srain.cube.mints.base.TitleBaseActivity.java
in.srain.cube.mints.base.TitleBaseFragment.java
in.srain.cube.mints.base.TitleHeaderBar.java
in.srain.cube.request.CacheAbleRequestDefaultHandler.java
in.srain.cube.request.CacheAbleRequestHandler.java
in.srain.cube.request.CacheAbleRequestJsonHandler.java
in.srain.cube.request.CacheAbleRequest.java
in.srain.cube.request.FailData.java
in.srain.cube.request.ICacheAbleRequest.java
in.srain.cube.request.IRequest.java
in.srain.cube.request.JsonData.java
in.srain.cube.request.RequestBase.java
in.srain.cube.request.RequestCacheManager.java
in.srain.cube.request.RequestData.java
in.srain.cube.request.RequestDefaultHandler.java
in.srain.cube.request.RequestFinishHandler.java
in.srain.cube.request.RequestHandler.java
in.srain.cube.request.RequestJsonHandler.java
in.srain.cube.request.SimpleJsonRequest.java
in.srain.cube.request.SimpleRequestManager.java
in.srain.cube.request.SimpleRequest.java
in.srain.cube.uri.CustomizedUri.java
in.srain.cube.util.Debug.java
in.srain.cube.util.Encrypt.java
in.srain.cube.util.Env.java
in.srain.cube.util.LocalDisplay.java
in.srain.cube.util.NetworkStatusManager.java
in.srain.cube.util.ResourceMan.java
in.srain.cube.util.SystemWatcher.java
in.srain.cube.util.Version.java
in.srain.cube.views.DotView.java
in.srain.cube.views.IScrollHeaderFrameHandler.java
in.srain.cube.views.MoreActionView.java
in.srain.cube.views.ScrollHeaderFrame.java
in.srain.cube.views.banner.BannerAdapter.java
in.srain.cube.views.banner.PagerIndicator.java
in.srain.cube.views.banner.SliderBanner.java
in.srain.cube.views.block.BlockListAdapter.java
in.srain.cube.views.block.BlockListView.java
in.srain.cube.views.list.ListPageInfo.java
in.srain.cube.views.list.ListViewDataAdapterBase.java
in.srain.cube.views.list.ListViewDataAdapter.java
in.srain.cube.views.list.PagedListDataModel.java
in.srain.cube.views.list.PagedListViewDataAdapter.java
in.srain.cube.views.list.ViewHolderBase.java
in.srain.cube.views.list.ViewHolderCreator.java
in.srain.cube.views.mix.AutoPlayer.java
in.srain.cube.views.pager.PageIndicator.java
in.srain.cube.views.pager.TabPageIndicator.java