LruCache cached bitmap (two)

Lrucache caching program closes automatically clear the cache, so to call onstart methods, they do not just close the program cache, it is divided by 1024 kb units

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 new LRUCache <String, Bitmap> ( 
                cacheSize) { 
            @Override 
            protected  int sizeOf (String Key, Bitmap Bitmap) {
                 // override this method to measure the size of each picture, the default number returned image. 
                return bitmap.getRowBytes () * bitmap.getHeight () / 1024 ; 
            } 
            @Override 
            protected  void entryRemoved ( Boolean evicted, String Key, 
                                        Bitmap oldValue, Bitmap newValue) { 
                Log.v ( "Tag", "Hard IS Full Cache, Push Soft Cache to " ); 
            }
        };
    }
    @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);
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/Ocean123123/p/10981307.html