From 81d7fb6ebe26a0b4a3db16d3f88efb2f748239a1 Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 14 Jul 2020 22:33:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../book/read/page/provider/ImageProvider.kt | 46 +++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt index e3bb11bce..76d772edd 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt @@ -13,12 +13,30 @@ import java.io.FileOutputStream object ImageProvider { - val bitmaps = hashMapOf() + private val cache = hashMapOf>() + + @Synchronized + fun getCache(chapterIndex: Int, src: String): Bitmap? { + return cache[chapterIndex]?.get(src) + } + + @Synchronized + fun setCache(chapterIndex: Int, src: String, bitmap: Bitmap) { + var indexCache = cache[chapterIndex] + if (indexCache == null) { + indexCache = hashMapOf() + cache[chapterIndex] = indexCache + } + indexCache[src] = bitmap + } fun getImage(book: Book, chapterIndex: Int, src: String): Bitmap? { + getCache(chapterIndex, src)?.let { + return it + } val vFile = FileUtils.getFile( App.INSTANCE.externalFilesDir, - "${MD5Utils.md5Encode16(src)}.jpg", + "${MD5Utils.md5Encode16(src)}${src.substringAfterLast(".")}", "images", book.name ) if (!vFile.exists()) { @@ -36,14 +54,36 @@ object ImageProvider { } } return try { - BitmapUtils.decodeBitmap( + val bitmap = BitmapUtils.decodeBitmap( vFile.absolutePath, ChapterProvider.visibleWidth, ChapterProvider.visibleHeight ) + setCache(chapterIndex, src, bitmap) + bitmap } catch (e: Exception) { null } } + fun clearAllCache() { + cache.forEach {indexCache-> + indexCache.value.forEach { + it.value.recycle() + } + } + cache.clear() + } + + fun clearOut(chapterIndex: Int) { + cache.forEach {indexCache-> + if (indexCache.key !in chapterIndex - 1..chapterIndex + 1) { + indexCache.value.forEach { + it.value.recycle() + } + cache.remove(indexCache.key) + } + } + } + } \ No newline at end of file