我解决了问题!yaaeeye :D @Itai 的回答为我指明了正确的方向。我仔细查看了 NetworkImageView 代码并根据需要对其进行了修改。所以我做了一个 CustumNetworkImageView
public class CustomNetworkImageView extends ImageView {
// Added code block start
public interface ResponseObserver {
public void onError();
public void onSuccess();
}
private ResponseObserver mResponseObserver;
public void setmResponseObserver(ResponseObserver observer) {
mResponseObserver = observer;
}
private Bitmap bmp = null;
// Added code block end
private String mUrl;
private int mDefaultImageId;
private int mErrorImageId;
private ImageLoader mImageLoader;
public CustomNetworkImageView(Context context) {
this(context, null);
}
public CustomNetworkImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomNetworkImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setImageUrl(String url, ImageLoader imageLoader) {
mUrl = url;
mImageLoader = imageLoader;
loadImageIfNecessary();
}
public void setDefaultImageResId(int defaultImage) {
mDefaultImageId = defaultImage;
}
public void setErrorImageResId(int errorImage) {
mErrorImageId = errorImage;
// Added code block start
if (mResponseObserver != null) {
mResponseObserver.onError();
}
// Added code block end
}
// Added code block start
public Bitmap getBitmap() {
return bmp;
}
// Added code block end
private void loadImageIfNecessary() {
int width = getWidth();
int height = getHeight();
if (width == 0 && height == 0) {
return;
}
if (TextUtils.isEmpty(mUrl)) {
ImageContainer oldContainer = (ImageContainer) getTag();
if (oldContainer != null) {
oldContainer.cancelRequest();
setImageBitmap(null);
}
return;
}
ImageContainer oldContainer = (ImageContainer) getTag();
if (oldContainer != null && oldContainer.getRequestUrl() != null) {
if (oldContainer.getRequestUrl().equals(mUrl)) {
return;
} else {
oldContainer.cancelRequest();
setImageBitmap(null);
}
}
ImageContainer newContainer = mImageLoader.get(mUrl,
ImageLoader.getImageListener(this, mDefaultImageId, mErrorImageId));
setTag(newContainer);
final Bitmap bitmap = newContainer.getBitmap();
if (bitmap != null) {
setImageBitmap(bitmap);
// Added code block start
bmp = bitmap;
if (mResponseObserver != null) {
mResponseObserver.onSuccess();
}
// Added code block end
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
loadImageIfNecessary();
}
@Override
protected void onDetachedFromWindow() {
ImageContainer oldContainer = (ImageContainer) getTag();
if (oldContainer != null) {
oldContainer.cancelRequest();
setImageBitmap(null);
setTag(null);
}
super.onDetachedFromWindow();
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
invalidate();
}
}
使用 CustomNetworkImageView :
viewHolder.getmImageView().setImageUrl(wallThumb.get(i), AppController.getInstance().getImageLoader());
viewHolder.getmImageView().setmResponseObserver(new CustomNetworkImageView.ResponseObserver() {
@Override
public void onError() {
}
@Override
public void onSuccess() {
// Image is loaded in ImageView.. Do Something..
Bitmap bitmap = viewHolder.getmImageView().getBitmap();
}
});
SetmResponseObserver 监听是否在 ImageView 中加载了位图,如果任务完成则调用 onSuccess。我还做了一个函数,它返回图像视图上的位图,可以用作
Bitmap bmp = viewHolde.getmImageView.getBitmap();
我是android新手,所以如果我做错了什么,请随时评论并纠正我。Yaaaeeyeee 快乐的一天