1、ImageRequest用法和StringRequest一样,主要分为3步:
(1)、实例化一个RequestQueue对象
(2)、设置ImageRequest对象参数,并将ImageRequest对象加入RequestQueue队列
(3)、执行start()方法
2、ImageLoader用法
(1)、实例化一个RequestQueue对象
(2)、实例化一个ImageLoader对象
(3)、实例化一个ImageListener对象
(4)、执行ImageLoader对象的get()方法
3、NetworkImageView用法
(1)、在布局文件中声明NetworkImageView
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.android.volley.toolbox.NetworkImageView android:id="@+id/iv_show_image2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
(2)、设置默认图片setDefaultImageResId()
(3)、设置出错图片setErrorImageResId()
(4)、设置URL和ImageLoader对象
4、完整代码
public class ImageActivity extends Activity { private ImageView mIvSHow; private ImageView mIvSHow1; private RequestQueue queue; private NetworkImageView mIvShow2; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_image); initView(); } @SuppressWarnings("static-access") public void initView() { mIvSHow = (ImageView) findViewById(R.id.iv_show_image); mIvSHow1 = (ImageView) findViewById(R.id.iv_show_image1); mIvShow2 = (NetworkImageView) findViewById(R.id.iv_show_image2); /** ImageRequest用法 */ queue = Volley.newRequestQueue(getBaseContext()); queue.add(imageRequest); queue.start(); /** ImageLoader用法 */ ImageLoader imageLoader = new ImageLoader(queue, new ImageCache() { @Override public Bitmap getBitmap(String url) { // TODO Auto-generated method stub return null; } @Override public void putBitmap(String url, Bitmap bitmap) { // TODO Auto-generated method stub } }); ImageListener imageListener = imageLoader.getImageListener(mIvSHow1, R.drawable.ic_launcher, R.drawable.he); imageLoader.get("https://www.baidu.com/img/bd_logo1.png", imageListener); /** NetworkImageView用法 */ mIvShow2.setDefaultImageResId(R.drawable.ic_launcher); mIvShow2.setErrorImageResId(R.drawable.ic_launcher); mIvShow2.setImageUrl("https://www.baidu.com/img/bd_logo1.png",imageLoader); } /** ImageRequest请求 */ public ImageRequest imageRequest = new ImageRequest( "https://www.baidu.com/img/bd_logo1.png", new Listener<Bitmap>() { @Override public void onResponse(Bitmap response) { // TODO Auto-generated method stub mIvSHow.setImageBitmap(response); } }, 0, 0, Config.RGB_565, new ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // TODO Auto-generated method stub Log.e("ImageRequest", error.toString()); } }); }
5、不要忘记加入网络访问权限
<uses-permission android:name="android.permission.INTERNET"/>
6、参考博文:
http://blog.csdn.net/guolin_blog/article/details/17482095/