com.android.volley.toolbox.ImageLoader.ImageListener Java Examples

The following examples show how to use com.android.volley.toolbox.ImageLoader.ImageListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BitmapTools.java    From volley with Apache License 2.0 6 votes vote down vote up
public ImageContainer doDisplay(View v, final String uri, BitmapDisplayConfig displayConfig, LoadingListener loadingListener) {
    	if (displayConfig == null) {
            displayConfig = mDisplayConfig;
        }
        final WeakReference<View> ref = new WeakReference<View>(v);
        final BitmapDisplayConfig curDisplayConfig = displayConfig;
        return doDisplay(ref.get(), uri, curDisplayConfig, new ImageListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
//                showImg(ref.get(), null, curDisplayConfig, false, false);
                mDisplayer.loadFailDisplay(ref.get(), curDisplayConfig);
            }

            @Override
            public void onResponse(ImageContainer response, boolean isImmediate) {
                if (response.getBitmap() == null) {
                    mDisplayer.loadDefaultDisplay(ref.get(), curDisplayConfig);
                } else {
                    curDisplayConfig.isImmediate = isImmediate;
                    mDisplayer.loadCompletedisplay(ref.get(), response.getBitmap(), curDisplayConfig);
                }
            }
        }, loadingListener);
    }
 
Example #2
Source File: PreviewPhotoActivity.java    From TraceByAmap with MIT License 6 votes vote down vote up
synchronized private void downLoadImage(int index) {
	mpbLoding.setVisibility(View.VISIBLE);
	String imageUrl = mCloudItem.getCloudImage().get(index).getUrl();
	listViews.get(index).setImageUrl(imageUrl, mImageLoader);
	mImageLoader.get(imageUrl, new ImageListener() {

		@Override
		public void onErrorResponse(VolleyError error) {
			mpbLoding.setVisibility(View.GONE);

		}

		@Override
		public void onResponse(ImageContainer response, boolean isImmediate) {
			if (response.getBitmap() != null) {
				mpbLoding.setVisibility(View.GONE);
				adapter.notifyDataSetChanged();
			}
		}
	});
}
 
Example #3
Source File: RssListAdapter.java    From android-opensource-library-56 with Apache License 2.0 5 votes vote down vote up
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflator.inflate(R.layout.card_list, null);
        holder = new ViewHolder();
        holder.text = (TextView) convertView.findViewById(R.id.title);
        holder.entryImage = (ImageView) convertView
                .findViewById(R.id.entry_image);
        holder.description = (TextView) convertView
                .findViewById(R.id.description);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    Item item = getItem(position);
    holder.text.setText(item.title);
    holder.description.setText(item.description);
    if (item.imageUrl != null) {
        ImageListener listener = ImageLoader.getImageListener(
                holder.entryImage, R.drawable.ic_launcher,
                R.drawable.ic_launcher);
        if (holder.container != null) {
            holder.container.cancelRequest();
        }
        holder.container = mImageLoader.get(item.imageUrl, listener);
        holder.entryImage.setVisibility(View.VISIBLE);
    } else {
        holder.entryImage.setVisibility(View.GONE);
    }

    return convertView;
}
 
Example #4
Source File: RssListAdapter.java    From android-opensource-library-56 with Apache License 2.0 5 votes vote down vote up
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflator.inflate(R.layout.card_list, null);
        holder = new ViewHolder();
        holder.text = (TextView) convertView.findViewById(R.id.title);
        holder.entryImage = (ImageView) convertView
                .findViewById(R.id.entry_image);
        holder.description = (TextView) convertView
                .findViewById(R.id.description);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    Item item = getItem(position);
    holder.text.setText(item.title);
    holder.description.setText(item.description);
    if (item.imageUrl != null) {
        ImageListener listener = ImageLoader.getImageListener(
                holder.entryImage, R.drawable.ic_launcher,
                R.drawable.ic_launcher);
        if (holder.container != null) {
            holder.container.cancelRequest();
        }
        holder.container = mImageLoader.get(item.imageUrl, listener);
        holder.entryImage.setVisibility(View.VISIBLE);
    } else {
        holder.entryImage.setVisibility(View.GONE);
    }

    return convertView;
}
 
