volley三种基本请求图片的方式与Lru的基本使用:正常的加载+含有Lru缓存的加载+Volley控件networkImageview的使用

时间:2024-04-22 07:07:45

首先做出全局的请求队列

 package com.qg.lizhanqi.myvolleydemo;

 import android.app.Application;

         import com.android.volley.RequestQueue;
import com.android.volley.toolbox.HttpStack;
import com.android.volley.toolbox.Volley;
/**
* Created by lizhanqi on 2016-7-27-0027.
*/ /**
* 这是一个本应用全局的Volley请求队列,所以这里继承了Application
* 由于这是一个自定义的全局的application,所以在清单文件application中加入属性
* android:name=".MyApplication"
*/
public class MyApplication extends Application {
public static RequestQueue queues; @Override
public void onCreate() {
super.onCreate();
//实例化全局的请求队列
queues = Volley.newRequestQueue(getApplicationContext(), (HttpStack) null);
}
public static RequestQueue getHttpQueues() {
return queues;
}
}

接着做出Lru缓存图片的类

 package com.qg.lizhanqi.myvolleydemo;

 import android.graphics.Bitmap;
import android.util.LruCache; import com.android.volley.toolbox.ImageLoader; /**
* Created by lizhanqi on 2016-7-27-0027.
*/
public class BitmapCache implements ImageLoader.ImageCache {//这里实现它的主要目的是volley需要一个这样类型的缓存方式,所以是继承了它然后搭配Lru一起实现缓存
public LruCache<String,Bitmap> lruCache;
int maxMomory = 10*1024*1024;//最大内存超过10M,启动内存回收或者使用 Runtime.getRuntime().maxMemory()/4代替; public BitmapCache() {
lruCache=new LruCache<String,Bitmap>(maxMomory){// maxMomory可以使用Runtime.getRuntime().maxMemory()/4代替;
@Override
protected int sizeOf(String key, Bitmap value) { //这里应该返回的是一个图片的大小
return value.getRowBytes()*value.getHeight();//或者value.getByteCount();
}
};
}
@Override
public Bitmap getBitmap(String s) {
return lruCache.get(s);
} @Override
public void putBitmap(String s, Bitmap bitmap) {
lruCache.put(s,bitmap);
}
}

  Main使用的方式

 package com.qg.lizhanqi.myvolleydemo;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
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.NetworkImageView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "MainActivity";
private Button VolleyGetString;
private Button VolleyPostString;
private Button VolleyGetJsonObject;
private Button VolleyGetImage;
private Button VolleyLruCacheGetImage;
private NetworkImageView networkImageView;
private ImageView imageView;
private Button loadnetworkimageview; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
VolleyGetImage = (Button) findViewById(R.id.VolleyGetImage);
VolleyLruCacheGetImage = (Button) findViewById(R.id.VolleyLruCacheGetImage);
networkImageView = (NetworkImageView) findViewById(R.id.networkimageview);
loadnetworkimageview = (Button) findViewById(R.id.loadnetworkimageview); loadnetworkimageview.setOnClickListener(this);
VolleyGetImage.setOnClickListener(this);
VolleyLruCacheGetImage.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.VolleyGetImage:
volleyNormalGetImage("http://p1.qhimg.com/t0151320b1d0fc50be8.png");
break;
case R.id.VolleyLruCacheGetImage:
voleyLruCacheGetImage("http://p1.qhimg.com/t0151320b1d0fc50be8.png");
break;
case R.id.loadnetworkimageview:
voleyLoadNetWorkImage("http://p1.qhimg.com/t0151320b1d0fc50be8.png");
break;
}
} //一般的加载
private void volleyNormalGetImage(String url) {
/**
* ImageRequest(String url, Listener<Bitmap> listener, int maxWidth, int maxHeight, Config decodeConfig, ErrorListener errorListener)
* String图片请求网址
* Listener<Bitmap> 请求成功回调的监听
* int maxWidth int maxHeight对于图片压缩的大小,填写数字,如果0,代表原图大小
* Config decodeConfig 图片格式的设置
* ErrorListener errorListener 请求错误的回调
*/
ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
}
}, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
imageRequest.setTag("volleyNormalGetImage");
MyApplication.getHttpQueues().add(imageRequest);
}
//带有缓存的加载
private void voleyLruCacheGetImage(String url) {
//(RequestQueue queue, ImageLoader.ImageCache imageCache),请求的队列,以及缓存
ImageLoader imageLoader = new ImageLoader(MyApplication.getHttpQueues(), new BitmapCache());
//getImageListener(Imageview,defaultimage,errorimage)
ImageLoader.ImageListener imageListener = imageLoader.getImageListener(imageView, R.mipmap.ic_launcher, R.mipmap.ic_launcher);
imageLoader.get(url, imageListener);
}
//volleyNetWorkImageView控件的加载
private void voleyLoadNetWorkImage(String url) {
//(RequestQueue queue, ImageLoader.ImageCache imageCache),请求的队列,以及缓存
ImageLoader imageLoader = new ImageLoader(MyApplication.getHttpQueues(), new BitmapCache());
networkImageView.setDefaultImageResId(R.mipmap.ic_launcher);
networkImageView.setErrorImageResId(R.mipmap.ic_launcher);
networkImageView.setImageUrl(url,imageLoader);
} }

//XML布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.qg.lizhanqi.myvolleydemo.MainActivity"> <Button
android:text="普通图片请求"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/VolleyGetImage"
android:layout_gravity="center_vertical" />
<Button
android:text="LRU缓存图片请求"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/VolleyLruCacheGetImage"
android:layout_gravity="center_vertical" />
<Button
android:text="加载networkimagview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/loadnetworkimageview"
android:layout_gravity="center_vertical" /> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:id="@+id/imageView"
android:layout_gravity="center_vertical" />
<com.android.volley.toolbox.NetworkImageView
android:src="@mipmap/ic_launcher"
android:id="@+id/networkimageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>