Reputation: 945
How does one calculate the bitmap size? My understanding is it should support three full screens from the Google I/O presentation on Volley. Does anyone know how I can calculate memory size for three full screens on any given Android device? I think this was referring to inmemory caching so a BitmapCache but not sure.
Now I have seen the following calculation suggested, but not sure if this is consistent with keeping three screens worth of data cached in memory.
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;
UPDATE: What is the logic of using 1/8th of the total memory for cache. how does this compare to keeping three screens worth of data?
Thanks
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import com.android.volley.toolbox.ImageLoader.ImageCache;
public class LruBitmapCache extends LruCache<String, Bitmap> implements ImageCache {
public LruBitmapCache(int maxSize) {
super(maxSize);
}
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight();
}
@Override
public Bitmap getBitmap(String url) {
return get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
put(url, bitmap);
}
}
Upvotes: 0
Views: 1554
Reputation: 340
To calculate the size of a bitmap that takes up the size of the screen, which I presume is what you're looking for, and store three of these bitmaps in an LRUCache of a set size which would correspond to the memory taken up by these bitmaps, would be:
// Gets the dimensions of the device's screen
DisplayMetrics dm = context.getResources().getDisplayMetrics();
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
// Assuming an ARGB_8888 pixel format, 4 bytes per pixel
int size = screenWidth * screenHeight * 4;
// 3 bitmaps to store therefore multiply bitmap size by 3
int cacheSize = size * 3;
From this, you should be able to work out the cache size you need to create to store these bitmaps.
Upvotes: 2