aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/util/BitmapLruCache.java
blob: 2c299fb3e09afef9ba149346a7e3b33437fd9dc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package org.wordpress.android.util;

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

import com.android.volley.toolbox.ImageLoader.ImageCache;

import java.util.Map;

public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageCache {
    public BitmapLruCache(int maxSize) {
        super(maxSize);
    }

    public void removeSimilar(String keyLike) {
        Map<String, Bitmap> map = snapshot();

        for (String key : map.keySet()) {
           if (key.contains(keyLike)) {
               remove(key);
           }
        }
    }

    @Override
    protected int sizeOf(String key, Bitmap value) {
        // The cache size will be measured in kilobytes rather than
        // number of items.
        int bytes = (value.getRowBytes() * value.getHeight());
        return (bytes / 1024); //value.getByteCount() introduced in HONEYCOMB_MR1 or higher.
    }

    @Override
    public Bitmap getBitmap(String key) {
        return this.get(key);
    }

    @Override
    public void putBitmap(String key, Bitmap bitmap) {
        this.put(key, bitmap);
    }
}