Example #5
Source File: BitmapTools.java    From volley with Apache License 2.0 5 votes vote down vote up
public ImageContainer doDisplay(final String uri, ImageListener listener, LoadingListener loadingListener) {
    if (TextUtils.isEmpty(uri)) {
        if (listener != null) {
            listener.onErrorResponse(new VolleyError());
        }
        return null;
    }
    return mImageLoader.get(mContext, uri, listener,loadingListener, mDisplayConfig.bitmapWidth, mDisplayConfig.bitmapHeight);
}
 
Example #6
Source File: BitmapTools.java    From volley with Apache License 2.0 4 votes vote down vote up
public ImageContainer doDisplay(final View view, final String uri, ImageListener listener, LoadingListener loadingListener) {
    return doDisplay(view, uri, null, listener, loadingListener);
}
 
Example #7
Source File: NetworkImageView.java    From CrossBow with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
    }

    // if the URL to be loaded in this view is empty, cancel any old requests and clear the
    // 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) {
                    // If this was an immediate response that was delivered inside of a layout
                    // pass do not set the image immediately as it will trigger a requestLayout
                    // inside of a layout. Instead, defer setting the image by posting back to
                    // 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;
}
 
Example #8
Source File: NetworkImageView.java    From WayHoo with Apache License 2.0 4 votes vote down vote up
/**
 * 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();

    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;
    }

    // if the URL to be loaded in this view is empty, cancel any old requests and clear the
    // 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) {
                    // If this was an immediate response that was delivered inside of a layout
                    // pass do not set the image immediately as it will trigger a requestLayout
                    // inside of a layout. Instead, defer setting the image by posting back to
                    // 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);

    // update the ImageContainer to be the new bitmap container.
    mImageContainer = newContainer;
}
 
Example #9
Source File: NetworkImageView.java    From barterli_android with Apache License 2.0 4 votes vote down vote up
/**
 * 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.
 */
private void loadImageIfNecessary(final boolean isInLayoutPass) {

    // if the URL to be loaded in this view is empty, cancel any old
    // requests and clear the
    // currently loaded image.
    if (TextUtils.isEmpty(mUrl)) {
        if (mImageContainer != null) {
            mImageContainer.cancelRequest();
            mImageContainer = null;
        }
        setImageBitmap(null);
        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();
            setImageBitmap(null);
        }
    }

    // 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, Request<?> request) {
                    if (mErrorImageId != 0) {
                        setImageResource(mErrorImageId);
                    }
                }

                @Override
                public void onResponse(final ImageContainer response, boolean isImmediate) {
                    // If this was an immediate response that was delivered
                    // inside of a layout
                    // pass do not set the image immediately as it will
                    // trigger a requestLayout
                    // inside of a layout. Instead, defer setting the image
                    // by posting back to
                    // the main thread.
                    if (isImmediate && isInLayoutPass) {
                        post(new Runnable() {
                            @Override
                            public void run() {
                                onResponse(response, false);
                            }
                        });
                        return;
                    }

                    if (response.getBitmap() != null) {
                        setImageBitmap(response.getBitmap(), !isImmediate);
                    } else if (mDefaultImageId != 0) {
                        setImageResource(mDefaultImageId);
                    }
                }
            }, getWidth(), getHeight());

    // update the ImageContainer to be the new bitmap container.
    mImageContainer = newContainer;
}
 
Example #10
Source File: NetworkImageView.java    From android_tv_metro with Apache License 2.0 4 votes vote down vote up
/**
 * 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.
 */
