我在搜索中找到了更好的解决方案:-)
NetworkImageView 在链接NetworkImageView.java中知道它在第 104 行的宽度和第 105 行的高度
下面是NetworkImageView.java的确切代码
private void loadImageIfNecessary(final boolean isInLayoutPass) {
int width = getWidth(); // at line no 104
int height = getHeight(); // at line no 105
您只需将此信息转发给图像加载器。
在第 141 行NetworkImageView.java调用 ImageLoader#get(String requestUrl, final ImageListener listener) 方法,没有宽度和高度。将此调用更改为 ImageLoader#get(String requestUrl, ImageListener imageListener, int maxWidth, int maxHeight)。
用下面的代码替换NetworkImageView.java 的第 141 到 172 行的代码
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);
}
}
}, width, height);