com.android.volley.toolbox.ImageRequest Java Examples

The following examples show how to use com.android.volley.toolbox.ImageRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MainActivity.java    From BlogDemo with Apache License 2.0 6 votes vote down vote up
public void setImageUrl(String url){
            RequestQueue mQueue = Volley.newRequestQueue(getContext());
            ImageRequest imageRequest = new ImageRequest(
                    url,
                    new Response.Listener<Bitmap>() {
                        @Override
                        public void onResponse(Bitmap response) {
                            imageView.setImage(ImageSource.bitmap(response));

                        }
                    }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
//                    // 设置失败图
//                    Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
//                    imgDrawable.setDrawable(drawable);
//                    Log.d("tAG", "加载失败");
//                    tv.postInvalidate();    // 刷新view
                }
            });
            mQueue.add(imageRequest);
        }
 
Example #2
Source File: ProcessingFragment.java    From timelapse-sony with GNU General Public License v3.0 6 votes vote down vote up
private void showImage(String url) {
    ImageRequest request = new ImageRequest(url,
            new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(final Bitmap bitmap) {
                    if (getActivity() == null) return;
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mImageReviewView.setImageBitmap(bitmap);
                        }
                    });
                }
            }, 0, 0, ImageView.ScaleType.CENTER_INSIDE, null,
            new Response.ErrorListener() {
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                }
            });
    mImagesQueue.add(request);
}
 
Example #3
Source File: ApiRequest.java    From syncthing-android with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Opens the connection, then returns success status and response bitmap.
 */
void makeImageRequest(Uri uri, @Nullable OnImageSuccessListener imageListener,
                      @Nullable OnErrorListener errorListener) {
    ImageRequest imageRequest =  new ImageRequest(uri.toString(), bitmap -> {
        if (imageListener != null) {
            imageListener.onImageSuccess(bitmap);
        }
    }, 0, 0, ImageView.ScaleType.CENTER, Bitmap.Config.RGB_565, volleyError -> {
        if(errorListener != null) {
            errorListener.onError(volleyError);
        }
        Log.d(TAG, "onErrorResponse: " + volleyError);
    }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            return ImmutableMap.of(HEADER_API_KEY, mApiKey);
        }
    };

    getVolleyQueue().add(imageRequest);
}
 
Example #4
Source File: ImagesActivity.java    From volley_demo with Apache License 2.0 6 votes vote down vote up
private void get_image()
    {
        textview.setText("获取图片开始了");
        imageView.setImageBitmap(null);
        networkImageView.setImageBitmap(null);
        // 错误url:String url = "ip.taobao.com/service/getIpInfo.php?ip=202.96.128.166"
//        String url = "http://d.hiphotos.baidu.com/image/h%3D200/sign=31db160e2a738bd4db21b531918a876c/6a600c338744ebf8f1230decddf9d72a6159a797.jpg";
        String url = "http://h.hiphotos.baidu.com/image/pic/item/d53f8794a4c27d1e3584e91b1fd5ad6edcc4384b.jpg";

        ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                textview.setText("获取图片成功");
                imageView.setImageBitmap(response);
            }
        }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                textview.setText("获取图片错误");
                imageView.setImageBitmap(null);
            }
        });

        MyApplication.getHttpQueues().add(request);

    }
 
Example #5
Source File: MainActivity.java    From android-advanced-light with MIT License 6 votes vote down vote up
private void UseImageRequest() {
    ImageRequest imageRequest = new ImageRequest(
            "http://img.my.csdn.net/uploads/201603/26/1458988468_5804.jpg",
            new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(Bitmap response) {
                    iv_image.setImageBitmap(response);
                }
            }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            iv_image.setImageResource(R.drawable.ico_default);
        }
    });
    mQueue.add(imageRequest);
}
 
Example #6
Source File: MainActivity.java    From Study_Android_Demo with Apache License 2.0 6 votes vote down vote up
public void btnClick4(View view){
    ImageRequest request = new ImageRequest("http://cdn8.staztic.com/app/a/5993/5993203/volley-17-l-280x280.png", new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(Bitmap response) {
            iv.setImageBitmap(response);
        }
    }, 200, 200, ImageView.ScaleType.FIT_XY, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    queue.add(request);

}
 
