Cleanup code

This commit is contained in:
tarsin committed 2023-01-18 18:50:38 +08:00
1 parent c1533ec66d
commit abda781f02
2 files changed
+19 -27

No files matched your search

+18 -24
View File
@@ -29,36 +29,30 @@ import java.nio.ByteBuffer
import kotlin.math.min
class Image private constructor(source: Source, private var src: ByteBufferSource?) {
var mObtainedDrawable: Drawable?
init {
mObtainedDrawable =
ImageDecoder.decodeDrawable(source) { decoder: ImageDecoder, info: ImageInfo, _: Source ->
decoder.setTargetColorSpace(colorSpace)
decoder.setTargetSampleSize(
min(
info.size.width / (2 * screenWidth),
info.size.height / (2 * screenHeight)
).coerceAtLeast(1)
)
var mObtainedDrawable: Drawable? =
ImageDecoder.decodeDrawable(source) { decoder: ImageDecoder, info: ImageInfo, _: Source ->
decoder.setTargetColorSpace(colorSpace)
decoder.setTargetSampleSize(
min(
info.size.width / (2 * screenWidth),
info.size.height / (2 * screenHeight)
).coerceAtLeast(1)
)
}.also {
(it as? BitmapDrawable)?.run {
src?.close()
src = null
}
if (mObtainedDrawable is BitmapDrawable) {
src?.close()
src = null
}
}
private set
val width: Int
get() = mObtainedDrawable?.intrinsicWidth ?: 0
val height: Int
get() = mObtainedDrawable?.intrinsicHeight ?: 0
val size: Int
get() = mObtainedDrawable!!.run { intrinsicHeight * intrinsicWidth * 4 * if (this is Animatable) 4 else 1 }
@Synchronized
fun recycle() {
(mObtainedDrawable as? Animatable)?.run {
stop()
}
(mObtainedDrawable as? BitmapDrawable)?.run { bitmap.recycle() }
(mObtainedDrawable as? Animatable)?.stop()
(mObtainedDrawable as? BitmapDrawable)?.bitmap?.recycle()
mObtainedDrawable?.callback = null
mObtainedDrawable = null
src?.close()
@@ -1,6 +1,5 @@
package eu.kanade.tachiyomi.ui.reader.loader
import android.graphics.drawable.Animatable
import androidx.annotation.CallSuper
import androidx.collection.LruCache
import com.hippo.image.Image
@@ -100,8 +99,7 @@ abstract class PageLoader {
}
override fun sizeOf(key: Int, value: Image): Int {
if (value.mObtainedDrawable is Animatable) return value.height * value.width * 15
return value.height * value.width * 4
return value.size
}
override fun entryRemoved(evicted: Boolean, key: Int, oldValue: Image, newValue: Image?) {