LruCache缓存bitmap(一)
生活随笔
收集整理的這篇文章主要介紹了
LruCache缓存bitmap(一)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Lrucache是把圖片緩存到內置sd卡,設置緩存容量為系統分配容量的八分之一,單位byte,超過緩存容量gc會自動回收不長使用的緩存.覺得lrucache就先map一樣,放入鍵值對就行了,比較方便,現在官方不讓用軟引用緩存了softpreference,好像是容易內存泄漏
public class MainActivity extends AppCompatActivity {private LruCache<String, Bitmap> lruCache;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ImageView imageView = findViewById(R.id.image);long maxMemory = Runtime.getRuntime().maxMemory();int cacheSize = (int) (maxMemory / 8);lruCache = new LruCache<String, Bitmap>(cacheSize) {@Overrideprotected int sizeOf(String key, Bitmap value) {return value.getByteCount();}};Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon12);lruCache.put("a", bitmap);Bitmap bitmap2 = lruCache.get("a");if (bitmap2 == null) {System.out.println("無緩存");} else {imageView.setImageBitmap(bitmap2);}} }因為是存在內部存儲所以不用獲取外部存儲權限,輸出無緩存就是沒緩存上,可以把第二個a改成b試一下就輸出無緩存
轉載于:https://www.cnblogs.com/Ocean123123/p/10980691.html
總結
以上是生活随笔為你收集整理的LruCache缓存bitmap(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库备份启用加密
- 下一篇: 用Python3Request爬取英雄联