Volley 的简单学习之加载图片


package com.imooc.volleyimage;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;

public class MainActivity extends AppCompatActivity {
    ImageView iv_img;
    private NetworkImageView mNetworkImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv_img = (ImageView) findViewById(R.id.iv_img);
        mNetworkImageView = (NetworkImageView) findViewById(R.id.iv_networkImageView);
        String url = "https://www.baidu.com/img/bdlogo.png";

        //方式三
        //   使用 ImageLoader
        ImageLoader imageLoader = new ImageLoader(MyApplication.getHtppQueues(), new BitmapCache());

        mNetworkImageView.setDefaultImageResId(R.mipmap.ic_launcher); //设置加载过程中默认显示图片
        mNetworkImageView.setErrorImageResId(R.mipmap.ic_launcher); //设置加载出错显示图片
        mNetworkImageView.setImageUrl(url, imageLoader);


        System.out.println("-----------------我是分割线-------------------------");

//        //方式二
       使用 ImageLoader
//        ImageLoader imageLoader=new ImageLoader(MyApplication.getHtppQueues(),new BitmapCache());
//
//        ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(iv_img,
//                R.mipmap.ic_launcher, //默认显示的图片
//                R.mipmap.ic_launcher //加载出错的图片
//                );
//        imageLoader.get(url,imageListener);
        System.out.println("-----------------我是分割线-------------------------");
        //方式 一
//        ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
//            @Override
//            public void onResponse(Bitmap response) {
//                iv_img.setImageBitmap(response);
//                Toast.makeText(MainActivity.this, "请求成功: " + response, Toast.LENGTH_LONG).show();
//
//            }
//        }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//                iv_img.setImageResource(R.mipmap.ic_launcher); //如果请求失败,给出一个默认显示的图片
//                Toast.makeText(MainActivity.this, "请求失败: " + error, Toast.LENGTH_SHORT).show();
//            }
//        });
// MyApplication.getHtppQueues().add(imageRequest); //注意 添加到全局队列中


    }
}

自定义 BitmapCache 结合LruCache 使用

package com.imooc.volleyimage;

import android.graphics.Bitmap;
import android.util.LruCache;

import com.android.volley.toolbox.ImageLoader;

/**
 * 图片的缓存
 * 结合 LruCache
 */
public class BitmapCache implements ImageLoader.ImageCache {

    public LruCache<String, Bitmap> mCache;
    public final int MAX_CACHE = 10 * 1024 * 1024; //定义最大缓存为 10M

    public BitmapCache() {

        //创建LruCache<String, Bitmap> 实例化对象
        mCache = new LruCache<String, Bitmap>(MAX_CACHE){
            @Override
            protected int sizeOf(String key, Bitmap value) {
                return value.getRowBytes()*value.getHeight(); //为什么 返回这个?

            }
        };
    }

    @Override
    public Bitmap getBitmap(String url) {
        return mCache.get(url);
    }

    @Override
    public void putBitmap(String url, Bitmap bitmap) {
        mCache.put(url,bitmap);
    }
}

自定义 Application

package com.imooc.volleyimage;

import android.app.Application;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

/**
 * 自定义 Application
 */
public class MyApplication extends Application {

    public static RequestQueue sQueue;

    @Override
    public void onCreate() {
        super.onCreate();
        sQueue = Volley.newRequestQueue(getApplicationContext());
    }

    public static RequestQueue getHtppQueues(){
        return sQueue;
}}

注意事项

AndroidManifest.xml中生命的权限
<uses-permission android:name="android.permission.INTERNET" />

在application 节点中 配置 name属性  android:name=".MyApplication"build.gradle 中的 Volley jar包的使用名称 
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.navercorp.volleyextensions:volley-views:2.0.1'
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值