LruBitmapCache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Bitmap | |
import androidx.collection.LruCache | |
val runtimeMemoryChunkInBytes: Int | |
get() = (Runtime.getRuntime().maxMemory() / 8).toInt() | |
/** | |
* https://developer.android.com/topic/performance/graphics/cache-bitmap | |
*/ | |
class LruBitmapCache : LruCache<String, Bitmap>(runtimeMemoryChunkInBytes) { | |
init { | |
Log.i("maxSize=${maxSize()}") | |
} | |
override fun entryRemoved(evicted: Boolean, key: String, oldValue: Bitmap, newValue: Bitmap?) { | |
super.entryRemoved(evicted, key, oldValue, newValue) | |
if (evicted && !oldValue.isRecycled) | |
oldValue.recycle() | |
} | |
override fun sizeOf(key: String, value: Bitmap): Int = value.allocationByteCount | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
line 19 should be:
if (evicted && !oldValue.isRecycled)