Example #7
Source File: BaseLearningModelImpl.java    From allenglish with Apache License 2.0 6 votes vote down vote up
@Override
        public void onADLoaded(final List<NativeADDataRef> lst) {
            if (lst == null || lst.isEmpty()) {
                return;
            }
//            DisplayMetrics metrics = new DisplayMetrics();
//            ((AppCompatActivity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
//            int width = metrics.widthPixels;
            RequestQueue mQueue = VolleySingleton.getInstance().getRequestQueue();
            ImageRequest imageRequest = new ImageRequest(
                    lst.get(0).getImage(),
                    new Response.Listener<Bitmap>() {
                        @Override
                        public void onResponse(Bitmap response) {
                            listener.onGetAdsImageSuccess(response, iflyNativeAd2, lst.get(0));
                        }
                    }, 0, 0, ImageView.ScaleType.FIT_XY, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });
            mQueue.add(imageRequest);
        }
 
Example #8
Source File: RegisterActivity.java    From kute with Apache License 2.0 5 votes vote down vote up
public void getImage(String url){
        ImageRequest ir = new ImageRequest(url, new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(Bitmap response) {
            ImageHandler.saveImageToprefrence(getSharedPreferences(ImageHandler.MainKey,MODE_PRIVATE),response);
            ImageView iv=(ImageView)findViewById(R.id.imageView);
            iv.setImageBitmap(response);
        }
    }, 0, 0, null, null);
    rq.add(ir);

}
 
Example #9
Source File: MenusAdapter.java    From elemeimitate with Apache License 2.0 5 votes vote down vote up
@Override
public View getView(int position, View convertView, ViewGroup parent) {
	final ViewHolder viewHolder ;
	if(convertView == null){
		convertView = inflater.inflate(R.layout.item_menu_content, parent, false);
		viewHolder = new ViewHolder();
		viewHolder.iv_image = (ImageView)convertView.findViewById(R.id.item_menu_content_img);
		viewHolder.tv_menusName = (TextView)convertView.findViewById(R.id.item_menu_content_title);
		viewHolder.tv_price = (TextView)convertView.findViewById(R.id.item_menu_content_price);
		
		convertView.setTag(viewHolder);
	}else{
		viewHolder = (ViewHolder)convertView.getTag();
	}

	//创建一个RequestQueue对象
	RequestQueue requestQueue = Volley.newRequestQueue(context);
	     	
    //创建ImageRequest对象
	ImageRequest imageRequest = new ImageRequest(
			Constant.URL_WEB_SERVER+datas.get(position).get("menusImagePath").toString(),//url
	    	new Response.Listener<Bitmap>() {//监听器Listener
				@Override
	    		public void onResponse(Bitmap response) {
	    			viewHolder.iv_image.setImageBitmap(response);
	    		}
	    		//参数3、4表示图片宽高,Bitmap.Config.ARGB_8888表示图片每个像素占据4个字节大小
	    	}, 0, 0, Config.ARGB_8888, new Response.ErrorListener() {//图片加载请求失败的回调Listener
	    			@Override
	    			public void onErrorResponse(VolleyError error) {
	    				viewHolder.iv_image.setImageResource(R.drawable.ic_normal_pic);
	    			}
	    	});
	//将ImageRequest加载到Queue
	 requestQueue.add(imageRequest);
	    
	viewHolder.tv_menusName.setText(datas.get(position).get("menuName").toString());
	viewHolder.tv_price.setText("¥"+datas.get(position).get("total_price").toString());
	return convertView;
}
 
