From 8a6bfcda0284b15fa0d2ac41495360be12d1953f Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 23 May 2022 15:37:54 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/ui/book/read/page/PageView.kt | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) 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..6f8fac6eb 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 @@ -14,6 +14,7 @@ import io.legado.app.data.entities.Bookmark import io.legado.app.databinding.ViewBookPageBinding import io.legado.app.help.config.ReadBookConfig import io.legado.app.help.config.ReadTipConfig +import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.ReadBook import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.ui.book.read.page.entities.TextPage @@ -66,8 +67,14 @@ class PageView(context: Context) : FrameLayout(context) { } private fun upBitmap() { - bitmap?.recycle() - bitmap = screenshot() + post { + Coroutine.async { + screenshot() + }.onSuccess { + bitmap?.recycle() + bitmap = screenshot() + } + } } fun upStyle() = binding.run { From 773458f0a998da7894a1db99e9ac3eb326ee1012 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 23 May 2022 16:06:45 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/ui/book/read/page/PageView.kt | 20 ------------------- .../page/delegate/HorizontalPageDelegate.kt | 9 +++++---- 2 files changed, 5 insertions(+), 24 deletions(-) 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 6f8fac6eb..9f1fd01ed 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,7 +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 @@ -14,7 +13,6 @@ import io.legado.app.data.entities.Bookmark import io.legado.app.databinding.ViewBookPageBinding import io.legado.app.help.config.ReadBookConfig import io.legado.app.help.config.ReadTipConfig -import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.ReadBook import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.ui.book.read.page.entities.TextPage @@ -42,7 +40,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() { @@ -62,21 +59,6 @@ class PageView(context: Context) : FrameLayout(context) { } } - fun getBitmap(): Bitmap? { - return bitmap?.copy(Bitmap.Config.ARGB_8888, false) - } - - private fun upBitmap() { - post { - Coroutine.async { - screenshot() - }.onSuccess { - bitmap?.recycle() - bitmap = screenshot() - } - } - } - fun upStyle() = binding.run { upTipStyle() ReadBookConfig.let { @@ -243,7 +225,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) { @@ -252,7 +233,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/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 } From c619dd5177b1368f3a3fbf724776927f23d2a5a9 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 23 May 2022 16:44:15 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/help/config/ReadBookConfig.kt | 14 +++++--------- .../java/io/legado/app/help/config/ThemeConfig.kt | 1 - .../io/legado/app/ui/book/read/ReadBookActivity.kt | 1 - .../io/legado/app/ui/book/read/config/BgAdapter.kt | 1 - .../app/ui/book/read/config/BgTextConfigDialog.kt | 1 - .../app/ui/book/read/config/ReadStyleDialog.kt | 1 - .../io/legado/app/ui/book/read/page/PageView.kt | 10 +++++++--- .../io/legado/app/ui/book/read/page/ReadView.kt | 10 ++++------ 8 files changed, 16 insertions(+), 23 deletions(-) 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..e04af5b86 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?.changeSize(width, height)) } else -> { val bitmap = BitmapUtils.decodeBitmap(curBgStr(), width, height) - BitmapDrawable(resources, bitmap) + BitmapDrawable(resources, bitmap?.changeSize(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 9f1fd01ed..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,7 +2,6 @@ package io.legado.app.ui.book.read.page import android.annotation.SuppressLint import android.content.Context -import android.graphics.drawable.Drawable import android.view.LayoutInflater import android.widget.FrameLayout import androidx.core.view.isGone @@ -59,6 +58,11 @@ class PageView(context: Context) : FrameLayout(context) { } } + override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { + super.onSizeChanged(w, h, oldw, oldh) + upBg() + } + fun upStyle() = binding.run { upTipStyle() ReadBookConfig.let { @@ -197,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() } 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 c96eb6fed..350436c5a 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 @@ -486,12 +486,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() { From 2d2f27800ef5ede1683bfc5c58a8da6d894a7630 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 23 May 2022 17:09:16 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/help/config/ReadBookConfig.kt | 4 ++-- .../java/io/legado/app/utils/BitmapUtils.kt | 20 +++++++++---------- .../java/io/legado/app/utils/QRCodeUtils.kt | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) 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 e04af5b86..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 @@ -568,11 +568,11 @@ object ReadBookConfig { 1 -> { val path = "bg" + File.separator + curBgStr() val bitmap = BitmapUtils.decodeAssetsBitmap(appCtx, path, width, height) - BitmapDrawable(resources, bitmap?.changeSize(width, height)) + BitmapDrawable(resources, bitmap?.copyAndRecycle(width, height)) } else -> { val bitmap = BitmapUtils.decodeBitmap(curBgStr(), width, height) - BitmapDrawable(resources, bitmap?.changeSize(width, height)) + BitmapDrawable(resources, bitmap?.copyAndRecycle(width, height)) } } } catch (e: OutOfMemoryError) { 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) From 8cdd5b990bbe695e86558d721824a0aced1d37a4 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 23 May 2022 19:45:35 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 350436c5a..a114eace1 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 @@ -98,7 +98,6 @@ class ReadView(context: Context, attrs: AttributeSet) : addView(curPage) addView(prevPage) if (!isInEditMode) { - upBg() setWillNotDraw(false) upPageAnim() } @@ -121,6 +120,9 @@ class ReadView(context: Context, attrs: AttributeSet) : setRect9x() prevPage.x = -w.toFloat() pageDelegate?.setViewSize(w, h) + if (w > 0 && h > 0) { + upBg() + } } override fun dispatchDraw(canvas: Canvas) {