Search in sources :

Example 1 with ImageContainer

use of com.android.volley.toolbox.ImageLoader.ImageContainer in project WordPress-Android by wordpress-mobile.

the class ReaderPhotoView method loadLoResImage.

private void loadLoResImage() {
    if (!hasLayout() || TextUtils.isEmpty(mLoResImageUrl)) {
        return;
    }
    // skip if this same image url is already being loaded
    if (isRequestingUrl(mLoResContainer, mLoResImageUrl)) {
        AppLog.d(AppLog.T.READER, "reader photo > already requesting lo-res");
        return;
    }
    Point pt = DisplayUtils.getDisplayPixelSize(this.getContext());
    int maxSize = Math.min(pt.x, pt.y);
    showProgress();
    mLoResContainer = WordPress.sImageLoader.get(mLoResImageUrl, new ImageLoader.ImageListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            AppLog.e(AppLog.T.READER, error);
            hideProgress();
            showError();
        }

        @Override
        public void onResponse(final ImageContainer response, boolean isImmediate) {
            post(new Runnable() {

                @Override
                public void run() {
                    handleResponse(response.getBitmap(), true);
                }
            });
        }
    }, maxSize, maxSize);
}
Also used : VolleyError(com.android.volley.VolleyError) Point(android.graphics.Point) Point(android.graphics.Point) ImageContainer(com.android.volley.toolbox.ImageLoader.ImageContainer)

Example 2 with ImageContainer

use of com.android.volley.toolbox.ImageLoader.ImageContainer in project WordPress-Android by wordpress-mobile.

the class ReaderPhotoView method loadHiResImage.

private void loadHiResImage() {
    if (!hasLayout() || TextUtils.isEmpty(mHiResImageUrl)) {
        return;
    }
    if (isRequestingUrl(mHiResContainer, mHiResImageUrl)) {
        AppLog.d(AppLog.T.READER, "reader photo > already requesting hi-res");
        return;
    }
    Point pt = DisplayUtils.getDisplayPixelSize(this.getContext());
    int maxSize = Math.max(pt.x, pt.y);
    mHiResContainer = WordPress.sImageLoader.get(mHiResImageUrl, new ImageLoader.ImageListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            AppLog.e(AppLog.T.READER, error);
        }

        @Override
        public void onResponse(final ImageContainer response, boolean isImmediate) {
            post(new Runnable() {

                @Override
                public void run() {
                    handleResponse(response.getBitmap(), false);
                }
            });
        }
    }, maxSize, maxSize);
}
Also used : VolleyError(com.android.volley.VolleyError) Point(android.graphics.Point) Point(android.graphics.Point) ImageContainer(com.android.volley.toolbox.ImageLoader.ImageContainer)

Example 3 with ImageContainer

use of com.android.volley.toolbox.ImageLoader.ImageContainer in project WordPress-Android by wordpress-mobile.

the class ThemeBrowserFragment method onMovedToScrapHeap.

@Override
public void onMovedToScrapHeap(View view) {
    // cancel image fetch requests if the view has been moved to recycler.
    NetworkImageView niv = (NetworkImageView) view.findViewById(R.id.theme_grid_item_image);
    if (niv != null) {
        // this tag is set in the ThemeBrowserAdapter class
        String requestUrl = (String) niv.getTag();
        if (requestUrl != null) {
            // need a listener to cancel request, even if the listener does nothing
            ImageContainer container = WordPress.sImageLoader.get(requestUrl, new ImageListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                }

                @Override
                public void onResponse(ImageContainer response, boolean isImmediate) {
                }
            });
            container.cancelRequest();
        }
    }
}
Also used : VolleyError(com.android.volley.VolleyError) ImageListener(com.android.volley.toolbox.ImageLoader.ImageListener) NetworkImageView(com.android.volley.toolbox.NetworkImageView) ImageContainer(com.android.volley.toolbox.ImageLoader.ImageContainer)

Example 4 with ImageContainer

use of com.android.volley.toolbox.ImageLoader.ImageContainer in project AndroidLife by CaMnter.

the class NetworkImageView method loadImageIfNecessary.