private void loadImageIfNecessary(final boolean isInLayoutPass) {
    int width = getWidth();
    int height = getHeight();

    boolean isFullyWrapContent = getLayoutParams().height == LayoutParams.WRAP_CONTENT
            && getLayoutParams().width == 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.
    if (width == 0 && height == 0 && !isFullyWrapContent) {
        return;
    }

    // if the URL to be loaded in this view is empty, cancel any old requests and clear the
    // currently loaded image.
    if (TextUtils.isEmpty(mUrl)) {
        if (mImageContainer != null) {
            mImageContainer.cancelRequest();
            mImageContainer = null;
        }
        setImageBitmap(null);
        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();
            setImageBitmap(null);
        }
    }

    // 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) {
                    // If this was an immediate response that was delivered inside of a layout
                    // pass do not set the image immediately as it will trigger a requestLayout
                    // inside of a layout. Instead, defer setting the image by posting back to
                    // 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);
                    }
                }
            });

    // update the ImageContainer to be the new bitmap container.
    mImageContainer = newContainer;
}
 
Example #11
Source File: NetworkImageView.java    From FeedListViewDemo with MIT License 4 votes vote down vote up
/**
 * 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();

    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;
    }

    // if the URL to be loaded in this view is empty, cancel any old requests and clear the
    // 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) {
                    // If this was an immediate response that was delivered inside of a layout
                    // pass do not set the image immediately as it will trigger a requestLayout
                    // inside of a layout. Instead, defer setting the image by posting back to
                    // 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);

    // update the ImageContainer to be the new bitmap container.
    mImageContainer = newContainer;
}
 
Example #12
Source File: NetworkImageView.java    From okulus with Apache License 2.0 4 votes vote down vote up
/**
 * 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();

    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;
    }

    // if the URL to be loaded in this view is empty, cancel any old requests and clear the
    // 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) {
                    // If this was an immediate response that was delivered inside of a layout
                    // pass do not set the image immediately as it will trigger a requestLayout
                    // inside of a layout. Instead, defer setting the image by posting back to
                    // 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);

    // update the ImageContainer to be the new bitmap container.
    mImageContainer = newContainer;
}
 
Example #13
Source File: BitmapTools.java    From volley with Apache License 2.0 4 votes vote down vote up
public ImageContainer doDisplay(final String uri, ImageListener listener) {
    return doDisplay(uri, listener, null);
}
 
Example #14
Source File: NetworkImageView.java    From volley with Apache License 2.0 4 votes vote down vote up
/**
 * 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();

    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;
    }

    // if the URL to be loaded in this view is empty, cancel any old requests and clear the
    // 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(getContext().getApplicationContext(), mUrl,
            new ImageListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    if (mErrorImageId != 0) {
                        setImageResource(mErrorImageId);
                    }
                }

                @Override
                public void onResponse(final ImageContainer response, boolean isImmediate) {
                    // If this was an immediate response that was delivered inside of a layout
                    // pass do not set the image immediately as it will trigger a requestLayout
                    // inside of a layout. Instead, defer setting the image by posting back to
                    // 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);

    // update the ImageContainer to be the new bitmap container.
    mImageContainer = newContainer;
}
 
Example #15
Source File: NetworkImageView.java    From volley_demo with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
    }

    // if the URL to be loaded in this view is empty, cancel any old requests and clear the
    // 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) {
                    // If this was an immediate response that was delivered inside of a layout
                    // pass do not set the image immediately as it will trigger a requestLayout
                    // inside of a layout. Instead, defer setting the image by posting back to
                    // 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;
}
 
Example #16
Source File: NetworkImageView.java    From product-emm with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
    }

    // if the URL to be loaded in this view is empty, cancel any old requests and clear the
    // 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) {
                    // If this was an immediate response that was delivered inside of a layout
                    // pass do not set the image immediately as it will trigger a requestLayout
                    // inside of a layout. Instead, defer setting the image by posting back to
                    // 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;
}
 
Example #17
Source File: NetworkImageView.java    From product-emm with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
    }

    // if the URL to be loaded in this view is empty, cancel any old requests and clear the
    // 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) {
                    // If this was an immediate response that was delivered inside of a layout
                    // pass do not set the image immediately as it will trigger a requestLayout
                    // inside of a layout. Instead, defer setting the image by posting back to
                    // 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;
}
 
Example #18
Source File: NetworkImageView.java    From android-discourse with Apache License 2.0 4 votes vote down vote up
/**
 * 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.
 */