Example #10
Source File: PictureFragment.java    From Tweetin with Apache License 2.0 5 votes vote down vote up
private void load() {
    progressWheel.setVisibility(View.VISIBLE);
    reloadView.setVisibility(View.GONE);
    pictureView.setVisibility(View.GONE);

    ImageRequest imageRequest = new ImageRequest(
            pictureURL,
            new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(Bitmap bitmap) {
                    originBitmap = bitmap;

                    pictureView.setImageBitmap(bitmap);
                    PhotoViewAttacher photoViewAttacher = new PhotoViewAttacher(pictureView);
                    photoViewAttacher.setZoomable(true);
                    photoViewAttacher.update();

                    progressWheel.setVisibility(View.GONE);
                    reloadView.setVisibility(View.GONE);
                    pictureView.setVisibility(View.VISIBLE);
                }
            },
            0,
            0,
            Bitmap.Config.ARGB_8888,
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    progressWheel.setVisibility(View.GONE);
                    reloadView.setVisibility(View.VISIBLE);
                    pictureView.setVisibility(View.GONE);
                }
            }
    );
    requestQueue.add(imageRequest);
}
 
Example #11
Source File: ImageRequestAdapter.java    From android-open-project-demo with Apache License 2.0 5 votes vote down vote up
@Override
void setImage(final ImageView imageView, String imageUrl) {
	
	//设置空图片
	imageView.setImageResource(R.drawable.ic_empty);
	
	//取消这个ImageView已有的请求
	VolleyUtil.getQueue(context).cancelAll(imageView);
	
	ImageRequest request=new ImageRequest(StringUtil.preUrl(imageUrl), new Listener<Bitmap>() {

		@Override
		public void onResponse(Bitmap bitmap) {
			imageView.setImageBitmap(bitmap);
		}
	}, 0, 0, Config.RGB_565, new ErrorListener() {

		@Override
		public void onErrorResponse(VolleyError arg0) {
			imageView.setImageResource(R.drawable.ic_empty);
		}
	});
	
	request.setTag(imageView);
	
	VolleyUtil.getQueue(this.context).add(request);
	
}
 
Example #12
Source File: ImageLoader.java    From android-project-wo2b with Apache License 2.0 4 votes vote down vote up
/**
 * Issues a bitmap request with the given URL if that image is not available
 * in the cache, and returns a bitmap container that contains all of the data
 * relating to the request (as well as the default image if the requested
 * image is not available).
 * @param requestUrl The url of the remote image
 * @param imageListener The listener to call when the remote image is loaded
 * @param maxWidth The maximum width of the returned image.
 * @param maxHeight The maximum height of the returned image.
 * @return A container object that contains all of the properties of the request, as well as
 *     the currently available image (default if remote is not loaded).
 */
public ImageContainer get(String requestUrl, ImageListener imageListener,
        int maxWidth, int maxHeight) {
    // only fulfill requests that were initiated from the main thread.
    throwIfNotOnMainThread();

    final String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight);

    // Try to look up the request in the cache of remote images.
    Bitmap cachedBitmap = mCache.getBitmap(cacheKey);
    if (cachedBitmap != null) {
        // Return the cached bitmap.
        ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null);
        imageListener.onResponse(container, true);
        return container;
    }

    // The bitmap did not exist in the cache, fetch it!
    ImageContainer imageContainer =
            new ImageContainer(null, requestUrl, cacheKey, imageListener);

    // Update the caller to let them know that they should use the default bitmap.
    imageListener.onResponse(imageContainer, true);

    // Check to see if a request is already in-flight.
    BatchedImageRequest request = mInFlightRequests.get(cacheKey);
    if (request != null) {
        // If it is, add this request to the list of listeners.
        request.addContainer(imageContainer);
        return imageContainer;
    }

    // The request is not already in flight. Send the new request to the network and
    // track it.
    Request<?> newRequest =
        new ImageRequest(requestUrl, new Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                onGetImageSuccess(cacheKey, response);
            }
        }, maxWidth, maxHeight,
        Config.RGB_565, new ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                onGetImageError(cacheKey, error);
            }
        });

    mRequestQueue.add(newRequest);
    mInFlightRequests.put(cacheKey,
            new BatchedImageRequest(newRequest, imageContainer));
    return imageContainer;
}
 
