Kaynağa Gözat

Merge remote-tracking branch 'origin/master'

kunfei 4 yıl önce
ebeveyn
işleme
5c82513065

+ 5 - 9
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) {

+ 0 - 1
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()

+ 0 - 1
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 -> {

+ 0 - 1
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)
                 }
             }

+ 0 - 1
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)
         }
     }

+ 0 - 1
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)

+ 5 - 14
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) {

+ 4 - 6
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() {

+ 5 - 4
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
         }

+ 10 - 10
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
 }
 
 /**

+ 1 - 1
app/src/main/java/io/legado/app/utils/QRCodeUtils.kt

@@ -182,7 +182,7 @@ object QRCodeUtils {
         hints: Map<DecodeHintType?, Any?> = 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)