谷歌官网的Volley网络框架实战②--ImageRequest、ImageLoader、NetworkImageView加载图片


介绍


Volley解析网络图片时候的图片加载有三种方法,各自不同,用法也不一样。那么我们Go go go!下面的工程都是建立在我的第一篇之上的,如有不懂,翻看看看。


ImageRequest加载图片

这里写图片描述


看下我们的布局文件。一个按钮,一个ImageView来显示下载的图片。


             <Button
                android:id="@+id/btn_ImageRequest"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="#34c083"
                android:text="ImageRequest加载图片"
                android:textColor="@android:color/white" />
            <ImageView
                android:id="@+id/iv_ImageRequest"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

该方法中要求传进去一个地图的网址URL,我这里用的是这张百度图片。
注意我们这里把这个地址拿到,即“https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png


这里写图片描述


代码如下:

private void getImageView_ImageRequest() {
        //创建一个请求队列
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        //创建一个图片请求
        ImageRequest imageRequest = new ImageRequest(URL_ImageRequest, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap bitmap) {
                //成功请求之后将控件设置为回调回来的 bitmap
                imageView_ImageRequest.setImageBitmap(bitmap);
            }   // 0,0表示下载之后默认图片大小,Bitmap.Config.RGB_565表示下载图片的质量
        }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(MainActivity.this,"错误请求图片:"+volleyError,Toast.LENGTH_LONG).show();
            }
        });
        //把请求添加到队列中
        requestQueue.add(imageRequest);
    } 

ImageLoader加载图片


布局:



<LinearLayout
        android:gravity="center"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/btn_LoaderRequest"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:background="#34c083"
            android:text="ImageLoader加载图片"
            android:textColor="@android:color/white" />
        <ImageView
            android:layout_marginTop="20dp"
            android:id="@+id/iv_ImageLoader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

代码:


  private void getImageView_ImageLoader() {

        //创建队列
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        //创建一个Loader队列请求 其中第二个参数是是否磁盘缓存,这里返回null,不做缓存
        ImageLoader imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String s) {
                return null;
            }
            @Override
            public void putBitmap(String s, Bitmap bitmap) {

            }
        });

     //加载图片 第一个是把成功从网络拿到的图片放进这个控件里面。第二个是失败时候显示的图片,第三个是加载时候的图片
        ImageLoader.ImageListener imageListen  =imageLoader.getImageListener(imageView_Loaderimage,R.mipmap.ic_launcher,R.mipmap.ic_launcher);
        imageLoader.get(URL_ImageRequest, imageListen);
    }

NetwordImageView加载网络图片


这个是Volley下的一个显示控件,所以我们要引用它:


        <Button
            android:id="@+id/btn_netWordkImageView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:background="#34c083"
            android:text="netWordkImageView加载图片"
            android:textColor="@android:color/white" />

        <com.android.volley.toolbox.NetworkImageView
            android:id="@+id/netWordkImageView"
            android:layout_height="200dp"
            android:layout_width="200dp">

        </com.android.volley.toolbox.NetworkImageView>

代码: 我们绑定时候是 这样的:

private NetworkImageView netWordkImageView ;
 netWordkImageView=(NetworkImageView)findViewById(R.id.netWordkImageView);

代码里面我新建一个缓存类实现ImageLoader.ImageCache ()的接口,具体代码内容看下载源码吧。

private void getNetWorkImageView() {
        //请求一个队列
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);

        //请求一个队列
        ImageLoader imageLoader = new ImageLoader(requestQueue, new BitmapCache());

        //设置默认图片
        netWordkImageView.setDefaultImageResId(R.mipmap.ic_launcher);

        //加载错误的图片
        netWordkImageView.setErrorImageResId(R.mipmap.ic_launcher);

        //加载图片
        netWordkImageView.setImageUrl(URL_ImageRequest,imageLoader);
    }

Volley框架基本学完了,是不是感觉很容易实现代码呢。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半颗心脏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值