mirror of
https://github.com/gedoor/legado.git
synced 2024-08-30 09:23:26 +08:00
优化
This commit is contained in:
parent
2d7e3cfe04
commit
55ed40c4b4
@ -130,7 +130,7 @@ class TipConfigDialog : BaseDialogFragment(R.layout.dialog_tip_config) {
|
|||||||
clearRepeat(tipValue)
|
clearRepeat(tipValue)
|
||||||
ReadTipConfig.tipHeaderLeft = tipValue
|
ReadTipConfig.tipHeaderLeft = tipValue
|
||||||
tvHeaderLeft.text = ReadTipConfig.tipNames[i]
|
tvHeaderLeft.text = ReadTipConfig.tipNames[i]
|
||||||
postEvent(EventBus.UP_CONFIG, arrayOf(2))
|
postEvent(EventBus.UP_CONFIG, arrayOf(2, 6))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
llHeaderMiddle.setOnClickListener {
|
llHeaderMiddle.setOnClickListener {
|
||||||
@ -139,7 +139,7 @@ class TipConfigDialog : BaseDialogFragment(R.layout.dialog_tip_config) {
|
|||||||
clearRepeat(tipValue)
|
clearRepeat(tipValue)
|
||||||
ReadTipConfig.tipHeaderMiddle = tipValue
|
ReadTipConfig.tipHeaderMiddle = tipValue
|
||||||
tvHeaderMiddle.text = ReadTipConfig.tipNames[i]
|
tvHeaderMiddle.text = ReadTipConfig.tipNames[i]
|
||||||
postEvent(EventBus.UP_CONFIG, arrayOf(2))
|
postEvent(EventBus.UP_CONFIG, arrayOf(2, 6))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
llHeaderRight.setOnClickListener {
|
llHeaderRight.setOnClickListener {
|
||||||
@ -148,7 +148,7 @@ class TipConfigDialog : BaseDialogFragment(R.layout.dialog_tip_config) {
|
|||||||
clearRepeat(tipValue)
|
clearRepeat(tipValue)
|
||||||
ReadTipConfig.tipHeaderRight = tipValue
|
ReadTipConfig.tipHeaderRight = tipValue
|
||||||
tvHeaderRight.text = ReadTipConfig.tipNames[i]
|
tvHeaderRight.text = ReadTipConfig.tipNames[i]
|
||||||
postEvent(EventBus.UP_CONFIG, arrayOf(2))
|
postEvent(EventBus.UP_CONFIG, arrayOf(2, 6))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
llFooterLeft.setOnClickListener {
|
llFooterLeft.setOnClickListener {
|
||||||
@ -157,7 +157,7 @@ class TipConfigDialog : BaseDialogFragment(R.layout.dialog_tip_config) {
|
|||||||
clearRepeat(tipValue)
|
clearRepeat(tipValue)
|
||||||
ReadTipConfig.tipFooterLeft = tipValue
|
ReadTipConfig.tipFooterLeft = tipValue
|
||||||
tvFooterLeft.text = ReadTipConfig.tipNames[i]
|
tvFooterLeft.text = ReadTipConfig.tipNames[i]
|
||||||
postEvent(EventBus.UP_CONFIG, arrayOf(2))
|
postEvent(EventBus.UP_CONFIG, arrayOf(2, 6))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
llFooterMiddle.setOnClickListener {
|
llFooterMiddle.setOnClickListener {
|
||||||
@ -166,7 +166,7 @@ class TipConfigDialog : BaseDialogFragment(R.layout.dialog_tip_config) {
|
|||||||
clearRepeat(tipValue)
|
clearRepeat(tipValue)
|
||||||
ReadTipConfig.tipFooterMiddle = tipValue
|
ReadTipConfig.tipFooterMiddle = tipValue
|
||||||
tvFooterMiddle.text = ReadTipConfig.tipNames[i]
|
tvFooterMiddle.text = ReadTipConfig.tipNames[i]
|
||||||
postEvent(EventBus.UP_CONFIG, arrayOf(2))
|
postEvent(EventBus.UP_CONFIG, arrayOf(2, 6))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
llFooterRight.setOnClickListener {
|
llFooterRight.setOnClickListener {
|
||||||
@ -175,7 +175,7 @@ class TipConfigDialog : BaseDialogFragment(R.layout.dialog_tip_config) {
|
|||||||
clearRepeat(tipValue)
|
clearRepeat(tipValue)
|
||||||
ReadTipConfig.tipFooterRight = tipValue
|
ReadTipConfig.tipFooterRight = tipValue
|
||||||
tvFooterRight.text = ReadTipConfig.tipNames[i]
|
tvFooterRight.text = ReadTipConfig.tipNames[i]
|
||||||
postEvent(EventBus.UP_CONFIG, arrayOf(2))
|
postEvent(EventBus.UP_CONFIG, arrayOf(2, 6))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
llTipColor.setOnClickListener {
|
llTipColor.setOnClickListener {
|
||||||
|
@ -222,8 +222,9 @@ data class TextChapter(
|
|||||||
}
|
}
|
||||||
// 判断是否已经排版到 charIndex ,没有则返回 -1
|
// 判断是否已经排版到 charIndex ,没有则返回 -1
|
||||||
if (!isCompleted && index == size - 1) {
|
if (!isCompleted && index == size - 1) {
|
||||||
val line = pages[index].lines.first()
|
val page = pages[index]
|
||||||
val pageEndPos = line.chapterPosition + line.charSize
|
val line = page.lines.first()
|
||||||
|
val pageEndPos = line.chapterPosition + page.charSize
|
||||||
if (charIndex > pageEndPos) {
|
if (charIndex > pageEndPos) {
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,10 @@ import android.graphics.Canvas
|
|||||||
import android.graphics.Paint
|
import android.graphics.Paint
|
||||||
import android.graphics.Rect
|
import android.graphics.Rect
|
||||||
import android.graphics.Typeface
|
import android.graphics.Typeface
|
||||||
|
import android.text.SpannableStringBuilder
|
||||||
|
import android.text.Spanned
|
||||||
import android.text.StaticLayout
|
import android.text.StaticLayout
|
||||||
|
import android.text.style.LineHeightSpan
|
||||||
import android.util.AttributeSet
|
import android.util.AttributeSet
|
||||||
import androidx.annotation.ColorInt
|
import androidx.annotation.ColorInt
|
||||||
import androidx.appcompat.widget.AppCompatTextView
|
import androidx.appcompat.widget.AppCompatTextView
|
||||||
@ -25,6 +28,13 @@ class BatteryView @JvmOverloads constructor(
|
|||||||
private val outFrame = Rect()
|
private val outFrame = Rect()
|
||||||
private val polar = Rect()
|
private val polar = Rect()
|
||||||
private val canvasRecorder = CanvasRecorderFactory.create()
|
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
|
var isBattery = false
|
||||||
set(value) {
|
set(value) {
|
||||||
field = value
|
field = value
|
||||||
@ -58,42 +68,60 @@ class BatteryView @JvmOverloads constructor(
|
|||||||
fun setBattery(battery: Int, text: String? = null) {
|
fun setBattery(battery: Int, text: String? = null) {
|
||||||
this.battery = battery
|
this.battery = battery
|
||||||
if (text.isNullOrEmpty()) {
|
if (text.isNullOrEmpty()) {
|
||||||
setText(battery.toString())
|
setText(getBatteryText(battery.toString()))
|
||||||
} else {
|
} else {
|
||||||
setText("$text $battery")
|
setText(getBatteryText("$text $battery"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDraw(canvas: Canvas) {
|
override fun onDraw(canvas: Canvas) {
|
||||||
|
if (canvas.isHardwareAccelerated) {
|
||||||
|
canvasRecorder.invalidate()
|
||||||
|
}
|
||||||
canvasRecorder.recordIfNeededThenDraw(canvas, width, height) {
|
canvasRecorder.recordIfNeededThenDraw(canvas, width, height) {
|
||||||
super.onDraw(this)
|
super.onDraw(this)
|
||||||
if (!isBattery) return@recordIfNeededThenDraw
|
if (!isBattery) return@recordIfNeededThenDraw
|
||||||
layout.getLineBounds(0, outFrame)
|
drawBattery(this)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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() {
|
override fun invalidate() {
|
||||||
super.invalidate()
|
super.invalidate()
|
||||||
kotlin.runCatching {
|
kotlin.runCatching {
|
||||||
|
Loading…
Reference in New Issue
Block a user