当前位置: 移动技术网 > IT编程>移动开发>Android > Android框架Volley之:利用Imageloader和NetWorkImageView加载图片

Android框架Volley之:利用Imageloader和NetWorkImageView加载图片

2019年05月17日  | 移动技术网IT编程  | 我要评论

张祖林,祁连山招标网,废后为妃19楼

首先我们在项目中导入这个框架:

implementation 'com.mcxiaoke.volley:library:1.0.19'

在androidmanifest文件当中添加网络权限:

<uses-permission android:name="android.permission.internet"/>

下面是我们的首页布局:
在这个布局当中我们将volley框架的所有功能都做成了一个按钮,按下按钮之后就会在“显示结果”下面显示结果,显示结果下面使用了一个scrollview,并在scrollview下面嵌套了一个textview和imageview,用于把我们加载成功之后的图片和文字进行显示。

下面是首页布局的代码:

 
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".mainactivity">
<button
    android:id="@+id/get"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="get请求"/>
    <button
        android:id="@+id/post"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="post请求"/>
    <button
        android:id="@+id/json"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请求json"/>
    <button
        android:id="@+id/imagerquest"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="imagerquest加载图片"/>
    <button
        android:id="@+id/imageloader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="imageloader加载图片"/>
    <button
        android:id="@+id/networkimageview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="networkimageview加载图片"/>
    <textview
        android:text="显示结果"
        android:textsize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <imageview
        android:visibility="gone"
        android:id="@+id/iv_volley"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <com.android.volley.toolbox.networkimageview
        android:id="@+id/network"
        android:visibility="gone"
        android:layout_width="200dp"
        android:layout_height="200dp" />
   <scrollview
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <textview
           android:id="@+id/tv_volley_result"
           android:layout_width="match_parent"
           android:layout_height="match_parent" />



   </scrollview>
</linearlayout>
 

图片缓存类bitmapcache.java代码:

import android.graphics.bitmap;
import android.support.v4.util.lrucache;

import com.android.volley.toolbox.imageloader;

public class bitmapcache implements imageloader.imagecache {

    private lrucache<string, bitmap> mcache;

    public bitmapcache() {
        int maxsize = 10 * 1024 * 1024;// 10m
        mcache = new lrucache<string, bitmap>(maxsize) {
            @override
            protected int sizeof(string key, bitmap bitmap) {
                return bitmap.getrowbytes() * bitmap.getheight();
            }
        };
    }

    @override
    public bitmap getbitmap(string url) {
        return mcache.get(url);
    }

    @override
    public void putbitmap(string url, bitmap bitmap) {
        mcache.put(url, bitmap);
    }


}

 

主活动业务逻辑实现代码:

import android.graphics.bitmap;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.button;
import android.widget.imageview;
import android.widget.textview;

import com.android.volley.authfailureerror;
import com.android.volley.request;
import com.android.volley.requestqueue;
import com.android.volley.response;
import com.android.volley.volleyerror;
import com.android.volley.toolbox.imageloader;
import com.android.volley.toolbox.imagerequest;
import com.android.volley.toolbox.jsonarrayrequest;
import com.android.volley.toolbox.jsonobjectrequest;
import com.android.volley.toolbox.networkimageview;
import com.android.volley.toolbox.stringrequest;
import com.android.volley.toolbox.volley;

import org.json.jsonobject;

import java.util.hashmap;
import java.util.map;

public class mainactivity extends appcompatactivity {
    private button get;
    private button post;
    private button json;
    private button imagerequest;
    private  button imageload;
    private  button networkimageview;

