mirror of
https://github.com/gedoor/legado.git
synced 2024-07-06 23:47:49 +08:00
Compare commits
2 Commits
9a8dba8820
...
fa9e9c6f91
Author | SHA1 | Date | |
---|---|---|---|
|
fa9e9c6f91 | ||
|
72ad27ea84 |
@ -6,10 +6,8 @@ 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.os.Build
|
||||||
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
|
||||||
@ -29,13 +27,6 @@ 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
|
||||||
@ -48,6 +39,9 @@ class BatteryView @JvmOverloads constructor(
|
|||||||
|
|
||||||
init {
|
init {
|
||||||
setPadding(4.dpToPx(), 3.dpToPx(), 6.dpToPx(), 3.dpToPx())
|
setPadding(4.dpToPx(), 3.dpToPx(), 6.dpToPx(), 3.dpToPx())
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||||
|
isFallbackLineSpacing = false
|
||||||
|
}
|
||||||
batteryPaint.strokeWidth = 1f.dpToPx()
|
batteryPaint.strokeWidth = 1f.dpToPx()
|
||||||
batteryPaint.isAntiAlias = true
|
batteryPaint.isAntiAlias = true
|
||||||
batteryPaint.color = paint.color
|
batteryPaint.color = paint.color
|
||||||
@ -69,9 +63,9 @@ 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(getBatteryText(battery.toString()))
|
setText(battery.toString())
|
||||||
} else {
|
} else {
|
||||||
setText(getBatteryText("$text $battery"))
|
setText("$text $battery")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,17 +78,16 @@ class BatteryView @JvmOverloads constructor(
|
|||||||
if (AppConfig.optimizeRender) {
|
if (AppConfig.optimizeRender) {
|
||||||
canvasRecorder.recordIfNeededThenDraw(canvas, width, height) {
|
canvasRecorder.recordIfNeededThenDraw(canvas, width, height) {
|
||||||
super.onDraw(this)
|
super.onDraw(this)
|
||||||
if (!isBattery) return@recordIfNeededThenDraw
|
|
||||||
drawBattery(this)
|
drawBattery(this)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
super.onDraw(canvas)
|
super.onDraw(canvas)
|
||||||
if (!isBattery) return
|
|
||||||
drawBattery(canvas)
|
drawBattery(canvas)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun drawBattery(canvas: Canvas) {
|
private fun drawBattery(canvas: Canvas) {
|
||||||
|
if (!isBattery) return
|
||||||
layout.getLineBounds(0, outFrame)
|
layout.getLineBounds(0, outFrame)
|
||||||
val batteryStart = layout
|
val batteryStart = layout
|
||||||
.getPrimaryHorizontal(text.length - battery.toString().length)
|
.getPrimaryHorizontal(text.length - battery.toString().length)
|
||||||
@ -120,22 +113,10 @@ class BatteryView @JvmOverloads constructor(
|
|||||||
canvas.drawRect(polar, batteryPaint)
|
canvas.drawRect(polar, batteryPaint)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getBatteryText(text: CharSequence?): SpannableStringBuilder? {
|
@Suppress("UNNECESSARY_SAFE_CALL")
|
||||||
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 {
|
canvasRecorder?.invalidate()
|
||||||
canvasRecorder.invalidate()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user