优化
This commit is contained in:
@@ -10,20 +10,15 @@ import android.graphics.Paint
|
|||||||
import android.graphics.Rect
|
import android.graphics.Rect
|
||||||
import android.util.AttributeSet
|
import android.util.AttributeSet
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.WindowInsets
|
|
||||||
import androidx.annotation.AttrRes
|
import androidx.annotation.AttrRes
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.core.content.res.use
|
import androidx.core.content.res.use
|
||||||
import androidx.core.graphics.ColorUtils
|
import androidx.core.graphics.ColorUtils
|
||||||
import androidx.core.view.ViewCompat
|
|
||||||
import androidx.core.view.WindowInsetsCompat
|
import androidx.core.view.WindowInsetsCompat
|
||||||
import io.legado.app.R
|
import io.legado.app.utils.dpToPx
|
||||||
import splitties.dimensions.dp
|
import io.legado.app.utils.setOnApplyWindowInsetsListenerCompat
|
||||||
import java.text.DecimalFormat
|
|
||||||
import java.text.NumberFormat
|
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
import java.util.Locale
|
|
||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
import com.google.android.material.R as materialR
|
import com.google.android.material.R as materialR
|
||||||
|
|
||||||
@@ -74,13 +69,23 @@ class ReaderInfoBarView @JvmOverloads constructor(
|
|||||||
get() = width - paddingLeft - paddingRight - insetLeft - insetRight
|
get() = width - paddingLeft - paddingRight - insetLeft - insetRight
|
||||||
|
|
||||||
init {
|
init {
|
||||||
val insetStart = dp(10f).toInt()
|
val insetStart = 10.dpToPx()
|
||||||
val insetEnd = dp(10f).toInt()
|
val insetEnd = 10.dpToPx()
|
||||||
paint.strokeWidth = dp(2f)
|
paint.strokeWidth = 2f.dpToPx()
|
||||||
paint.setShadowLayer(2f, 1f, 1f, Color.GRAY)
|
paint.setShadowLayer(2f, 1f, 1f, Color.GRAY)
|
||||||
insetLeft = insetStart
|
insetLeft = insetStart
|
||||||
insetRight = insetEnd
|
insetRight = insetEnd
|
||||||
insetTop = minOf(insetLeft, insetRight)
|
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) {
|
override fun onDraw(canvas: Canvas) {
|
||||||
@@ -119,15 +124,9 @@ class ReaderInfoBarView @JvmOverloads constructor(
|
|||||||
|
|
||||||
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
|
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
|
||||||
super.onSizeChanged(w, h, oldw, oldh)
|
super.onSizeChanged(w, h, oldw, oldh)
|
||||||
updateCutoutInsets(ViewCompat.getRootWindowInsets(this))
|
|
||||||
updateTextSize()
|
updateTextSize()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onApplyWindowInsets(insets: WindowInsets): WindowInsets {
|
|
||||||
updateCutoutInsets(WindowInsetsCompat.toWindowInsetsCompat(insets))
|
|
||||||
return super.onApplyWindowInsets(insets)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onAttachedToWindow() {
|
override fun onAttachedToWindow() {
|
||||||
super.onAttachedToWindow()
|
super.onAttachedToWindow()
|
||||||
ContextCompat.registerReceiver(
|
ContextCompat.registerReceiver(
|
||||||
@@ -136,7 +135,6 @@ class ReaderInfoBarView @JvmOverloads constructor(
|
|||||||
IntentFilter(Intent.ACTION_TIME_TICK),
|
IntentFilter(Intent.ACTION_TIME_TICK),
|
||||||
ContextCompat.RECEIVER_EXPORTED,
|
ContextCompat.RECEIVER_EXPORTED,
|
||||||
)
|
)
|
||||||
updateCutoutInsets(ViewCompat.getRootWindowInsets(this))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDetachedFromWindow() {
|
override fun onDetachedFromWindow() {
|
||||||
@@ -150,33 +148,6 @@ class ReaderInfoBarView @JvmOverloads constructor(
|
|||||||
invalidate()
|
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() {
|
private fun updateTextSize() {
|
||||||
val testTextSize = 48f
|
val testTextSize = 48f
|
||||||
paint.textSize = testTextSize
|
paint.textSize = testTextSize
|
||||||
@@ -213,20 +184,6 @@ class ReaderInfoBarView @JvmOverloads constructor(
|
|||||||
drawText(text, x, y, paint)
|
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() {
|
private inner class TimeReceiver : BroadcastReceiver() {
|
||||||
|
|
||||||
override fun onReceive(context: Context?, intent: Intent?) {
|
override fun onReceive(context: Context?, intent: Intent?) {
|
||||||
|
|||||||
Reference in New Issue
Block a user