Example #13
Source File: ImageLoader.java    From android-common-utils with Apache License 2.0 4 votes vote down vote up
/**
 * Issues a bitmap request with the given URL if that image is not available
 * in the cache, and returns a bitmap container that contains all of the data
 * relating to the request (as well as the default image if the requested
 * image is not available).
 * @param requestUrl The url of the remote image
 * @param imageListener The listener to call when the remote image is loaded
 * @param maxWidth The maximum width of the returned image.
 * @param maxHeight The maximum height of the returned image.
 * @return A container object that contains all of the properties of the request, as well as
 *     the currently available image (default if remote is not loaded).
 */
public ImageContainer get(String requestUrl, ImageListener imageListener,
        int maxWidth, int maxHeight) {
    // only fulfill requests that were initiated from the main thread.
    throwIfNotOnMainThread();

    final String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight);

    // Try to look up the request in the cache of remote images.
    Bitmap cachedBitmap = mCache.getBitmap(cacheKey);
    if (cachedBitmap != null) {
        // Return the cached bitmap.
        ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null);
        imageListener.onResponse(container, true);
        return container;
    }

    // The bitmap did not exist in the cache, fetch it!
    ImageContainer imageContainer =
            new ImageContainer(null, requestUrl, cacheKey, imageListener);

    // Update the caller to let them know that they should use the default bitmap.
    imageListener.onResponse(imageContainer, true);

    // Check to see if a request is already in-flight.
    BatchedImageRequest request = mInFlightRequests.get(cacheKey);
    if (request != null) {
        // If it is, add this request to the list of listeners.
        request.addContainer(imageContainer);
        return imageContainer;
    }

    // The request is not already in flight. Send the new request to the network and
    // track it.
    Request<?> newRequest =
        new ImageRequest(requestUrl, new Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                onGetImageSuccess(cacheKey, response);
            }
        }, maxWidth, maxHeight,
        Config.RGB_565, new ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                onGetImageError(cacheKey, error);
            }
        });

    mRequestQueue.add(newRequest);
    mInFlightRequests.put(cacheKey,
            new BatchedImageRequest(newRequest, imageContainer));
    return imageContainer;
}
 
Example #14
Source File: BusinessInfoAdapter.java    From elemeimitate with Apache License 2.0 4 votes vote down vote up
@Override
public View getView(int position, View convertView, ViewGroup parent) {
	final ViewHolder viewHolder ;
	if(convertView == null){
		convertView = inflater.inflate(R.layout.item_list_restaurant, parent, false);
		viewHolder = new ViewHolder();
		viewHolder.iv_logo = (ImageView)convertView.findViewById(R.id.logo_iv);
		viewHolder.tv_businessName = (TextView)convertView.findViewById(R.id.business_name_tv);
		viewHolder.tv_sendOutPrice = (TextView)convertView.findViewById(R.id.sendOutPrice_tv);
		viewHolder.tv_distributionPrice = (TextView)convertView.findViewById(R.id.distributionPrice_tv);
		viewHolder.tv_shopHours = (TextView)convertView.findViewById(R.id.shopHours_tv);
		
		convertView.setTag(viewHolder);
	}else{
		viewHolder = (ViewHolder)convertView.getTag();
	}

	//创建一个RequestQueue对象
	RequestQueue requestQueue = Volley.newRequestQueue(context);
	     	
    //创建ImageRequest对象
	ImageRequest imageRequest = new ImageRequest(
			Constant.URL_WEB_SERVER+datas.get(position).get("logo").toString(),//url
	    	new Response.Listener<Bitmap>() {//监听器Listener
				@Override
	    		public void onResponse(Bitmap response) {
	    			viewHolder.iv_logo.setImageBitmap(response);
	    		}
	    		//参数3、4表示图片宽高,Bitmap.Config.ARGB_8888表示图片每个像素占据4个字节大小
	    	}, 0, 0, Config.ARGB_8888, new Response.ErrorListener() {//图片加载请求失败的回调Listener
	    			@Override
	    			public void onErrorResponse(VolleyError error) {
	    				viewHolder.iv_logo.setImageResource(R.drawable.ic_normal_pic);
	    			}
	    	});
	//将ImageRequest加载到Queue
	 requestQueue.add(imageRequest);
	    
	viewHolder.tv_businessName.setText(datas.get(position).get("businessName").toString());
	viewHolder.tv_sendOutPrice.setText("¥"+datas.get(position).get("sendOutPrice").toString());
	viewHolder.tv_distributionPrice.setText("配送价:"+datas.get(position).get("distributionPrice").toString());
	viewHolder.tv_shopHours.setText("送货时间:"+datas.get(position).get("shopHours").toString());
	return convertView;
}
 
