现在当使用按钮切换字重时,会和官方逻辑相同

This commit is contained in:
HapeLee
2025-09-13 03:42:13 +08:00
parent 2de4ced6bc
commit 7b615cd186
3 changed files with 36 additions and 19 deletions
@@ -137,7 +137,7 @@ class FontSelectDialog : BaseBottomSheetDialogFragment(R.layout.dialog_font_sele
} }
val weightOptions = context?.resources?.getStringArray(R.array.text_font_weight) val weightOptions = context?.resources?.getStringArray(R.array.text_font_weight)
val weightValues = listOf(500, 700, 300) val weightValues = listOf(2, 1, 0)
val initialIndex = weightValues.indexOf(ReadBookConfig.textBold) val initialIndex = weightValues.indexOf(ReadBookConfig.textBold)
binding.textFontWeightConverter.text = weightOptions?.getOrNull(initialIndex) ?: "自定义" binding.textFontWeightConverter.text = weightOptions?.getOrNull(initialIndex) ?: "自定义"
@@ -912,19 +912,33 @@ object ChapterProvider {
} }
private fun getPaints(typeface: Typeface?): Pair<TextPaint, TextPaint> { private fun getPaints(typeface: Typeface?): Pair<TextPaint, TextPaint> {
// 字体统一处理 val bold = Typeface.create(typeface, Typeface.BOLD)
val (titleFont, textFont) = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { val normal = Typeface.create(typeface, Typeface.NORMAL)
Pair( val (titleFont, textFont) = when (ReadBookConfig.textBold) {
Typeface.create(typeface, ReadBookConfig.textBold, false), 1 -> {
Typeface.create(typeface, ReadBookConfig.textBold, false) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
) Pair(Typeface.create(typeface, 900, false), bold)
} else { else
val tf = when (ReadBookConfig.textBold) { Pair(bold, bold)
in 700..900 -> Typeface.DEFAULT_BOLD
in 300..499 -> Typeface.DEFAULT
else -> Typeface.DEFAULT
} }
Pair(tf, tf)
2 -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
Pair(normal, Typeface.create(typeface, 300, false))
else
Pair(normal, normal)
}
in 100..900 -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Pair(normal, Typeface.create(typeface, ReadBookConfig.textBold, false))
} else {
Pair(normal, normal)
}
}
else -> Pair(bold, normal)
} }
//标题 //标题
@@ -932,7 +946,8 @@ object ChapterProvider {
tPaint.color = ReadBookConfig.textColor tPaint.color = ReadBookConfig.textColor
tPaint.letterSpacing = ReadBookConfig.letterSpacing tPaint.letterSpacing = ReadBookConfig.letterSpacing
tPaint.typeface = titleFont tPaint.typeface = titleFont
tPaint.setFontVariationSettings("'wght' ${ReadBookConfig.textBold}") if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && ReadBookConfig.textBold in 100..900)
tPaint.setFontVariationSettings("'wght' ${ReadBookConfig.textBold}")
tPaint.textSize = with(ReadBookConfig) { textSize + titleSize }.toFloat().spToPx() tPaint.textSize = with(ReadBookConfig) { textSize + titleSize }.toFloat().spToPx()
tPaint.isAntiAlias = true tPaint.isAntiAlias = true
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q && AppConfig.optimizeRender) { if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q && AppConfig.optimizeRender) {
@@ -943,12 +958,14 @@ object ChapterProvider {
cPaint.color = ReadBookConfig.textColor cPaint.color = ReadBookConfig.textColor
cPaint.letterSpacing = ReadBookConfig.letterSpacing cPaint.letterSpacing = ReadBookConfig.letterSpacing
cPaint.typeface = textFont cPaint.typeface = textFont
cPaint.setFontVariationSettings("'wght' ${ReadBookConfig.textBold}") if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && ReadBookConfig.textBold in 100..900)
cPaint.setFontVariationSettings("'wght' ${ReadBookConfig.textBold}")
cPaint.textSize = ReadBookConfig.textSize.toFloat().spToPx() cPaint.textSize = ReadBookConfig.textSize.toFloat().spToPx()
cPaint.isAntiAlias = true cPaint.isAntiAlias = true
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q && AppConfig.optimizeRender) { if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q && AppConfig.optimizeRender) {
cPaint.isLinearText = true cPaint.isLinearText = true
} }
return Pair(tPaint, cPaint) return Pair(tPaint, cPaint)
} }
@@ -30,8 +30,8 @@
style="@style/Widget.Material3Expressive.Button.TonalButton" style="@style/Widget.Material3Expressive.Button.TonalButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="8dp" android:layout_marginStart="12dp"
android:minWidth="0dp" android:minWidth="80dp"
android:textSize="12sp" /> android:textSize="12sp" />
<com.google.android.material.slider.Slider <com.google.android.material.slider.Slider
@@ -188,9 +188,9 @@
android:valueFrom="0" android:valueFrom="0"
android:valueTo="20" android:valueTo="20"
app:isBottomBackground="true" app:isBottomBackground="true"
app:thumbHeight="28dp" app:thumbHeight="24dp"
app:title="@string/paragraph_size" app:title="@string/paragraph_size"
app:trackHeight="28dp" /> app:trackHeight="24dp" />
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view" android:id="@+id/recycler_view"