/**
 * Loads the image for the view if it isn't already loaded.
 *
 * @param isInLayoutPass True if this was invoked from a layout pass, false otherwise.
 */
/*
     * 加载图片
     *
     * isInLayoutPass true:XML 渲染的时候就开始加载
     * isInLayoutPass false:XML 渲染的时候不加载
     */
void loadImageIfNecessary(final boolean isInLayoutPass) {
    // 获取宽高
    int width = getWidth();
    int height = getHeight();
    // 获取 ImageView.ScaleType( CENTER_CROP, FIT_XY ... )
    ScaleType scaleType = getScaleType();
    // 记录 是不是 wrap_content 类型的 宽高
    boolean wrapWidth = false, wrapHeight = false;
    if (getLayoutParams() != null) {
        wrapWidth = getLayoutParams().width == LayoutParams.WRAP_CONTENT;
        wrapHeight = getLayoutParams().height == LayoutParams.WRAP_CONTENT;
    }
    // if the view's bounds aren't known yet, and this is not a wrap-content/wrap-content
    // view, hold off on loading the image.
    // 如果 宽高 都是 wrap_content 类型的,记录 isFullyWrapContent = true
    boolean isFullyWrapContent = wrapWidth && wrapHeight;
    if (width == 0 && height == 0 && !isFullyWrapContent) {
        return;
    }
    /*
         * 如果没有图片的 url
         * 除了 不去加载图片 以外
         * 还要取消 当前正在执行的请求
         */
    if (TextUtils.isEmpty(mUrl)) {
        // 取消 当前正在执行的请求
        if (mImageContainer != null) {
            mImageContainer.cancelRequest();
            mImageContainer = null;
        }
        // 设置 默认图片
        setDefaultImageOrNull();
        return;
    }
    /*
         * 当前显示的图片 的相关信息,还存在( 也可能是前一个 url )
         */
    if (mImageContainer != null && mImageContainer.getRequestUrl() != null) {
        /*
             * 当前显示的图片 url 和 本次需要 加载 的图片 url 一致
             * 就返回,不做任何操作
             */
        if (mImageContainer.getRequestUrl().equals(mUrl)) {
            // if the request is from the same URL, return.
            return;
        } else {
            // if there is a pre-existing request, cancel it if it's fetching a different URL.
            /*
                 * url 不一样
                 * 不管怎样,先将当前 正在执行的请求 取消
                 * 意思是:当前显示的图片,已经达到预期效果
                 * 为了 预防 被修改,要将 ImageContainer 内 正在执行的请求 取消到
                 * 因为:预期效果已经达到,就必须需要正在执行的请求
                 */
            mImageContainer.cancelRequest();
            // 设置 默认图片
            setDefaultImageOrNull();
        }
    }
    // Calculate the max image width / height to use while ignoring WRAP_CONTENT dimens.
    // 根据 wrap 标记,设置最大宽高
    int maxWidth = wrapWidth ? 0 : width;
    int maxHeight = wrapHeight ? 0 : height;
    // The pre-existing content of this view didn't match the current URL. Load the new image
    // from the network.
    /**
     ****************************************************************
     * 使用 ImageLoader 加载图片 返回 对应的 ImageContainer 图片相关信息类 *
     *****************************************************************
     */
    ImageContainer newContainer = mImageLoader.get(mUrl, new ImageListener() {

        /*
             * 加载 失败
             */
        @Override
        public void onErrorResponse(VolleyError error) {
            // 设置 加载失败 图片
            if (mErrorImageId != 0) {
                setImageResource(mErrorImageId);
            }
        }

        /*
             * 加载 成功
             */
        @Override
        public void onResponse(final ImageContainer response, boolean isImmediate) {
            /*
                 * 只有
                 * isImmediate = true
                 * isInLayoutPass = true
                 */
            if (isImmediate && isInLayoutPass) {
                // 发回 主 UI 线程
                post(new Runnable() {

                    @Override
                    public void run() {
                        onResponse(response, false);
                    }
                });
                return;
            }
            // 如果 存在 Bitmap 数据
            if (response.getBitmap() != null) {
                // 设置 图片
                setImageBitmap(response.getBitmap());
            } else if (mDefaultImageId != 0) {
                // 设置 默认图片
                setImageResource(mDefaultImageId);
            }
        }
    }, maxWidth, maxHeight, scaleType);
    // update the ImageContainer to be the new bitmap container.
    // 重新设置 图片相关信息
    mImageContainer = newContainer;
}
Also used : VolleyError(com.android.volley.VolleyError) ImageListener(com.android.volley.toolbox.ImageLoader.ImageListener) ImageContainer(com.android.volley.toolbox.ImageLoader.ImageContainer)

