我在我的项目中使用凌空库。
我通常让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())行什么都不做。
怎么可能? 提前谢谢!
答案 0 :(得分:35)
此版本的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);
}
}
}
答案 1 :(得分:3)
你可以通过在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);
}
}
}
答案 2 :(得分:2)
接受的答案对我不起作用......以下代码有效:
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));
}
}
答案 3 :(得分:0)
正如Lefteris所提到的,如果要加载监听器范例,只需使用普通的ImageView。这对我有用。
答案 4 :(得分:0)
您还可以通过图像加载器设置本地位图,而不是触摸NetworkImageView
imageSaver = new ImageLoader(VolleyWebServiceManager.getInstance().getRequestQueue(), new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(50);
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url, bitmap);
FileManager.getInstance().saveImage(bitmap, FileManager.getInstance().getLastPathComponent(url), true, false);
}
public Bitmap getBitmap(String url) {
// Puts the bitmap from the file system into the cache
if (mCache.get(url) == null && FileManager.getInstance().getLocalImage(FileManager.getInstance().getLastPathComponent(url)) != null) {
putBitmap(url, FileManager.getInstance().getLocalImage(FileManager.getInstance().getLastPathComponent(url)));
}
return mCache.get(url);
}
});
答案 5 :(得分:0)
获取NetworkImageView网址,它将使用完整。
private static CustomVolleyRequestQueue mInstance;
private static Context mCtx;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private CustomVolleyRequestQueue(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized CustomVolleyRequestQueue getInstance(Context context) {
if (mInstance == null) {
mInstance = new CustomVolleyRequestQueue(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
Cache cache = new DiskBasedCache(mCtx.getCacheDir(), 10 * 1024 * 1024);
Network network = new BasicNetwork(new HurlStack());
mRequestQueue = new RequestQueue(cache, network);
// Don't forget to start the volley request queue
mRequestQueue.start();
}
return mRequestQueue;
}
public ImageLoader getImageLoader() {
return mImageLoader;
}