diff --git a/app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt b/app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt index ace52897c..a27bb3179 100644 --- a/app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt +++ b/app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt @@ -37,7 +37,6 @@ object ReadBookConfig { if (shareLayout) { shareConfig = value } - upBg() } var bg: Drawable? = null @@ -88,11 +87,8 @@ object ReadBookConfig { shareConfig = c ?: configList.getOrNull(5) ?: Config() } - fun upBg() { - val resources = appCtx.resources - val dm = resources.displayMetrics - val width = dm.widthPixels - val height = dm.heightPixels + fun upBg(width: Int, height: Int) { + val tmp = bg bg = durConfig.curBgDrawable(width, height).apply { if (this is BitmapDrawable) { bgMeanColor = bitmap.getMeanColor() @@ -100,6 +96,7 @@ object ReadBookConfig { bgMeanColor = color } } + (tmp as? BitmapDrawable)?.bitmap?.recycle() } fun save() { @@ -123,7 +120,6 @@ object ReadBookConfig { if (styleSelect > 0) { styleSelect -= 1 } - upBg() return true } return false @@ -572,11 +568,11 @@ object ReadBookConfig { 1 -> { val path = "bg" + File.separator + curBgStr() val bitmap = BitmapUtils.decodeAssetsBitmap(appCtx, path, width, height) - BitmapDrawable(resources, bitmap) + BitmapDrawable(resources, bitmap?.copyAndRecycle(width, height)) } else -> { val bitmap = BitmapUtils.decodeBitmap(curBgStr(), width, height) - BitmapDrawable(resources, bitmap) + BitmapDrawable(resources, bitmap?.copyAndRecycle(width, height)) } } } catch (e: OutOfMemoryError) { diff --git a/app/src/main/java/io/legado/app/help/config/ThemeConfig.kt b/app/src/main/java/io/legado/app/help/config/ThemeConfig.kt index 01a012bc5..8394e0fce 100644 --- a/app/src/main/java/io/legado/app/help/config/ThemeConfig.kt +++ b/app/src/main/java/io/legado/app/help/config/ThemeConfig.kt @@ -38,7 +38,6 @@ object ThemeConfig { } fun applyDayNight(context: Context) { - ReadBookConfig.upBg() applyTheme(context) initNightMode() BookCover.upDefaultCover() diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index c507cb2cf..1ca3712d9 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -984,7 +984,6 @@ class ReadBookActivity : BaseReadBookActivity(), } BG_COLOR -> { setCurBg(0, "#${color.hexString}") - ReadBookConfig.upBg() postEvent(EventBus.UP_CONFIG, false) } TIP_COLOR -> { diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/BgAdapter.kt b/app/src/main/java/io/legado/app/ui/book/read/config/BgAdapter.kt index 81cacd945..1dfb26983 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/BgAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/BgAdapter.kt @@ -41,7 +41,6 @@ class BgAdapter(context: Context, val textColor: Int) : this.setOnClickListener { getItemByLayoutPosition(holder.layoutPosition)?.let { ReadBookConfig.durConfig.setCurBg(1, it) - ReadBookConfig.upBg() postEvent(EventBus.UP_CONFIG, false) } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt index e1cd8a329..4105d7ea5 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt @@ -358,7 +358,6 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) { inputStream.copyTo(outputStream) } ReadBookConfig.durConfig.setCurBg(2, file.absolutePath) - ReadBookConfig.upBg() postEvent(EventBus.UP_CONFIG, false) } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt index eca08ee46..840c80bd3 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt @@ -158,7 +158,6 @@ class ReadStyleDialog : BaseDialogFragment(R.layout.dialog_read_book_style), val oldIndex = ReadBookConfig.styleSelect if (index != oldIndex) { ReadBookConfig.styleSelect = index - ReadBookConfig.upBg() upView() styleAdapter.notifyItemChanged(oldIndex) styleAdapter.notifyItemChanged(index) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt index 062384559..13bc4c849 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt @@ -2,8 +2,6 @@ package io.legado.app.ui.book.read.page import android.annotation.SuppressLint import android.content.Context -import android.graphics.Bitmap -import android.graphics.drawable.Drawable import android.view.LayoutInflater import android.widget.FrameLayout import androidx.core.view.isGone @@ -41,7 +39,6 @@ class PageView(context: Context) : FrameLayout(context) { private var tvBookName: BatteryView? = null private var tvTimeBattery: BatteryView? = null private var tvTimeBatteryP: BatteryView? = null - private var bitmap: Bitmap? = null val headerHeight: Int get() { @@ -61,13 +58,9 @@ class PageView(context: Context) : FrameLayout(context) { } } - fun getBitmap(): Bitmap? { - return bitmap?.copy(Bitmap.Config.ARGB_8888, false) - } - - private fun upBitmap() { - bitmap?.recycle() - bitmap = screenshot() + override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { + super.onSizeChanged(w, h, oldw, oldh) + upBg() } fun upStyle() = binding.run { @@ -208,9 +201,9 @@ class PageView(context: Context) : FrameLayout(context) { } } - fun setBg(bg: Drawable?) { + fun upBg() { binding.vwRoot.backgroundColor = ReadBookConfig.bgMeanColor - binding.vwBg.background = bg + binding.vwBg.background = ReadBookConfig.bg upBgAlpha() } @@ -236,7 +229,6 @@ class PageView(context: Context) : FrameLayout(context) { val time = timeFormat.format(Date(System.currentTimeMillis())) tvTimeBattery?.setBattery(battery, time) tvTimeBatteryP?.text = "$time $battery%" - upBitmap() } fun setContent(textPage: TextPage, resetPageOffset: Boolean = true) { @@ -245,7 +237,6 @@ class PageView(context: Context) : FrameLayout(context) { resetPageOffset() } binding.contentTextView.setContent(textPage) - upBitmap() } fun setContentDescription(content: String) { diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt index 8dd1d1f6b..cc0cff8ae 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt @@ -489,12 +489,10 @@ class ReadView(context: Context, attrs: AttributeSet) : } fun upBg() { - ReadBookConfig.bg ?: let { - ReadBookConfig.upBg() - } - curPage.setBg(ReadBookConfig.bg) - prevPage.setBg(ReadBookConfig.bg) - nextPage.setBg(ReadBookConfig.bg) + ReadBookConfig.upBg(width, height) + curPage.upBg() + prevPage.upBg() + nextPage.upBg() } fun upBgAlpha() { diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/delegate/HorizontalPageDelegate.kt b/app/src/main/java/io/legado/app/ui/book/read/page/delegate/HorizontalPageDelegate.kt index 737c238c6..0d70b03f2 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/delegate/HorizontalPageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/delegate/HorizontalPageDelegate.kt @@ -4,6 +4,7 @@ import android.graphics.Bitmap import android.view.MotionEvent import io.legado.app.ui.book.read.page.ReadView import io.legado.app.ui.book.read.page.entities.PageDirection +import io.legado.app.utils.screenshot abstract class HorizontalPageDelegate(readView: ReadView) : PageDelegate(readView) { @@ -20,15 +21,15 @@ abstract class HorizontalPageDelegate(readView: ReadView) : PageDelegate(readVie when (mDirection) { PageDirection.PREV -> { prevBitmap?.recycle() - prevBitmap = prevPage.getBitmap() + prevBitmap = prevPage.screenshot() curBitmap?.recycle() - curBitmap = curPage.getBitmap() + curBitmap = curPage.screenshot() } PageDirection.NEXT -> { nextBitmap?.recycle() - nextBitmap = nextPage.getBitmap() + nextBitmap = nextPage.screenshot() curBitmap?.recycle() - curBitmap = curPage.getBitmap() + curBitmap = curPage.screenshot() } else -> Unit } diff --git a/app/src/main/java/io/legado/app/utils/BitmapUtils.kt b/app/src/main/java/io/legado/app/utils/BitmapUtils.kt index e2407c95e..dee6ab206 100644 --- a/app/src/main/java/io/legado/app/utils/BitmapUtils.kt +++ b/app/src/main/java/io/legado/app/utils/BitmapUtils.kt @@ -210,27 +210,27 @@ object BitmapUtils { } -fun Bitmap.changeSize(newWidth: Int, newHeight: Int): Bitmap { +/** + * 获取指定宽高的图片 + */ +fun Bitmap.copyAndRecycle(newWidth: Int, newHeight: Int): Bitmap { val width = this.width val height = this.height //计算压缩的比率 - var scaleWidth = newWidth.toFloat() / width - var scaleHeight = newHeight.toFloat() / height - - if (scaleWidth > scaleHeight) { - scaleWidth = scaleHeight - } else { - scaleHeight = scaleWidth - } + val scaleWidth = newWidth.toFloat() / width + val scaleHeight = newHeight.toFloat() / height //获取想要缩放的matrix val matrix = Matrix() matrix.postScale(scaleWidth, scaleHeight) //获取新的bitmap - return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true) + val bitmap = Bitmap.createBitmap(this, 0, 0, width, height, matrix, true) + + recycle() + return bitmap } /** diff --git a/app/src/main/java/io/legado/app/utils/QRCodeUtils.kt b/app/src/main/java/io/legado/app/utils/QRCodeUtils.kt index a6a8747a0..072a2f725 100644 --- a/app/src/main/java/io/legado/app/utils/QRCodeUtils.kt +++ b/app/src/main/java/io/legado/app/utils/QRCodeUtils.kt @@ -182,7 +182,7 @@ object QRCodeUtils { hints: Map = DecodeFormatManager.ALL_HINTS ): Result? { if (bitmap.width > reqWidth || bitmap.height > reqHeight) { - val bm = bitmap.changeSize(reqWidth, reqHeight) + val bm = bitmap.copyAndRecycle(reqWidth, reqHeight) return parseCodeResult(getRGBLuminanceSource(bm), hints) } return parseCodeResult(getRGBLuminanceSource(bitmap), hints)