LruCache使用方式-----LruCache单例实现及其使用方法

2019-04-14 16:44发布

单例类: import android.util.LruCache; public class MyLruCache extends LruCache { private static volatile MyLruCache instance = null; private MyLruCache(int maxSize) { super(maxSize); } public static MyLruCache getInstance() { if (instance == null) { synchronized (MyLruCache.class) { if (instance == null) { instance = new MyLruCache(1024 * 1024 * 20); } } } return instance; } }   使用方式: MyLruCache myLruCache = MyLruCache.getInstance(); Bitmap thatBitmap = myLruCache.get("key"); if (thatBitmap == null) { thatBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg_047); myLruCache.put("key", thatBitmap); } iv1.setImageBitmap(thatBitmap); 第二次加载的时候就可以直接使用内存中的bitmap了,速度快了很多