mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -10,20 +10,15 @@ import android.graphics.Paint
|
||||
import android.graphics.Rect
|
||||
import android.util.AttributeSet
|
||||
import android.view.View
|
||||
import android.view.WindowInsets
|
||||
import androidx.annotation.AttrRes
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.content.res.use
|
||||
import androidx.core.graphics.ColorUtils
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import io.legado.app.R
|
||||
import splitties.dimensions.dp
|
||||
import java.text.DecimalFormat
|
||||
import java.text.NumberFormat
|
||||
import io.legado.app.utils.dpToPx
|
||||
import io.legado.app.utils.setOnApplyWindowInsetsListenerCompat
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
import kotlin.math.min
|
||||
import com.google.android.material.R as materialR
|
||||
|
||||
@@ -74,13 +69,23 @@ class ReaderInfoBarView @JvmOverloads constructor(
|
||||
get() = width - paddingLeft - paddingRight - insetLeft - insetRight
|
||||
|
||||
init {
|
||||
val insetStart = dp(10f).toInt()
|
||||
val insetEnd = dp(10f).toInt()
|
||||
paint.strokeWidth = dp(2f)
|
||||
val insetStart = 10.dpToPx()
|
||||
val insetEnd = 10.dpToPx()
|
||||
paint.strokeWidth = 2f.dpToPx()
|
||||
paint.setShadowLayer(2f, 1f, 1f, Color.GRAY)
|
||||
insetLeft = insetStart
|
||||
insetRight = insetEnd
|
||||
insetTop = minOf(insetLeft, insetRight)
|
||||
setOnApplyWindowInsetsListenerCompat { _, windowInsets ->
|
||||
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
|
||||
if (insets.left >= paddingLeft) {
|
||||
cutoutInsetLeft = insets.left
|
||||
}
|
||||
if (insets.right >= paddingRight) {
|
||||
cutoutInsetRight = insets.right
|
||||
}
|
||||
windowInsets
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDraw(canvas: Canvas) {
|
||||
@@ -119,15 +124,9 @@ class ReaderInfoBarView @JvmOverloads constructor(
|
||||
|
||||
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
|
||||
super.onSizeChanged(w, h, oldw, oldh)
|
||||
updateCutoutInsets(ViewCompat.getRootWindowInsets(this))
|
||||
updateTextSize()
|
||||
}
|
||||
|
||||
override fun onApplyWindowInsets(insets: WindowInsets): WindowInsets {
|
||||
updateCutoutInsets(WindowInsetsCompat.toWindowInsetsCompat(insets))
|
||||
return super.onApplyWindowInsets(insets)
|
||||
}
|
||||
|
||||
override fun onAttachedToWindow() {
|
||||
super.onAttachedToWindow()
|
||||
ContextCompat.registerReceiver(
|
||||
@@ -136,7 +135,6 @@ class ReaderInfoBarView @JvmOverloads constructor(
|
||||
IntentFilter(Intent.ACTION_TIME_TICK),
|
||||
ContextCompat.RECEIVER_EXPORTED,
|
||||
)
|
||||
updateCutoutInsets(ViewCompat.getRootWindowInsets(this))
|
||||
}
|
||||
|
||||
override fun onDetachedFromWindow() {
|
||||
@@ -150,33 +148,6 @@ class ReaderInfoBarView @JvmOverloads constructor(
|
||||
invalidate()
|
||||
}
|
||||
|
||||
private fun Number.format(
|
||||
decimals: Int = 0,
|
||||
decPoint: Char = '.',
|
||||
thousandsSep: Char? = ' '
|
||||
): String {
|
||||
val formatter = NumberFormat.getInstance(Locale.US) as DecimalFormat
|
||||
val symbols = formatter.decimalFormatSymbols
|
||||
if (thousandsSep != null) {
|
||||
symbols.groupingSeparator = thousandsSep
|
||||
formatter.isGroupingUsed = true
|
||||
} else {
|
||||
formatter.isGroupingUsed = false
|
||||
}
|
||||
symbols.decimalSeparator = decPoint
|
||||
formatter.decimalFormatSymbols = symbols
|
||||
formatter.minimumFractionDigits = decimals
|
||||
formatter.maximumFractionDigits = decimals
|
||||
return when (this) {
|
||||
is Float,
|
||||
is Double,
|
||||
-> formatter.format(this.toDouble())
|
||||
|
||||
else -> formatter.format(this.toLong())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun updateTextSize() {
|
||||
val testTextSize = 48f
|
||||
paint.textSize = testTextSize
|
||||
@@ -213,20 +184,6 @@ class ReaderInfoBarView @JvmOverloads constructor(
|
||||
drawText(text, x, y, paint)
|
||||
}
|
||||
|
||||
private fun updateCutoutInsets(insetsCompat: WindowInsetsCompat?) {
|
||||
val cutouts = (insetsCompat ?: return).displayCutout?.boundingRects.orEmpty()
|
||||
cutoutInsetLeft = 0
|
||||
cutoutInsetRight = 0
|
||||
for (rect in cutouts) {
|
||||
if (rect.left <= paddingLeft) {
|
||||
cutoutInsetLeft += rect.width()
|
||||
}
|
||||
if (rect.right >= width - paddingRight) {
|
||||
cutoutInsetRight += rect.width()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private inner class TimeReceiver : BroadcastReceiver() {
|
||||
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
|
||||
Reference in New Issue
Block a user