Example #15
Source File: NetworkFragment.java    From okulus with Apache License 2.0 4 votes vote down vote up
private void loadImage(final ImageView imageView, final int position) {

            final String imageurl = (String) getItem(position);
            switch (getItemViewType(position)) {

                //Volley - ImageRequest
                case 0: {
                    final ImageRequest request = new ImageRequest(
                            imageurl,
                            new Response.Listener<Bitmap>() {
                                @Override
                                public void onResponse(Bitmap response) {
                                    imageView.setImageBitmap(response);
                                }
                            },
                            dpToPx(128),
                            dpToPx(96),
                            null,
                            new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {

                                }
                            }
                    );

                    mRequestQueue.add(request);
                    break;
                }

                //Volley - NetworkImageView - In this case, NetworkImageView has been modified to extend OkulusImageView
                case 1: {

                    final NetworkImageView networkImageView = (NetworkImageView) imageView;
                    networkImageView.setImageUrl(imageurl, mImageLoader);
                    break;
                }

                //Picasso
                case 2: {

                    Picasso.with(imageView.getContext())
                            .load(imageurl)
                            .resize(dpToPx(128), dpToPx(96))
                            .centerCrop()
                            .into(new Target() {
                                @Override
                                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                                    imageView.setImageBitmap(bitmap);
                                }

                                @Override
                                public void onBitmapFailed(Drawable errorDrawable) {

                                }

                                @Override
                                public void onPrepareLoad(Drawable placeHolderDrawable) {

                                }
                            });
                    break;
                }

                //Glide
                case 3: {

                    Glide.with(imageView.getContext())
                            .load(imageurl)
                            .asBitmap()
                            //.override(dpToPx(128), dpToPx(96))
                            .centerCrop()
                            .into(new ViewTarget<ImageView, Bitmap>(imageView) {
                                @Override
                                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                    imageView.setImageBitmap(resource);
                                }
                            });
                    break;
                }

                // Universal Image Loader
                case 4: {

                    ImageSize targetSize = new ImageSize(dpToPx(96), dpToPx(128));
                    mUniversalImageLoader.loadImage(imageurl, targetSize, new SimpleImageLoadingListener() {

                        @Override
                        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                            imageView.setImageBitmap(loadedImage);
                        }
                    });
                    break;
                }

            }
        }
 
Example #16
Source File: ImageLoader.java    From FeedListViewDemo with MIT License 4 votes vote down vote up
/**
 * Issues a bitmap request with the given URL if that image is not available
 * in the cache, and returns a bitmap container that contains all of the data
 * relating to the request (as well as the default image if the requested
 * image is not available).
 * @param requestUrl The url of the remote image
 * @param imageListener The listener to call when the remote image is loaded
 * @param maxWidth The maximum width of the returned image.
 * @param maxHeight The maximum height of the returned image.
 * @return A container object that contains all of the properties of the request, as well as
 *     the currently available image (default if remote is not loaded).
 */