private void loadImageIfNecessary(final boolean isInLayoutPass) {
    int width = getWidth();
    int height = getHeight();

    boolean isFullyWrapContent = getLayoutParams() != null && getLayoutParams().height == LayoutParams.WRAP_CONTENT && getLayoutParams().width == 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.
    if (width == 0 && height == 0 && !isFullyWrapContent) {
        return;
    }

    // if the URL to be loaded in this view is empty, cancel any old requests and clear the
    // currently loaded image.
    if (TextUtils.isEmpty(mUrl)) {
        if (mImageContainer != null) {
            mImageContainer.cancelRequest();
            mImageContainer = null;
        }
        setImageBitmap(null);
        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();
            setImageBitmap(null);
        }
    }

    // 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) {
                    // If this was an immediate response that was delivered inside of a layout
                    // pass do not set the image immediately as it will trigger a requestLayout
                    // inside of a layout. Instead, defer setting the image by posting back to
                    // 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);
                    }
                }
            });

    // update the ImageContainer to be the new bitmap container.
    mImageContainer = newContainer;
}
 
Example #19
Source File: NetworkImageView.java    From TitanjumNote with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
    }

    // if the URL to be loaded in this view is empty, cancel any old requests and clear the
    // 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) {
                    // If this was an immediate response that was delivered inside of a layout
                    // pass do not set the image immediately as it will trigger a requestLayout
                    // inside of a layout. Instead, defer setting the image by posting back to
                    // 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;
}
 
Example #20
Source File: NetworkImageView.java    From android-common-utils with Apache License 2.0 4 votes vote down vote up
/**
 * 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();

    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;
    }

    // if the URL to be loaded in this view is empty, cancel any old requests and clear the
    // 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) {
                    // If this was an immediate response that was delivered inside of a layout
                    // pass do not set the image immediately as it will trigger a requestLayout
                    // inside of a layout. Instead, defer setting the image by posting back to
                    // 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);

    // update the ImageContainer to be the new bitmap container.
    mImageContainer = newContainer;
}
 
Example #21
Source File: NetworkImageView.java    From SimplifyReader with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
    }

    // if the URL to be loaded in this view is empty, cancel any old requests and clear the
    // 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) {
                    // If this was an immediate response that was delivered inside of a layout
                    // pass do not set the image immediately as it will trigger a requestLayout
                    // inside of a layout. Instead, defer setting the image by posting back to
                    // 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;
}
 
Example #22
Source File: NetworkImageView.java    From android-project-wo2b with Apache License 2.0 4 votes vote down vote up
/**
 * 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();

    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;
    }

    // if the URL to be loaded in this view is empty, cancel any old requests and clear the
    // 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) {
                    // If this was an immediate response that was delivered inside of a layout
                    // pass do not set the image immediately as it will trigger a requestLayout
                    // inside of a layout. Instead, defer setting the image by posting back to
                    // 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);

    // update the ImageContainer to be the new bitmap container.
    mImageContainer = newContainer;
}
 
Example #23
Source File: NetworkImageView.java    From device-database with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
    }

    // if the URL to be loaded in this view is empty, cancel any old requests and clear the
    // 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) {
                    // If this was an immediate response that was delivered inside of a layout
                    // pass do not set the image immediately as it will trigger a requestLayout
                    // inside of a layout. Instead, defer setting the image by posting back to
                    // 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;
}
 
Example #24
Source File: VolleyHelper.java    From AndroidLinkup with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 加载网络图片到ImageView
 * 
 * @param iv
 *            ImageView实例
 * @param url
 *            url地址
 */
public void loadImage(ImageView iv, String url) {
    ImageLoader imageLoader = new ImageLoader(requestQueue, new BitmapCache());
    ImageListener listener = ImageLoader.getImageListener(iv, R.drawable.icon_default, R.drawable.icon_default);
    imageLoader.get(url, listener);
}