volley三种基本请求图片的方式与Lru的基本使用:正常的加载+含有Lru缓存的加载+Volley控件networkImageview的使用
首先做出全局的请求队列
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>
volley三种基本请求图片的方式与Lru的基本使用:正常的加载+含有Lru缓存的加载+Volley控件networkImageview的使用的更多相关文章
- javascript 中数组的创建 添加 与将数组转换成字符串 页面三种提交请求的方式
创建js数组 var array=new Array(); Java中创建数组 private String[] array=new String[3]; 两个完全不同的,js中是可变长度的 添加内容 ...
- ASP.NET Core应用的错误处理[1]:三种呈现错误页面的方式
由于ASP.NET Core应用是一个同时处理多个请求的服务器应用,所以在处理某个请求过程中抛出的异常并不会导致整个应用的终止.出于安全方面的考量,为了避免敏感信息的外泄,客户端在默认的情况下并不会得 ...
- Android中三种超实用的滑屏方式汇总(转载)
Android中三种超实用的滑屏方式汇总 现如今主流的Android应用中,都少不了左右滑动滚屏这项功能,(貌似现在好多人使用智能机都习惯性的有事没事的左右滑屏,也不知道在干什么...嘿嘿),由于 ...
- HttpClient 三种 Http Basic Authentication 认证方式,你了解了吗?
Http Basic 简介 HTTP 提供一个用于权限控制和认证的通用框架.最常用的 HTTP 认证方案是 HTTP Basic authentication.Http Basic 认证是一种用来允许 ...
- Js之Dom学习-三种获取页面元素的方式、事件、innerText和innerHTML的异同
一.三种获取页面元素的方式: getElementById:通过id来获取 <body> <input type="text" value="请输入一个 ...
- .NET提供了三种后台输出js的方式:
.NET提供了三种后台输出js的方式: 首先创建 js文件testjs.js { Page.ClientScript.RegisterClientScriptInclude("keys ...
- Hive中的三种不同的数据导出方式介绍
问题导读:1.导出本地文件系统和hdfs文件系统区别是什么?2.带有local命令是指导出本地还是hdfs文件系统?3.hive中,使用的insert与传统数据库insert的区别是什么?4.导出数据 ...
- Android三种实现自定义ProgressBar的方式介绍
一.通过动画实现 定义res/anim/loading.xml如下: View Row Code<?xml version="1.0" encoding="UTF- ...
- Haproxy的三种保持客户端会话保持方式
2017-03-25 15:41:41 haproxy 三种保持客户端Seesion; 一.源地址hash(用户IP识别) haroxy 将用户IP经过hash计算后 指定到固定的真实服务器上(类 ...
随机推荐
- 介绍 Visifire 常用属性的设置
转载自http://www.cnblogs.com/xinyus/p/3422198.html 主要介绍 Visifire 常用属性的设置,用来生成不同样式的图例 设置Chart的属 //设置titl ...
- ComboBox绑定数据源时触发SelectedIndexChanged事件的处理办法
转载:http://blog.sina.com.cn/s/blog_629e606f01014d4b.html ComboBox最经常使用的事件就是SelectedIndexChanged.但在将Co ...
- unity——使用角色控制器组件+射线移动
首先要导入unity标准资源包Character Controllers 这个标准资源包,为了方便,还添加了两外一个资源包Scripts,后者包含了一些基本的脚本个摄像机脚本. 没错,这次我们要使用其 ...
- unity音频组件
unity 支持的四种音频格式: .AIFF 适用于较短的音乐文件可用作游戏打斗音效 .WAV 适用于较短的音乐文件可用作游戏打斗音效 .MP3 适用于较长的音乐文件可用作游戏背景音乐 .OGG ...
- Maybe I go too extreme
昨天拖着一个没睡好的身体去面试了2家公司 被问到Collection的子集的时候顿时傻了一会,明明很简单的问题一时就想不起来了,哈哈.果然做it的人身体要顾好,状态太差了. 发现了一个问题,其实也是早 ...
- BZOJ4006 JLOI2015 管道连接(斯坦纳树生成森林)
4006: [JLOI2015]管道连接 Time Limit: 30 Sec Memory Limit: 128 MB Description 小铭铭最近进入了某情报部门,该部门正在被如何建立安全的 ...
- 自动生成代码工具【JAVA版】
发现任何项目无非五类操作:新增.修改.删除.查询详细.查询列表 大多数的服务端基础代码都是相同的,但是每次开发一个新项目都会做很多重复工作,从controller,bean,service,到数据库访 ...
- Big Event in HDU(HDU 1171 多重背包)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- IOS 文件管理 2
IOS开发-文件管理(二) 五.Plist文件 String方式添加 NSString *path = [NSHomeDirectory( ) stringByAppen ...
- Linux远程拷贝scp命令
今天要从admin服务器将测试上修正content和image_count后的数据库更新到dz服务器. 首先需要备份数据库,使用mysqldump命令 整表全部备份: mysqldump -u ...