This commit is contained in:
gedoor 2021-10-22 10:12:29 +08:00
parent e711093efe
commit b264ff2f05
11 changed files with 50 additions and 3 deletions

View File

@ -6,6 +6,7 @@ import android.content.res.Resources
import android.os.Build
import android.os.LocaleList
import io.legado.app.constant.PreferKey
import io.legado.app.utils.getPrefInt
import io.legado.app.utils.getPrefString
import java.util.*
@ -13,13 +14,17 @@ import java.util.*
object AppContextWrapper {
fun wrap(context: Context): Context {
var fontScale = context.getPrefInt(PreferKey.fontScale) / 10f
if (fontScale !in 1f..2f) {
fontScale = Configuration().fontScale
}
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val resources: Resources = context.resources
val configuration: Configuration = resources.configuration
val targetLocale = getSetLocale(context)
configuration.setLocale(targetLocale)
configuration.setLocales(LocaleList(targetLocale))
configuration.fontScale = 1f
configuration.fontScale = fontScale
context.createConfigurationContext(configuration)
} else {
val resources: Resources = context.resources
@ -27,7 +32,7 @@ object AppContextWrapper {
val configuration: Configuration = resources.configuration
@Suppress("DEPRECATION")
configuration.locale = targetLocale
configuration.fontScale = 1f
configuration.fontScale = fontScale
@Suppress("DEPRECATION")
resources.updateConfiguration(configuration, resources.displayMetrics)
context

View File

@ -2,6 +2,7 @@ package io.legado.app.constant
object PreferKey {
const val language = "language"
const val fontScale = "fontScale"
const val themeMode = "themeMode"
const val userAgent = "userAgent"
const val showUnread = "showUnread"

View File

@ -56,6 +56,7 @@ class ThemeConfigFragment : BasePreferenceFragment(),
upPreferenceSummary(PreferKey.bgImage, getPrefString(PreferKey.bgImage))
upPreferenceSummary(PreferKey.bgImageN, getPrefString(PreferKey.bgImageN))
upPreferenceSummary(PreferKey.barElevation, AppConfig.elevation.toString())
upPreferenceSummary(PreferKey.fontScale)
findPreference<ColorPreference>(PreferKey.cBackground)?.let {
it.onSaveColor = { color ->
if (!ColorUtils.isColorLight(color)) {
@ -152,6 +153,18 @@ class ThemeConfigFragment : BasePreferenceFragment(),
AppConfig.elevation = it
recreateActivities()
}
PreferKey.fontScale -> NumberPickerDialog(requireContext())
.setTitle(getString(R.string.font_scale))
.setMaxValue(20)
.setMinValue(10)
.setCustomButton((R.string.btn_default_s)) {
putPrefInt(PreferKey.fontScale, 0)
recreateActivities()
}
.show {
putPrefInt(PreferKey.fontScale, it)
recreateActivities()
}
PreferKey.bgImage -> selectBgAction(false)
PreferKey.bgImageN -> selectBgAction(true)
"themeList" -> ThemeListDialog().show(childFragmentManager, "themeList")
@ -257,11 +270,18 @@ class ThemeConfigFragment : BasePreferenceFragment(),
postEvent(EventBus.RECREATE, "")
}
private fun upPreferenceSummary(preferenceKey: String, value: String?) {
private fun upPreferenceSummary(preferenceKey: String, value: String? = null) {
val preference = findPreference<Preference>(preferenceKey) ?: return
when (preferenceKey) {
PreferKey.barElevation -> preference.summary =
getString(R.string.bar_elevation_s, value)
PreferKey.fontScale -> {
var fontScale = getPrefInt(PreferKey.fontScale) / 10f
if (fontScale !in 1f..2f) {
fontScale = resources.configuration.fontScale
}
preference.summary = getString(R.string.font_scale_summary, fontScale)
}
PreferKey.bgImage,
PreferKey.bgImageN -> preference.summary = if (value.isNullOrBlank()) {
getString(R.string.select_image)

View File

@ -900,5 +900,7 @@
<string name="del_login_header">删除登录头</string>
<string name="show_login_header">查看登录头</string>
<string name="login_header">登录头</string>
<string name="font_scale">字体大小</string>
<string name="font_scale_summary">当前字体大小:%.1f</string>
</resources>

View File

@ -904,5 +904,7 @@
<string name="del_login_header">删除登录头</string>
<string name="show_login_header">查看登录头</string>
<string name="login_header">登录头</string>
<string name="font_scale">字体大小</string>
<string name="font_scale_summary">当前字体大小:%.1f</string>
</resources>

View File

@ -904,5 +904,7 @@
<string name="del_login_header">删除登录头</string>
<string name="show_login_header">查看登录头</string>
<string name="login_header">登录头</string>
<string name="font_scale">字体大小</string>
<string name="font_scale_summary">当前字体大小:%.1f</string>
</resources>

View File

@ -901,5 +901,7 @@
<string name="del_login_header">删除登录头</string>
<string name="show_login_header">查看登录头</string>
<string name="login_header">登录头</string>
<string name="font_scale">字体大小</string>
<string name="font_scale_summary">当前字体大小:%.1f</string>
</resources>

View File

@ -903,5 +903,7 @@
<string name="del_login_header">删除登录头</string>
<string name="show_login_header">查看登录头</string>
<string name="login_header">登录头</string>
<string name="font_scale">字体大小</string>
<string name="font_scale_summary">当前字体大小:%.1f</string>
</resources>

View File

@ -903,5 +903,7 @@
<string name="del_login_header">删除登录头</string>
<string name="show_login_header">查看登录头</string>
<string name="login_header">登录头</string>
<string name="font_scale">字体大小</string>
<string name="font_scale_summary">当前字体大小:%.1f</string>
</resources>

View File

@ -904,5 +904,7 @@
<string name="del_login_header">删除登录头</string>
<string name="show_login_header">查看登录头</string>
<string name="login_header">登录头</string>
<string name="font_scale">字体大小</string>
<string name="font_scale_summary">当前字体大小:%.1f</string>
</resources>

View File

@ -33,6 +33,13 @@
android:title="@string/bar_elevation"
app:iconSpaceReserved="false" />
<io.legado.app.ui.widget.prefs.Preference
android:defaultValue="true"
android:key="fontScale"
android:summary="@string/font_scale_summary"
android:title="@string/font_scale"
app:iconSpaceReserved="false" />
<io.legado.app.ui.widget.prefs.Preference
android:defaultValue="false"
android:key="coverConfig"