use of com.android.volley.toolbox.ImageLoader.ImageListener 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();
}
}
}
use of com.android.volley.toolbox.ImageLoader.ImageListener 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;
}
use of com.android.volley.toolbox.ImageLoader.ImageListener 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;
}
use of com.android.volley.toolbox.ImageLoader.ImageListener in project saga-android by AnandChowdhary.
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);
}
if (mObserver != null) {
mObserver.onError();
}
}
@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());
if (mObserver != null) {
mObserver.onSuccess();
}
} else if (mDefaultImageId != 0) {
setImageResource(mDefaultImageId);
}
}
}, maxWidth, maxHeight, scaleType);
// update the ImageContainer to be the new bitmap container.
mImageContainer = newContainer;
}
use of com.android.volley.toolbox.ImageLoader.ImageListener in project android-volley by mcxiaoke.
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;
}
Aggregations