LruCache缓存bitmap
Posted ocean123123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LruCache缓存bitmap相关的知识,希望对你有一定的参考价值。
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);
以上是关于LruCache缓存bitmap的主要内容,如果未能解决你的问题,请参考以下文章