feat: 优化代码

pull/133/head
kunfei 5 years ago
parent af7f21dc5c
commit 0f97c19cd4
  1. 8
      app/src/main/java/io/legado/app/help/ReadBookConfig.kt
  2. 2
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/SimulationPageDelegate.kt
  3. 33
      app/src/main/java/io/legado/app/utils/BitmapUtils.kt

@ -27,6 +27,7 @@ object ReadBookConfig {
val durConfig get() = getConfig(styleSelect) val durConfig get() = getConfig(styleSelect)
private val shareConfig get() = getConfig(5) private val shareConfig get() = getConfig(5)
var bg: Drawable? = null var bg: Drawable? = null
var bgMeanColor: Int = 0
init { init {
upConfig() upConfig()
@ -68,6 +69,13 @@ object ReadBookConfig {
val width = dm.widthPixels val width = dm.widthPixels
val height = dm.heightPixels val height = dm.heightPixels
bg = durConfig.bgDrawable(width, height) 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() { fun save() {

@ -3,6 +3,7 @@ package io.legado.app.ui.book.read.page.delegate
import android.graphics.* import android.graphics.*
import android.graphics.drawable.GradientDrawable import android.graphics.drawable.GradientDrawable
import android.os.Build import android.os.Build
import io.legado.app.help.ReadBookConfig
import io.legado.app.ui.book.read.page.PageView import io.legado.app.ui.book.read.page.PageView
import kotlin.math.* import kotlin.math.*
@ -268,6 +269,7 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi
mMatrix.setValues(mMatrixArray) mMatrix.setValues(mMatrixArray)
mMatrix.preTranslate(-mBezierControl1.x, -mBezierControl1.y) mMatrix.preTranslate(-mBezierControl1.x, -mBezierControl1.y)
mMatrix.postTranslate(mBezierControl1.x, mBezierControl1.y) mMatrix.postTranslate(mBezierControl1.x, mBezierControl1.y)
canvas.drawColor(ReadBookConfig.bgMeanColor)
canvas.drawBitmap(bitmap, mMatrix, mPaint) canvas.drawBitmap(bitmap, mMatrix, mPaint)
mPaint.colorFilter = null mPaint.colorFilter = null
canvas.rotate(mDegrees, mBezierStart1.x, mBezierStart1.y) canvas.rotate(mDegrees, mBezierStart1.x, mBezierStart1.y)

@ -5,6 +5,7 @@ import android.graphics.Bitmap
import android.graphics.Bitmap.Config import android.graphics.Bitmap.Config
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.Color
import android.renderscript.Allocation import android.renderscript.Allocation
import android.renderscript.Element import android.renderscript.Element
import android.renderscript.RenderScript import android.renderscript.RenderScript
@ -12,10 +13,7 @@ import android.renderscript.ScriptIntrinsicBlur
import android.view.View import android.view.View
import io.legado.app.App import io.legado.app.App
import java.io.IOException import java.io.IOException
import kotlin.math.ceil import kotlin.math.*
import kotlin.math.floor
import kotlin.math.min
import kotlin.math.sqrt
@Suppress("unused", "WeakerAccess") @Suppress("unused", "WeakerAccess")
@ -257,4 +255,31 @@ object BitmapUtils {
return blurredBitmap 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
)
}
} }

Loading…
Cancel
Save