下面这个例子是运用LruCache进行图片缓存的,关于LruCache前面已经提过一次了,这里再说一次只是说明这货很重要而已。用DDMS测试内存发现还是可以的,资料有说引用软引用机制也会起到很大的作用,但是又有人说现在软引用和弱引用在GC的时候都会被强行回收掉,所以比较郁闷,继续去查资料。
下面这个应用是一个简单的图片浏览器,用了sharepreferences存储页码而已。
package com.example.mytest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.util.LruCache;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv;
private Button pre;
private Button next;
private int curPage = 1;
final private int maxPage = 23;
private int windowsWidth;
private int windowsHeight;
private String parentPath = Environment.getExternalStorageDirectory()
+ "/finger/";
private String curPath;
private LruCache<String, Bitmap> mMemoryCache;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获得起始
SharedPreferences sp = getSharedPreferences("config", Context.MODE_PRIVATE);
int t = sp.getInt("page", 0);
if (t != 0) {
curPage = t;
}
// 生成当前图片的路径
curPath = parentPath + curPage + ".png";
pre = (Button) findViewById(R.id.pre);
next = (Button) findViewById(R.id.next);
iv = (ImageView) findViewById(R.id.iv);
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
windowsWidth = wm.getDefaultDisplay().getWidth();
windowsHeight = wm.getDefaultDisplay().getHeight();
mMemoryCache = new LruCache<String, Bitmap>((cacheSize)) {
@SuppressLint("NewApi")
@Override
protected int sizeOf(String key, Bitmap value) {
// TODO Auto-generated method stub
return value.getByteCount() / 1024;
}
};
// 加载第一张图片
Bitmap firstPic = creatBm(curPage);
iv.setImageBitmap(firstPic);
// 缓存当前和下一张图片
savePage(curPage);
if (curPage < maxPage) {
savePage(curPage + 1);
}
/**
* 上一张按钮的监听器
*/
pre.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (curPage <= 1) {
return;
}
curPage--;
Bitmap bm = mMemoryCache.get(curPage + "");
if (bm == null) {
curPath = parentPath + curPage + ".png";
bm = creatBm(curPage);
}
iv.setImageBitmap(bm);
// 缓存前一张图片
if (curPage > 1) {
savePage(curPage - 1);
}
}
});
/**
* 下一张按钮的监听器
*/
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (curPage >= maxPage) {
return;
}
curPage++;
Bitmap bm = mMemoryCache.get(curPage + "");
if (bm == null) {
curPath = parentPath + curPage + ".png";
bm = creatBm(curPage);
}
iv.setImageBitmap(bm);
// 缓存下一张图片
if (curPage < maxPage) {
savePage(curPage + 1);
}
}
});
}
/**
*
* 缓存图片函数
*
* @param page
* :要缓存图片的页码
*
*
*/
private void savePage(int page) {
Bitmap curPic = mMemoryCache.get(page + "");
if (curPic == null) {
curPic = creatBm(page);
}
mMemoryCache.put(page + "", curPic);
}
/**
* 根据屏幕大小进行创建并缩放图片
*
* @param page
* :要创建的图片的标示
* @return 创建的Bitmap
*/
private Bitmap creatBm(int page) {
int picWidth = 0;
int picHeight = 0;
String path = parentPath + page + ".png";
Options opts = new Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, opts);
picWidth = opts.outWidth;
picHeight = opts.outHeight;
float scareWidth = (float) picWidth / windowsWidth;
float scareHeight = (float) picHeight / windowsHeight;
float scare = (scareWidth > scareHeight) ? scareWidth : scareHeight;
if (scare < 1) {
scare = 1;
}
opts.inJustDecodeBounds = false;
opts.inSampleSize = (int) scare;
Bitmap bm = BitmapFactory.decodeFile(path, opts);
return bm;
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
SharedPreferences sp = getSharedPreferences("config", Context.MODE_PRIVATE);
Editor ed = sp.edit();
ed.putInt("page", curPage);
ed.commit();
}
}