Volley的网络图片加载简单用法

作者: 阳光底下的那个少年 | 来源:发表于2017-06-07 14:59 被阅读21次

    简介

    Volley是在2013年Google I/O大会上推出了一个新的网络通信框架,内部封装了HttpURLConnection和HttpClient, 解决了网络数据解析和线程切换的问题。今天不详解Volley的具体原理,只是简单地讲下怎么利用Volley获取网络图片和加载它.
    步骤如下:
    1.添加volley依赖

        compile 'eu.the4thfloor.volley:com.android.volley:2015.05.28'
    

    2.采用Volley框架自定义的挂载图片控件NetWorkImageView

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        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"
        tools:context="com.example.jasonwang.loadernetpicture.MainActivity">
    
        <com.android.volley.toolbox.NetworkImageView
            android:id="@+id/niv_pic"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
           />
    
    </RelativeLayout>
    

    3.请求网络图片的主要方法

    package com.example.jasonwang.loadernetpicture;
    
    import android.graphics.Bitmap;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.LruCache;
    
    import com.android.volley.RequestQueue;
    import com.android.volley.toolbox.ImageLoader;
    import com.android.volley.toolbox.NetworkImageView;
    import com.android.volley.toolbox.Volley;
    
    public class MainActivity extends AppCompatActivity {
    
        private NetworkImageView nivImageView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            nivImageView = (NetworkImageView) findViewById(R.id.niv_pic);
            getPicFromNetWork();   //获取网络图片
        }
    
    
        int maxSize = 5 * 1024 * 1024;  //定义缓存的大小:5M
        public void getPicFromNetWork() {
            final String url = "http://imgsrc.baidu.com/baike/pic/item/b3fb43166d224f4aa657f6540af790529922d1af.jpg";
           //创建请求队列对象
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            ///创建图片缓存区对象
            ImageLoader.ImageCache  imageCache = new MyLruCache(maxSize);
            //创建图片加载器对象
            final ImageLoader imageLoader  =new ImageLoader(requestQueue,imageCache);
            //将网络图片加载都控件里
            nivImageView.setImageUrl(url,imageLoader);
    
        }
    
        /**
         * 定义自己的图片缓存区,这里的缓存只是一级缓存(也就是内存缓
         *存)
        */
        class MyLruCache extends LruCache<String,Bitmap> implements ImageLoader.ImageCache{
    
            /**
             * @param maxSize for caches that do not override {@link #sizeOf}, this is
             *                the maximum number of entries in the cache. For all other caches,
             *                this is the maximum sum of the sizes of the entries in this cache.
             */
            public MyLruCache(int maxSize) {      //定义缓存的大小
                super(maxSize);
            }
    
            /**
             *获取要缓存的图片的大小,检查是否会爆仓
             * @param key
             * @param value
             * @return
             */
            @Override
            protected int sizeOf(String key, Bitmap value) {
                return  value.getByteCount();
            }
    
            /**
             * 从缓存中获取图片
             * @param url
             * @return
             */
            @Override
            public Bitmap getBitmap(String url) {
                return get(url);
            }
    
            /**
             * 将图片存入缓存中
             * @param url
             * @param bitmap
             */
            @Override
            public void putBitmap(String url, Bitmap bitmap) {
              put(url, bitmap);
            }
        }
    }
    

    注意:Volley获取网络图片跟流行的Picasso,Glide开源框架不太一样,Volley底层是没有封装好图片缓存区,需要自己定义肯定会有小伙伴问我,明明有其他那么多封装好的框架不用,还要用这个.因为我想告诉大家如果用Volley获取网络图片的话该如何定义图片缓存

    4.添加网络权限

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

    好了,看下我帅气的偶像胡歌吧

    image.png

    相关文章

      网友评论

        本文标题:Volley的网络图片加载简单用法

        本文链接:https://www.haomeiwen.com/subject/jmjmfxtx.html