Example 5 with ImageContainer

use of com.android.volley.toolbox.ImageLoader.ImageContainer in project FastDev4Android by jiangqqlmj.

the class NetworkImageView method loadImageIfNecessary.

/**
 * Loads the image for the view if it isn't already loaded.
 * @param isInLayoutPass True if this was invoked from a layout pass, false otherwise.
 */
void loadImageIfNecessary(final boolean isInLayoutPass) {
    int width = getWidth();
    int height = getHeight();
    ScaleType scaleType = getScaleType();
    boolean wrapWidth = false, wrapHeight = false;
    if (getLayoutParams() != null) {
        wrapWidth = getLayoutParams().width == LayoutParams.WRAP_CONTENT;
        wrapHeight = getLayoutParams().height == LayoutParams.WRAP_CONTENT;
    }
    // if the view's bounds aren't known yet, and this is not a wrap-content/wrap-content
    // view, hold off on loading the image.
    boolean isFullyWrapContent = wrapWidth && wrapHeight;
    if (width == 0 && height == 0 && !isFullyWrapContent) {
        return;
    }
    // currently loaded image.
    if (TextUtils.isEmpty(mUrl)) {
        if (mImageContainer != null) {
            mImageContainer.cancelRequest();
            mImageContainer = null;
        }
        setDefaultImageOrNull();
        return;
    }
    // if there was an old request in this view, check if it needs to be canceled.
    if (mImageContainer != null && mImageContainer.getRequestUrl() != null) {
        if (mImageContainer.getRequestUrl().equals(mUrl)) {
            // if the request is from the same URL, return.
            return;
        } else {
            // if there is a pre-existing request, cancel it if it's fetching a different URL.
            mImageContainer.cancelRequest();
            setDefaultImageOrNull();
        }
    }
    // Calculate the max image width / height to use while ignoring WRAP_CONTENT dimens.
    int maxWidth = wrapWidth ? 0 : width;
    int maxHeight = wrapHeight ? 0 : height;
    // The pre-existing content of this view didn't match the current URL. Load the new image
    // from the network.
    ImageContainer newContainer = mImageLoader.get(mUrl, new ImageListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            if (mErrorImageId != 0) {
                setImageResource(mErrorImageId);
            }
        }

        @Override
        public void onResponse(final ImageContainer response, boolean isImmediate) {
            // the main thread.
            if (isImmediate && isInLayoutPass) {
                post(new Runnable() {

                    @Override
                    public void run() {
                        onResponse(response, false);
                    }
                });
                return;
            }
            if (response.getBitmap() != null) {
                setImageBitmap(response.getBitmap());
            } else if (mDefaultImageId != 0) {
                setImageResource(mDefaultImageId);
            }
        }
    }, maxWidth, maxHeight, scaleType);
    // update the ImageContainer to be the new bitmap container.
    mImageContainer = newContainer;
}
Also used : VolleyError(com.android.volley.VolleyError) ImageListener(com.android.volley.toolbox.ImageLoader.ImageListener) ImageContainer(com.android.volley.toolbox.ImageLoader.ImageContainer)

Aggregations

VolleyError (com.android.volley.VolleyError)10 ImageContainer (com.android.volley.toolbox.ImageLoader.ImageContainer)10 ImageListener (com.android.volley.toolbox.ImageLoader.ImageListener)8 Point (android.graphics.Point)2 Handler (android.os.Handler)1 NetworkImageView (com.android.volley.toolbox.NetworkImageView)1 CountDownLatch (java.util.concurrent.CountDownLatch)1