Compare commits

..

3 Commits

Author SHA1 Message Date
kunfei
796d3cdfce
Merge b6b6718177 into 5336adc035 2024-05-15 20:06:39 +08:00
dependabot[bot]
5336adc035
Bump pnpm/action-setup from 3 to 4 (#3930)
Bumps [pnpm/action-setup](https://github.com/pnpm/action-setup) from 3 to 4.
- [Release notes](https://github.com/pnpm/action-setup/releases)
- [Commits](https://github.com/pnpm/action-setup/compare/v3...v4)

---
updated-dependencies:
- dependency-name: pnpm/action-setup
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-05-15 20:06:20 +08:00
Horis
72ad27ea84 优化 2024-05-14 09:33:56 +08:00
2 changed files with 10 additions and 29 deletions

View File

@ -27,7 +27,7 @@ jobs:
with: with:
node-version: 16 node-version: 16
- uses: pnpm/action-setup@v3 - uses: pnpm/action-setup@v4
name: Install pnpm name: Install pnpm
id: pnpm-install id: pnpm-install
with: with:

View File

@ -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()
}
} }
} }