Android Volley:在imageload之后执行任务

时间:2013-12-17 11:22:05

标签: android android-volley networkimageview

我正在使用Volley Library下载图像。我想在加载图像后更改TextView的文本。就像,在加载文本之前。并且在加载图像后,它应该更改为已加载。

NetworkImageView image = (NetworkImageView)findViewById(R.id.imageView1);
image.setDefaultImageResId(R.drawable.def);
image.setImageUrl(url, ImageUtil.getImageLoader(getApplicationContext()));

当图像完全加载时,我如何收听或调用功能?

1 个答案:

答案 0 :(得分:1)

您可以在下面的代码段中尝试。

ImageView image = ImageView.class.cast(layout.findViewById(R.id...));
    String url = "..."; // URL of the image

    mImageLoader.get(url, new ImageListener() {

                public void onErrorResponse(VolleyError arg0) {
                    image.setImageResource(R.drawable.icon_error); // set an error image if the download fails
                }

                public void onResponse(ImageContainer response, boolean arg1) {
                    if (response.getBitmap() != null) {
                        image.startAnimation(AnimationUtils.loadAnimation(container.getContext(), android.R.anim.fade_in));
                        image.setImageBitmap(response.getBitmap());
                    } else
                        image.setImageResource(R.drawable.icon_loading); // set the loading image while the download is in progress
                }
            });

您可以按如下所示创建mImageLoader实例以实现缓存

mRequestQueue = Volley.newRequestQueue(context);
mImageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() {
    private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10);
    public void putBitmap(String url, Bitmap bitmap) {
        mCache.put(url, bitmap);
    }
    public Bitmap getBitmap(String url) {
        return mCache.get(url);
    }
});'