我在我的项目中使用volley库。
我通常让NetworkImageView使用setImageUrl方法下载图像:
networkImageView.setImageUrl(imageUrl, mImageLoader)这很好,但是..。当我尝试使用ImageLoader的get方法“手动”下载位图,然后自行设置位图时,它不起作用:
mImageLoader.get(imageUrl,new ImageLoader.ImageListener()
{
@Override
public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b)
{
if (imageContainer.getBitmap() != null)
{
networkImageView.setImageBitmap(imageContainer.getBitmap());
}
}
@Override
public void onErrorResponse(VolleyError volleyError)
{
}
});networkImageView.setImageBitmap(imageContainer.getBitmap())行什么也不做。
怎么可能是?提前感谢!
发布于 2014-03-19 08:56:31
此版本的NetworkImageView解决了此问题。
public class CustomNetworkImageView extends NetworkImageView {
private Bitmap mLocalBitmap;
private boolean mShowLocal;
public void setLocalImageBitmap(Bitmap bitmap) {
if (bitmap != null) {
mShowLocal = true;
}
this.mLocalBitmap = bitmap;
requestLayout();
}
@Override
public void setImageUrl(String url, ImageLoader imageLoader) {
mShowLocal = false;
super.setImageUrl(url, imageLoader);
}
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);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (mShowLocal) {
setImageBitmap(mLocalBitmap);
}
}
}发布于 2014-03-14 03:33:06
您可以通过在NetWorkImageView源代码中添加几行代码来实现这一点(我认为您有权编辑源代码,如果不能,您只需扩展NetWorkImageView,这是非常容易的)。
public class NetworkImageView extends ImageView {
private Bitmap bitmap;
public void setLocalImageBitmap(Bitmap bitmap){
this.bitmap=bitmap;
}
/**The volley verison of NetworkImageView has This method, you just need to add
a new condition, which is else if(bitmap!=null).
**/
private void setDefaultImageOrNull() {
if(mDefaultImageId != 0) {
setImageResource(mDefaultImageId);
}
else if(bitmap!=null){
setImageBitmap(bitmap);
}
else {
setImageBitmap(null);
}
}}
发布于 2017-03-09 07:14:42
接受的答案对我没有用..。以下代码起作用:
public class CustomNetworkImageView extends NetworkImageView {
Context mContext;
public CustomNetworkImageView(Context context) {
super(context);
mContext = context;
}
public CustomNetworkImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
mContext = context;
}
public CustomNetworkImageView(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
mContext = context;
}
@Override
public void setImageBitmap(Bitmap bm) {
if (bm == null) return;
setImageDrawable(new BitmapDrawable(mContext.getResources(), bm));
}
}https://stackoverflow.com/questions/21270152
复制相似问题