Lrucache缓存程序关闭缓存自动清除,所以要在onstart方法中调用,只要不关闭程序缓存就在,除以1024是以kb为单位

public class MainActivity extends AppCompatActivity {
private LruCache<String, Bitmap> mMemoryCache;
ImageView imageView;
Bitmap bitmap;
int cacheSize; @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.image);
long maxMemory = Runtime.getRuntime().maxMemory();
cacheSize = (int) (maxMemory / 8)/1024;
mMemoryCache = new LruCache<String, Bitmap>(
cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// 重写此方法来衡量每张图片的大小,默认返回图片数量。
return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
}
@Override
protected void entryRemoved(boolean evicted, String key,
Bitmap oldValue, Bitmap newValue) {
Log.v("tag", "hard cache is full , push to soft cache");
}
};
}
@Override
protected void onStart() {
super.onStart();
Bitmap bitmap2 = mMemoryCache.get("a");
if (bitmap2 != null) {
imageView.setImageBitmap(bitmap2);
} else {
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon12);
mMemoryCache.put("a",bitmap);
imageView.setImageBitmap(bitmap);
}
}
}

最新文章

  1. 【C#】【Thread】SpinWait
  2. git clone时,报403错误,完美解决方案
  3. C中的流程控制
  4. Character Controller (角色控制器) 中 Move()和SimpleMove() 的区别
  5. 为什么用服务不用线程-Android
  6. 第一次使用easyUI
  7. Android中shape中的属性大全
  8. 把pgboucer做成postgresql服务
  9. 编程范式感想(一)——在C中进行对模板功能的实现
  10. 数据的动态合并和导出至EXCEL
  11. 在Delphi中使用C++对象(两种方法,但都要改造C++提供的DLL)
  12. Android:百度地图 + 百度导航
  13. django登录
  14. thinkphp 区间查询 查符合某个字段的数据 但是n个条件 用and or 配合
  15. Java 设计模式专栏
  16. 有关两个jar包中包含完全相同的包名和类名的加载问题
  17. java基础-温故而知新(02)
  18. angular自定义指令
  19. swift笔记(二) —— 运算符
  20. Redis-概述

热门文章

  1. SpringBoot框架:&#39;url&#39; attribute is not specified and no embedded datasource could be configured问题处理
  2. @JsonCreator自定义反序列化函数-JSON框架Jackson精解第5篇
  3. 【大数据】深入源码解析Map Reduce的架构
  4. 关于取整函数ceil(),floor(),round()函数得应用
  5. dubbo学习(五)注册中心zookeeper
  6. 前端性能测试工具之PageSpeed Insights
  7. 如何使用 Python 進行字串格式化
  8. 【记】《.net之美》之读书笔记(一) C#语言基础
  9. 039 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 01 循环结构概述
  10. Github 太狠了,居然把 &quot;master&quot; 干掉了!