public ImageContainer get(String requestUrl, ImageListener imageListener,
        int maxWidth, int maxHeight) {
    // only fulfill requests that were initiated from the main thread.
    throwIfNotOnMainThread();

    final String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight);

    // Try to look up the request in the cache of remote images.
    Bitmap cachedBitmap = mCache.getBitmap(cacheKey);
    if (cachedBitmap != null) {
        // Return the cached bitmap.
        ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null);
        imageListener.onResponse(container, true);
        return container;
    }

    // The bitmap did not exist in the cache, fetch it!
    ImageContainer imageContainer =
            new ImageContainer(null, requestUrl, cacheKey, imageListener);

    // Update the caller to let them know that they should use the default bitmap.
    imageListener.onResponse(imageContainer, true);

    // Check to see if a request is already in-flight.
    BatchedImageRequest request = mInFlightRequests.get(cacheKey);
    if (request != null) {
        // If it is, add this request to the list of listeners.
        request.addContainer(imageContainer);
        return imageContainer;
    }

    // The request is not already in flight. Send the new request to the network and
    // track it.
    Request<?> newRequest =
        new ImageRequest(requestUrl, new Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                onGetImageSuccess(cacheKey, response);
            }
        }, maxWidth, maxHeight,
        Config.RGB_565, new ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                onGetImageError(cacheKey, error);
            }
        });

    mRequestQueue.add(newRequest);
    mInFlightRequests.put(cacheKey,
            new BatchedImageRequest(newRequest, imageContainer));
    return imageContainer;
}
 
Example #17
Source File: ImageLoader.java    From android_tv_metro with Apache License 2.0 4 votes vote down vote up
/**
 * Issues a bitmap request with the given URL if that image is not available
 * in the cache, and returns a bitmap container that contains all of the data
 * relating to the request (as well as the default image if the requested
 * image is not available).
 * @param requestUrl The url of the remote image
 * @param imageListener The listener to call when the remote image is loaded
 * @param maxWidth The maximum width of the returned image.
 * @param maxHeight The maximum height of the returned image.
 * @return A container object that contains all of the properties of the request, as well as
 *     the currently available image (default if remote is not loaded).
 */
public ImageContainer get(String requestUrl, ImageListener imageListener,
        int maxWidth, int maxHeight) {
    // only fulfill requests that were initiated from the main thread.
    throwIfNotOnMainThread();

    final String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight);

    // Try to look up the request in the cache of remote images.
    Bitmap cachedBitmap = mCache.getBitmap(cacheKey);
    if (cachedBitmap != null) {
        // Return the cached bitmap.
        ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null);
        imageListener.onResponse(container, true);
        return container;
    }

    // The bitmap did not exist in the cache, fetch it!
    ImageContainer imageContainer =
            new ImageContainer(null, requestUrl, cacheKey, imageListener);

    // Update the caller to let them know that they should use the default bitmap.
    imageListener.onResponse(imageContainer, true);

    // Check to see if a request is already in-flight.
    BatchedImageRequest request = mInFlightRequests.get(cacheKey);
    if (request != null) {
        // If it is, add this request to the list of listeners.
        request.addContainer(imageContainer);
        return imageContainer;
    }

    // The request is not already in flight. Send the new request to the network and
    // track it.
    Request<?> newRequest =
        new ImageRequest(requestUrl, new Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                onGetImageSuccess(cacheKey, response);
            }
        }, maxWidth, maxHeight,
        Config.RGB_565, new ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                onGetImageError(cacheKey, error);
            }
        });

    mRequestQueue.add(newRequest);
    mInFlightRequests.put(cacheKey,
            new BatchedImageRequest(newRequest, imageContainer));
    return imageContainer;
}
 
