【问题标题】:Load image into custom Imageview rather than NetworkImageView while using volley使用 volley 时将图像加载到自定义 Imageview 而不是 NetworkImageView
【发布时间】:2016-07-16 04:00:22
【问题描述】:

在使用 volley 从服务器加载图像时,我需要在 Pinview 而不是本机 NetworkImageView 中加载图像。

我浏览了很多文章,但一无所获。有什么办法吗?

【问题讨论】:

    标签: android-volley networkimageview subsampling


    【解决方案1】:

    我不知道是否有人会觉得它有用,但以防万一。

    我的要求是我需要将图像加载到我需要位图的自定义图像视图中。

    我使用了单例类。

    public class MyVolleySingleton {
    
    private static MyVolleySingleton mInstance;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    private static Context mCtx;
    
    private MyVolleySingleton(Context context) {
        mCtx = context;
        mRequestQueue = getRequestQueue();
        mImageLoader = getImageLoaderRequest();
    }
    private ImageLoader getImageLoaderRequest() {
        return 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 MyVolleySingleton getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new MyVolleySingleton(context);
        }
        return mInstance;
    }
    
    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            // getApplicationContext() is key, it keeps you from leaking the
            // Activity or BroadcastReceiver if someone passes one in.
            mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
        }
        return mRequestQueue;
    }
    
    public <T> void addToRequestQueue(Request<T> req) {
        getRequestQueue().add(req);
    }
    public ImageLoader getImageLoader() {
        return mImageLoader;
    }
    
    }
    

    现在在主要活动中,我使用了传递要加载的图像的 url 的函数。

        private void loadImage(String plan_url) {
            String url = plan_url;
            if (url.equals("")) {
                Toast.makeText(this, "<Error Message>", Toast.LENGTH_LONG).show();
                return;
            }
    
            // Retrieves an image specified by the URL, displays it in the UI.
            ImageRequest request = new ImageRequest(url,
                    new Response.Listener<Bitmap>() {
                        @Override
                        public void onResponse(Bitmap bitmap) {
                            imageView.setImage(ImageSource.bitmap(bitmap));
    
                        }
                    }, 0, 0, null,
                    new Response.ErrorListener() {
                        public void onErrorResponse(VolleyError error) {
                            imageView.setImage(ImageSource.resource(R.drawable.demo1));
                        }
                    });
    // Access the RequestQueue through your singleton class.
            MyVolleySingelton.getInstance(this).addToRequestQueue(request);
        }
    

    Volley developer docs

    【讨论】:

      猜你喜欢
      • 2015-03-17
      • 1970-01-01
      • 2013-05-28
      • 1970-01-01
      • 2017-12-17
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多