    private imageview iv;
    private networkimageview network;
    private textview tv_volley_result;
    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);
        initview();
        initlistener();

    }
    public void initview()//把需要初始化的控件的逻辑都写在这里是一个很好的编程范式
    {

        get=findviewbyid(r.id.get);
        post=findviewbyid(r.id.post);
        json=findviewbyid(r.id.json);
        imagerequest=findviewbyid(r.id.imagerquest);
        imageload=findviewbyid(r.id.imageloader);
        networkimageview=findviewbyid(r.id.networkimageview);
        iv=findviewbyid(r.id.iv_volley);
        network=findviewbyid(r.id.network);
        tv_volley_result=findviewbyid(r.id.tv_volley_result);



    }
    public void initlistener()
    {
        get.setonclicklistener(new view.onclicklistener() {
            @override
            public void onclick(view view) {
                //创建一个请求队列
                requestqueue requestqueue=volley.newrequestqueue(mainactivity.this);
                //创建一个请求
                string url="http://gank.io/api/xiandu/category/wow";
                stringrequest stringrequest=new stringrequest(url, new response.listener<string>() {
                   //正确接受数据之后的回调
                    @override
                    public void onresponse(string response) {
                    tv_volley_result.settext(response);
                    }
                }, new response.errorlistener() {//发生异常之后的监听回调
                    @override
                    public void onerrorresponse(volleyerror error) {
                        tv_volley_result.settext("加载错误"+error);
                    }
                });
                //将创建的请求添加到请求队列当中
                requestqueue.add(stringrequest);
            }
        });


        post.setonclicklistener(new view.onclicklistener() {
            @override
            public void onclick(view view) {
                // 1 创建一个请求队列
                requestqueue requestqueue = volley.newrequestqueue(mainactivity.this);

                // 2 创建一个post请求
                string url = "http://api.m.mtime.cn/pagesubarea/trailerlist.api";
                stringrequest stringrequest = new stringrequest(request.method.post, url, new response.listener<string>() {
                    @override
                    public void onresponse(string s) {
                        tv_volley_result.settext(s);
                    }
                }, new response.errorlistener() {
                    @override
                    public void onerrorresponse(volleyerror volleyerror) {
                        tv_volley_result.settext("请求失败" + volleyerror);
                    }
                }) {
                    @override
                    protected map<string, string> getparams() throws authfailureerror {

                        map<string, string> map = new hashmap<string, string>();
//                        map.put("value1","param1");

                        return map;
                    }
                };

                // 3 将post请求添加到队列中
                requestqueue.add(stringrequest);




            }
        });

        json.setonclicklistener(new view.onclicklistener() {
            @override
            public void onclick(view view) {
// 1 创建一个请求队列
                requestqueue requestqueue = volley.newrequestqueue(mainactivity.this);

                // 2 创建一个请求
                string url = "http://gank.io/api/xiandu/category/wow";
                //jsonarrayrequest jsonobjectrequest2=......
                jsonobjectrequest jsonobjectrequest = new jsonobjectrequest(url, null, new response.listener<jsonobject>() {
                    @override
                    public void onresponse(jsonobject jsonobject) {
                        tv_volley_result.settext(jsonobject.tostring());
                    }
                }, new response.errorlistener() {
                    @override
                    public void onerrorresponse(volleyerror volleyerror) {
                        tv_volley_result.settext("请求失败" + volleyerror);
                    }
                });

                // 3 将创建的请求添加到请求队列中
                requestqueue.add(jsonobjectrequest);

//这一步完成之后就可以使用我们的json解析了

            }
        });

        imagerequest.setonclicklistener(new view.onclicklistener() {
            @override
            public void onclick(view view) {
                // 1 创建一个请求队列
                requestqueue requestqueue = volley.newrequestqueue(mainactivity.this);

                // 2 创建一个图片的请求
                string url = "http://img5.mtime.cn/mg/2016/10/11/160347.30270341.jpg";
                imagerequest imagerequest = new imagerequest(url, new response.listener<bitmap>() {
                    @override
                    public void onresponse(bitmap bitmap) {
                        // 正确接收到图片
                        iv.setvisibility(view.visible);//将图片设置为可见
                        iv.setimagebitmap(bitmap);//将接受到的图片bitmap对象传入到我们的imageview当中
                    }
                }, 0, 0, bitmap.config.rgb_565, new response.errorlistener() {
                    //前面两个0,0的参数表示的是我们加载图片最大宽度和高度,后面的bitmap.config.rgb_565表示图片的质量
                    @override
                    public void onerrorresponse(volleyerror volleyerror) {
                        iv.setimageresource(r.drawable.test);
                    }
                });

                // 3 将请求添加到请求队列中
                requestqueue.add(imagerequest);

            }
        });


        imageload.setonclicklistener(new view.onclicklistener() {
            @override
            public void onclick(view view) {
                requestqueue requestqueue = volley.newrequestqueue(mainactivity.this);


                imageloader imageloader = new imageloader(requestqueue, new imageloader.imagecache() {
                   @override
                   public bitmap getbitmap(string s) {
                      return null;
                   }

                   @override
                  public void putbitmap(string s, bitmap bitmap) {

                  }
               });


                // 加载图片
                string url = "http://img5.mtime.cn/mg/2016/10/11/160347.30270341.jpg";
                iv.setvisibility(view.visible);
                imageloader.imagelistener imagelistener = imageloader.getimagelistener(iv, r.drawable.test, r.drawable.test);
                //上述代码后面两个参数分别表示的是默认的图片和加载失败之后的图片
                imageloader.get(url, imagelistener);
            }
        });

        networkimageview.setonclicklistener(new view.onclicklistener() {
            @override
            public void onclick(view view) {

                // 让控件显示
                network.setvisibility(view.visible);

                // 创建一个请求队列
                requestqueue requestqueue = volley.newrequestqueue(mainactivity.this);

                // 创建一个imageloader
                imageloader imageloader = new imageloader(requestqueue, new bitmapcache());

                // 默认图片和异常图片设置
                network.setdefaultimageresid(r.drawable.test);
                network.seterrorimageresid(r.drawable.test);

                // 加载图片
                string url = "http://img5.mtime.cn/mg/2016/10/11/160347.30270341.jpg";
                network.setimageurl(url, imageloader);


            }
        });

    }
}

得解。

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网