Example #18
Source File: OrderAdapter.java    From elemeimitate with Apache License 2.0 4 votes vote down vote up
@Override
public View getView(int position, View convertView, ViewGroup parent) {
	final ViewHolder viewHolder ;
	if(convertView == null){
		convertView = inflater.inflate(R.layout.item_list_order, parent, false);
		viewHolder = new ViewHolder();
		viewHolder.iv_image = (ImageView)convertView.findViewById(R.id.order_iv);
		viewHolder.tv_menusName = (TextView)convertView.findViewById(R.id.order_menusName_tv);
		viewHolder.tv_amount = (TextView)convertView.findViewById(R.id.order_amount_tv);
		viewHolder.tv_time = (TextView)convertView.findViewById(R.id.order_time_tv);
		viewHolder.tv_price = (TextView)convertView.findViewById(R.id.order_price_tv);
		viewHolder.tv_status = (TextView)convertView.findViewById(R.id.order_status_tv);
		
		convertView.setTag(viewHolder);
	}else{
		viewHolder = (ViewHolder)convertView.getTag();
	}

	//创建一个RequestQueue对象
	RequestQueue requestQueue = Volley.newRequestQueue(context);
	     	
    //创建ImageRequest对象
	ImageRequest imageRequest = new ImageRequest(
			Constant.URL_WEB_SERVER+datas.get(position).get("picPath").toString(),//url
	    	new Response.Listener<Bitmap>() {//监听器Listener
				@Override
	    		public void onResponse(Bitmap response) {
	    			viewHolder.iv_image.setImageBitmap(response);
	    		}
	    		//参数3、4表示图片宽高,Bitmap.Config.ARGB_8888表示图片每个像素占据4个字节大小
	    	}, 0, 0, Config.ARGB_8888, new Response.ErrorListener() {//图片加载请求失败的回调Listener
	    			@Override
	    			public void onErrorResponse(VolleyError error) {
	    				viewHolder.iv_image.setImageResource(R.drawable.ic_normal_pic);
	    			}
	    	});
	//将ImageRequest加载到Queue
	 requestQueue.add(imageRequest);
	    
	viewHolder.tv_menusName.setText(datas.get(position).get("name").toString());
	viewHolder.tv_amount.setText("×"+datas.get(position).get("amount").toString());
	viewHolder.tv_time.setText("订单时间:"+datas.get(position).get("order_time").toString());
	viewHolder.tv_price.setText("总价:¥"+datas.get(position).get("total_price").toString());
	viewHolder.tv_status.setText(datas.get(position).get("status").toString());
	return convertView;
}
 
Example #19
Source File: ImageLoader.java    From WayHoo with Apache License 2.0 4 votes vote down vote up
/**
 * Issues a bitmap request with the given URL if that image is not available
 * in the cache, and returns a bitmap container that contains all of the data
 * relating to the request (as well as the default image if the requested
 * image is not available).
 * @param requestUrl The url of the remote image
 * @param imageListener The listener to call when the remote image is loaded
 * @param maxWidth The maximum width of the returned image.
 * @param maxHeight The maximum height of the returned image.
 * @return A container object that contains all of the properties of the request, as well as
 *     the currently available image (default if remote is not loaded).
 */
public ImageContainer get(String requestUrl, ImageListener imageListener,
        int maxWidth, int maxHeight) {
    // only fulfill requests that were initiated from the main thread.
    throwIfNotOnMainThread();

    final String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight);

    // Try to look up the request in the cache of remote images.
    Bitmap cachedBitmap = mCache.getBitmap(cacheKey);
    if (cachedBitmap != null) {
        // Return the cached bitmap.
        ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null);
        imageListener.onResponse(container, true);
        return container;
    }

    // The bitmap did not exist in the cache, fetch it!
    ImageContainer imageContainer =
            new ImageContainer(null, requestUrl, cacheKey, imageListener);

    // Update the caller to let them know that they should use the default bitmap.
    imageListener.onResponse(imageContainer, true);

    // Check to see if a request is already in-flight.
    BatchedImageRequest request = mInFlightRequests.get(cacheKey);
    if (request != null) {
        // If it is, add this request to the list of listeners.
        request.addContainer(imageContainer);
        return imageContainer;
    }

    // The request is not already in flight. Send the new request to the network and
    // track it.
    Request<?> newRequest =
        new ImageRequest(requestUrl, new Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                onGetImageSuccess(cacheKey, response);
            }
        }, maxWidth, maxHeight,
        Config.RGB_565, new ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                onGetImageError(cacheKey, error);
            }
        });

    mRequestQueue.add(newRequest);
    mInFlightRequests.put(cacheKey,
            new BatchedImageRequest(newRequest, imageContainer));
    return imageContainer;
}