From 55ed40c4b4bbdca9fb116c95fcd6e498e935c49f Mon Sep 17 00:00:00 2001 From: Horis <821938089@qq.com> Date: Thu, 22 Feb 2024 22:13:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/book/read/config/TipConfigDialog.kt | 12 +-- .../ui/book/read/page/entities/TextChapter.kt | 5 +- .../io/legado/app/ui/widget/BatteryView.kt | 78 +++++++++++++------ 3 files changed, 62 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/TipConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/TipConfigDialog.kt index 6862c7734..d78a2d9f0 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/TipConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/TipConfigDialog.kt @@ -130,7 +130,7 @@ class TipConfigDialog : BaseDialogFragment(R.layout.dialog_tip_config) { clearRepeat(tipValue) ReadTipConfig.tipHeaderLeft = tipValue tvHeaderLeft.text = ReadTipConfig.tipNames[i] - postEvent(EventBus.UP_CONFIG, arrayOf(2)) + postEvent(EventBus.UP_CONFIG, arrayOf(2, 6)) } } llHeaderMiddle.setOnClickListener { @@ -139,7 +139,7 @@ class TipConfigDialog : BaseDialogFragment(R.layout.dialog_tip_config) { clearRepeat(tipValue) ReadTipConfig.tipHeaderMiddle = tipValue tvHeaderMiddle.text = ReadTipConfig.tipNames[i] - postEvent(EventBus.UP_CONFIG, arrayOf(2)) + postEvent(EventBus.UP_CONFIG, arrayOf(2, 6)) } } llHeaderRight.setOnClickListener { @@ -148,7 +148,7 @@ class TipConfigDialog : BaseDialogFragment(R.layout.dialog_tip_config) { clearRepeat(tipValue) ReadTipConfig.tipHeaderRight = tipValue tvHeaderRight.text = ReadTipConfig.tipNames[i] - postEvent(EventBus.UP_CONFIG, arrayOf(2)) + postEvent(EventBus.UP_CONFIG, arrayOf(2, 6)) } } llFooterLeft.setOnClickListener { @@ -157,7 +157,7 @@ class TipConfigDialog : BaseDialogFragment(R.layout.dialog_tip_config) { clearRepeat(tipValue) ReadTipConfig.tipFooterLeft = tipValue tvFooterLeft.text = ReadTipConfig.tipNames[i] - postEvent(EventBus.UP_CONFIG, arrayOf(2)) + postEvent(EventBus.UP_CONFIG, arrayOf(2, 6)) } } llFooterMiddle.setOnClickListener { @@ -166,7 +166,7 @@ class TipConfigDialog : BaseDialogFragment(R.layout.dialog_tip_config) { clearRepeat(tipValue) ReadTipConfig.tipFooterMiddle = tipValue tvFooterMiddle.text = ReadTipConfig.tipNames[i] - postEvent(EventBus.UP_CONFIG, arrayOf(2)) + postEvent(EventBus.UP_CONFIG, arrayOf(2, 6)) } } llFooterRight.setOnClickListener { @@ -175,7 +175,7 @@ class TipConfigDialog : BaseDialogFragment(R.layout.dialog_tip_config) { clearRepeat(tipValue) ReadTipConfig.tipFooterRight = tipValue tvFooterRight.text = ReadTipConfig.tipNames[i] - postEvent(EventBus.UP_CONFIG, arrayOf(2)) + postEvent(EventBus.UP_CONFIG, arrayOf(2, 6)) } } llTipColor.setOnClickListener { diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt index 6779eca4c..57dc774ef 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt @@ -222,8 +222,9 @@ data class TextChapter( } // 判断是否已经排版到 charIndex ,没有则返回 -1 if (!isCompleted && index == size - 1) { - val line = pages[index].lines.first() - val pageEndPos = line.chapterPosition + line.charSize + val page = pages[index] + val line = page.lines.first() + val pageEndPos = line.chapterPosition + page.charSize if (charIndex > pageEndPos) { return -1 } diff --git a/app/src/main/java/io/legado/app/ui/widget/BatteryView.kt b/app/src/main/java/io/legado/app/ui/widget/BatteryView.kt index eb1dca7f5..baf7da5d0 100644 --- a/app/src/main/java/io/legado/app/ui/widget/BatteryView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/BatteryView.kt @@ -6,7 +6,10 @@ import android.graphics.Canvas import android.graphics.Paint import android.graphics.Rect import android.graphics.Typeface +import android.text.SpannableStringBuilder +import android.text.Spanned import android.text.StaticLayout +import android.text.style.LineHeightSpan import android.util.AttributeSet import androidx.annotation.ColorInt import androidx.appcompat.widget.AppCompatTextView @@ -25,6 +28,13 @@ class BatteryView @JvmOverloads constructor( private val outFrame = Rect() private val polar = Rect() private val canvasRecorder = CanvasRecorderFactory.create() + private val batterySpan = LineHeightSpan { _, _, _, _, _, fm -> + fm.top = -22 + fm.ascent = -28 + fm.descent = 7 + fm.bottom = 1 + fm.leading = 0 + } var isBattery = false set(value) { field = value @@ -58,42 +68,60 @@ class BatteryView @JvmOverloads constructor( fun setBattery(battery: Int, text: String? = null) { this.battery = battery if (text.isNullOrEmpty()) { - setText(battery.toString()) + setText(getBatteryText(battery.toString())) } else { - setText("$text $battery") + setText(getBatteryText("$text $battery")) } } override fun onDraw(canvas: Canvas) { + if (canvas.isHardwareAccelerated) { + canvasRecorder.invalidate() + } canvasRecorder.recordIfNeededThenDraw(canvas, width, height) { super.onDraw(this) if (!isBattery) return@recordIfNeededThenDraw - layout.getLineBounds(0, outFrame) - val batteryStart = layout - .getPrimaryHorizontal(text.length - battery.toString().length) - .toInt() + 2.dpToPx() - val batteryEnd = batteryStart + - StaticLayout.getDesiredWidth(battery.toString(), paint).toInt() + 4.dpToPx() - outFrame.set( - batteryStart, - 2.dpToPx(), - batteryEnd, - height - 2.dpToPx() - ) - val dj = (outFrame.bottom - outFrame.top) / 3 - polar.set( - batteryEnd, - outFrame.top + dj, - batteryEnd + 2.dpToPx(), - outFrame.bottom - dj - ) - batteryPaint.style = Paint.Style.STROKE - drawRect(outFrame, batteryPaint) - batteryPaint.style = Paint.Style.FILL - drawRect(polar, batteryPaint) + drawBattery(this) } } + private fun drawBattery(canvas: Canvas) { + layout.getLineBounds(0, outFrame) + val batteryStart = layout + .getPrimaryHorizontal(text.length - battery.toString().length) + .toInt() + 2.dpToPx() + val batteryEnd = batteryStart + + StaticLayout.getDesiredWidth(battery.toString(), paint).toInt() + 4.dpToPx() + outFrame.set( + batteryStart, + 2.dpToPx(), + batteryEnd, + height - 2.dpToPx() + ) + val dj = (outFrame.bottom - outFrame.top) / 3 + polar.set( + batteryEnd, + outFrame.top + dj, + batteryEnd + 2.dpToPx(), + outFrame.bottom - dj + ) + batteryPaint.style = Paint.Style.STROKE + canvas.drawRect(outFrame, batteryPaint) + batteryPaint.style = Paint.Style.FILL + canvas.drawRect(polar, batteryPaint) + } + + private fun getBatteryText(text: CharSequence?): SpannableStringBuilder? { + if (text == null) { + return null + } + + return SpannableStringBuilder(text).apply { + setSpan(batterySpan, 0, text.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) + } + } + + override fun invalidate() { super.invalidate() kotlin.runCatching {