LruCache缓存bitmap(二)
2024-08-25 04:59:22
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);
}
}
}
最新文章
- 【C#】【Thread】SpinWait
- git clone时,报403错误,完美解决方案
- C中的流程控制
- Character Controller (角色控制器) 中 Move()和SimpleMove() 的区别
- 为什么用服务不用线程-Android
- 第一次使用easyUI
- Android中shape中的属性大全
- 把pgboucer做成postgresql服务
- 编程范式感想(一)——在C中进行对模板功能的实现
- 数据的动态合并和导出至EXCEL
- 在Delphi中使用C++对象(两种方法,但都要改造C++提供的DLL)
- Android:百度地图 + 百度导航
- django登录
- thinkphp 区间查询 查符合某个字段的数据 但是n个条件 用and or 配合
- Java 设计模式专栏
- 有关两个jar包中包含完全相同的包名和类名的加载问题
- java基础-温故而知新(02)
- angular自定义指令
- swift笔记(二) —— 运算符
- Redis-概述
热门文章
- SpringBoot框架:&#39;url&#39; attribute is not specified and no embedded datasource could be configured问题处理
- @JsonCreator自定义反序列化函数-JSON框架Jackson精解第5篇
- 【大数据】深入源码解析Map Reduce的架构
- 关于取整函数ceil(),floor(),round()函数得应用
- dubbo学习(五)注册中心zookeeper
- 前端性能测试工具之PageSpeed Insights
- 如何使用 Python 進行字串格式化
- 【记】《.net之美》之读书笔记(一) C#语言基础
- 039 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 01 循环结构概述
- Github 太狠了,居然把 ";master"; 干掉了!