diff --git a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt index a3a6de521..0801f88dd 100644 --- a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt +++ b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt @@ -27,6 +27,7 @@ object ReadBookConfig { val durConfig get() = getConfig(styleSelect) private val shareConfig get() = getConfig(5) var bg: Drawable? = null + var bgMeanColor: Int = 0 init { upConfig() @@ -68,6 +69,13 @@ object ReadBookConfig { val width = dm.widthPixels val height = dm.heightPixels bg = durConfig.bgDrawable(width, height) + bg?.let { + if (it is BitmapDrawable) { + bgMeanColor = BitmapUtils.getMeanColor(it.bitmap) + } else if (it is ColorDrawable) { + bgMeanColor = it.color + } + } } fun save() { diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/delegate/SimulationPageDelegate.kt b/app/src/main/java/io/legado/app/ui/book/read/page/delegate/SimulationPageDelegate.kt index c5e7d40a4..616a41614 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/delegate/SimulationPageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/delegate/SimulationPageDelegate.kt @@ -3,6 +3,7 @@ package io.legado.app.ui.book.read.page.delegate import android.graphics.* import android.graphics.drawable.GradientDrawable import android.os.Build +import io.legado.app.help.ReadBookConfig import io.legado.app.ui.book.read.page.PageView import kotlin.math.* @@ -268,6 +269,7 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi mMatrix.setValues(mMatrixArray) mMatrix.preTranslate(-mBezierControl1.x, -mBezierControl1.y) mMatrix.postTranslate(mBezierControl1.x, mBezierControl1.y) + canvas.drawColor(ReadBookConfig.bgMeanColor) canvas.drawBitmap(bitmap, mMatrix, mPaint) mPaint.colorFilter = null canvas.rotate(mDegrees, mBezierStart1.x, mBezierStart1.y) 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 371be8122..138a0c063 100644 --- a/app/src/main/java/io/legado/app/utils/BitmapUtils.kt +++ b/app/src/main/java/io/legado/app/utils/BitmapUtils.kt @@ -5,6 +5,7 @@ import android.graphics.Bitmap import android.graphics.Bitmap.Config import android.graphics.BitmapFactory import android.graphics.Canvas +import android.graphics.Color import android.renderscript.Allocation import android.renderscript.Element import android.renderscript.RenderScript @@ -12,10 +13,7 @@ import android.renderscript.ScriptIntrinsicBlur import android.view.View import io.legado.app.App import java.io.IOException -import kotlin.math.ceil -import kotlin.math.floor -import kotlin.math.min -import kotlin.math.sqrt +import kotlin.math.* @Suppress("unused", "WeakerAccess") @@ -257,4 +255,31 @@ object BitmapUtils { return blurredBitmap } + fun getMeanColor(bitmap: Bitmap): Int { + val width: Int = bitmap.width + val height: Int = bitmap.height + var pixel: Int + var pixelSumRed = 0 + var pixelSumBlue = 0 + var pixelSumGreen = 0 + for (i in 0..99) { + for (j in 70..99) { + pixel = bitmap.getPixel( + (i * width / 100.toFloat()).roundToInt(), + (j * height / 100.toFloat()).roundToInt() + ) + pixelSumRed += Color.red(pixel) + pixelSumGreen += Color.green(pixel) + pixelSumBlue += Color.blue(pixel) + } + } + val averagePixelRed = pixelSumRed / 3000 + val averagePixelBlue = pixelSumBlue / 3000 + val averagePixelGreen = pixelSumGreen / 3000 + return Color.rgb( + averagePixelRed + 3, + averagePixelGreen + 3, averagePixelBlue + 3 + ) + } + }