Last active
June 3, 2022 17:59
-
-
Save kibotu/e0a6a0af8ea0eac3932bccf60d2a1edf to your computer and use it in GitHub Desktop.
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)