AndroidLru的简单使⽤
Lru 算法(缓存淘汰算法)
LRU(Least recently used,最近最少使⽤)算法根据数据的历史访问记录来进⾏淘汰数据,其核⼼思想是“如果数据最近被访问过,那么将来被访问的⼏率也更⾼”。(PS:有兴趣可以⽤链表⾃⼰实现)
Android LruCache
在Android LruCache中的原理⽐较简单,有兴趣可以⾃⼰去了解.具体的代码这⾥就不放了.
1 LruCacheIMG 类
这⾥采⽤单例模式,主要原因是 在App中不是只有⼀处需要加载图⽚和缓存图⽚
private LruCacheIMG(){
android简单教程Runtime t = Runtime();
// 这⾥⽤app 运⾏内存的 1/8 作为缓存图⽚的能缓存的最⼤容量
// 图⽚现在⼀般都⽐较⼤,但是也不能给过多的内存
int max = (int) (t.maxMemory()/8);
mLrc = new LruCache<String, Bitmap>(max){
@Override
protected int sizeOf(String key, Bitmap value) {
ByteCount();
}
};
}
private static LruCacheIMG Init()
{
if (lruCacheImg == null)
{
synchronized (LruCacheIMG.class){
lruCacheImg = new LruCacheIMG();
}
}
return lruCacheImg;
}
// 这⾥采⽤单例模式,因为不是只有⼀个地⽅需要去加载图⽚
private static LruCacheIMG getInstance()
{
return lruCacheImg == null ? Init() : lruCacheImg;
}
2 主要功能display downloadIMG getBitMap
disPlay 接受到传过来的url 和 view 利⽤getBitMap 获取存在LrcCache中的Bitmap
如果存在 就加载 如果不存在呢? 那么就去下载
注意 ⼦线程是不能更改UI的
public static void disPlay(ImageView view,String url)
{
Bitmap bitmap = getInstance().getBitMap(url);
if (bitmap == null)
{
getInstance().DownLoadImg(view,url);
}else {
view.setImageBitmap(bitmap);
}
}
private Bitmap getBitMap(String url)
{
(url);
}
private void DownLoadImg(final ImageView view, final String url)
{
new Thread(new Runnable() {
@Override
public void run() {
URL imgURL = null;
try {
imgURL = new URL(url);
}catch (MalformedURLException e){
e.printStackTrace();
}
try {
HttpsURLConnection conn = (HttpsURLConnection) imgURL.openConnection();
/
/ 这⾥假设⼀定可以从⽹络获取到图⽚
InputStream is = InputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
mLrc.put(url,bitmap);
Message msg = new Message();
Iview = view;
msg.obj = url;
handler.sendMessage(msg);
}catch (IOException e){
e.printStackTrace();
}
}
}).start();
}
3 主要代码如下
public class LruCacheIMG {
private static LruCache<String,Bitmap> mLrc;
private static LruCacheIMG lruCacheImg;
private ImageView Iview;
private android.os.Handler handler = new android.os.Handler(new android.os.Handler.Callback() { @Override
public boolean handleMessage(Message message) {
String url = (String) message.obj;
if (Iview != null)
Iview.setImageBitmap(getBitMap(url));
return true;
}
});
});
private LruCacheIMG(){
Runtime t = Runtime();
int max = (int) (t.maxMemory()/8);
mLrc = new LruCache<String, Bitmap>(max){
@Override
protected int sizeOf(String key, Bitmap value) {
ByteCount();
}
};
}
private static LruCacheIMG Init()
{
if (lruCacheImg == null)
{
synchronized (LruCacheIMG.class){
lruCacheImg = new LruCacheIMG();
}
}
return lruCacheImg;
}
private static LruCacheIMG getInstance()
{
return lruCacheImg == null ? Init() : lruCacheImg;
}
public static void disPlay(ImageView view,String url)
{
Bitmap bitmap = getInstance().getBitMap(url);
if (bitmap == null)
{
getInstance().DownLoadImg(view,url);
}else {
view.setImageBitmap(bitmap);
}
}
private Bitmap getBitMap(String url)
{
(url);
}
private void DownLoadImg(final ImageView view, final String url)
{
new Thread(new Runnable() {
@Override
public void run() {
URL imgURL = null;
try {
imgURL = new URL(url);
}catch (MalformedURLException e){
e.printStackTrace();
}
try {
HttpsURLConnection conn = (HttpsURLConnection) imgURL.openConnection(); t();
// 这⾥假设⼀定可以从⽹络获取到图⽚
InputStream is = InputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
mLrc.put(url,bitmap);
Message msg = new Message();
Iview = view;
msg.obj = url;
handler.sendMessage(msg);
}catch (IOException e){
e.printStackTrace();
}
}
}).start();
}
}
这只是⾃⼰做的⼀个简单的demo,如果其中有写的不好的地⽅,还望各位能够指出.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论