直接上代碼 兩種方式 ImageView 和NetworkImageViewandroid
若有問題或者好的建議、意見 歡迎你們加入技術羣(羣號: 387648673 )緩存
先自定義全局Application 獲取app
public classMyApplication extends Application {
private static RequestQueuerequestQueue;
private static MyApplicationapplication;
@Override
public void onCreate() {
// TODO Auto-generated methodstub
super.onCreate();
application = this;
requestQueue =Volley.newRequestQueue(this);
}
public static MyApplicationgetInstance() {
// TODO Auto-generated methodstub
return application;
}
public static RequestQueuegetRequestQueue(){
return requestQueue;
}
}ide
先上佈局文件佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv_test1"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="10dp"/>
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/iv_test2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp" >
</com.android.volley.toolbox.NetworkImageView>
</LinearLayout>this
方式一用ImageView加載url
須要封裝一下自定義的BitmapCache類實現圖片緩存接口ImageCache.net
public class BitmapCache implements ImageCache{
public LruCache<String, Bitmap> cache;
public BitmapCache(){
int maxMermorySize = (int)(Runtime.getRuntime().maxMemory()/1024);
cache = new LruCache<String, Bitmap>(maxMermorySize/8){
@Override
protected int sizeOf(String key, Bitmap value) {
// TODO Auto-generated method stub
return value.getRowBytes()*value.getHeight()/1024;
}
};
}
@Override
public Bitmap getBitmap(String key) {
// TODO Auto-generated method stub
return cache.get(key);
}
@Override
public void putBitmap(String key, Bitmap value) {
// TODO Auto-generated method stub
cache.put(key, value);
}
}xml
方式二 用NetworkImageView記載接口
封裝一個VolleyUtils圖片加載並處理緩存的類,使用起來就比較方便了
public class VolleyUtils {
private static ImageLoader imageLoader;
private static LruCache<String,Bitmap> lruCache;
private static HashMap<String,SoftReference<Bitmap>> softmap;
private static void initCache() {
// TODO Auto-generated method stub
//實例化二級緩存
softmap = new HashMap<String, SoftReference<Bitmap>>();
//實例化一級緩存 字節單位 KB
int maxMermorySize = (int)(Runtime.getRuntime().maxMemory()/1024);
lruCache = new LruCache<String, Bitmap>(maxMermorySize/8){
@Override
protected int sizeOf(String key, Bitmap value) {
// TODO Auto-generated method stub
//return lruCache.size()
return value.getRowBytes()*value.getHeight()/1024;//返回KB的大小
}
@Override
protected void entryRemoved(boolean evicted, String key,
Bitmap oldValue, Bitmap newValue) {
//當oldValue被一級緩存逐出時,放入二級緩存中
if (evicted) {
softmap.put(key, new SoftReference<Bitmap>(oldValue));
}
}
};
}
public static ImageLoader getImageLoader(Context context){
if (imageLoader==null) {
initCache();
imageLoader = new ImageLoader(MyApplication.getRequestQueue(), new ImageCache() {
@Override
public void putBitmap(String url, Bitmap bitmap) {
// 存入一級緩存
lruCache.put(url, bitmap);
}
@Override
public Bitmap getBitmap(String url) {
//從一級緩存中獲取
Bitmap bitmap = lruCache.get(url);
//判斷一級緩存是否存在
if ( bitmap== null) {//一級緩存不存在
//從二級緩存中獲取
SoftReference<Bitmap> softRef = softmap.get(url);
if (softRef!=null) {
bitmap = softRef.get();
//獲取軟引用中圖片
//爲了便於使用將二級緩存中的圖片存放一級緩存中
lruCache.put(url, bitmap);
//將此圖片從二級緩存中移除
softmap.remove(url);
}
}
return null;
}
});
}
return imageLoader;
}
}
在Activity或者Fragment或者適配中能夠直接調取用,下邊是以Activity爲實例的代碼
private ImageView test1;//用ImageView加載網略圖片
private NetworkImageView test2;//用NetworkImageView記載網略圖片
@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
test1 = (ImageView)findViewById(R.id.iv_test1);
test2 = (NetworkImageView) findViewById(R.id.iv_test2);
initImage();
}
private void initImage(){
//本人隨便找了一張美女的圖片
String url = "http://img.pconline.com.cn/images/upload/upc/tx/wallpaper/1205/25/c2/11755122_1337938898582.jpg";
//方式一用ImageView加載
ImageLoader loader = new ImageLoader(MyApplication.getRequestQueue(), new BitmapCache());
ImageListener listener = ImageLoader.getImageListener(test1,
R.drawable.ic_launcher, R.drawable.ic_launcher);
loader.get(url, listener);
//方式二用NetworkImageView加載
ImageLoader imageLoader = VolleyUtils.getImageLoader(this);
test2.setDefaultImageResId(R.drawable.ic_launcher);
test2.setErrorImageResId(R.drawable.ic_launcher);
test2.setImageUrl(url, imageLoader);
}