From 45c953024fd39060c73f34a308055b9ee7be0155 Mon Sep 17 00:00:00 2001 From: HapeLee <1321903405@qq.com> Date: Sun, 19 Oct 2025 00:40:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=80=89=E6=8B=A9=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E5=88=9B=E5=BB=BA=E4=B8=BB=E9=A2=98=E9=A2=9C=E8=89=B2?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legato/kazusa/App.kt | 34 +++++- .../io/legato/kazusa/base/BaseActivity.kt | 35 +++++- .../io/legato/kazusa/constant/PreferKey.kt | 2 + .../lib/prefs/ImagePreviewPreference.kt | 53 +++++++++ .../kazusa/ui/config/ThemeConfigFragment.kt | 106 +++++++++++++----- app/src/main/res/layout/view_icon.xml | 14 ++- app/src/main/res/values-zh/strings.xml | 5 +- app/src/main/res/values/strings.xml | 3 + app/src/main/res/xml/pref_config_theme.xml | 9 +- 9 files changed, 218 insertions(+), 43 deletions(-) create mode 100644 app/src/main/java/io/legato/kazusa/lib/prefs/ImagePreviewPreference.kt diff --git a/app/src/main/java/io/legato/kazusa/App.kt b/app/src/main/java/io/legato/kazusa/App.kt index 46b71c6ef..b516c817b 100644 --- a/app/src/main/java/io/legato/kazusa/App.kt +++ b/app/src/main/java/io/legato/kazusa/App.kt @@ -7,7 +7,9 @@ import android.app.NotificationManager import android.content.Context import android.content.pm.ApplicationInfo import android.content.res.Configuration +import android.graphics.BitmapFactory import android.os.Build +import androidx.core.graphics.scale import com.github.liuyueyi.quick.transfer.constants.TransType import com.google.android.material.color.DynamicColors import com.google.android.material.color.DynamicColorsOptions @@ -59,6 +61,7 @@ import kotlinx.coroutines.launch import org.chromium.base.ThreadUtils import splitties.init.appCtx import splitties.systemservices.notificationManager +import java.io.File import java.net.URL import java.util.concurrent.TimeUnit import java.util.logging.Level @@ -72,9 +75,34 @@ class App : Application() { if (getPrefString("app_theme", "0") == "11") { if (AppConfig.customMode == "accent") setTheme(R.style.ThemeOverlay_WhiteBackground) - DynamicColors.applyToActivitiesIfAvailable(this, DynamicColorsOptions.Builder() - .setContentBasedSource(this.primaryColor) - .build()) + + val colorImagePath = getPrefString(PreferKey.colorImage) + if (!colorImagePath.isNullOrBlank()) { + val file = File(colorImagePath) + if (file.exists()) { + val bitmap = BitmapFactory.decodeFile(file.absolutePath) + if (bitmap != null) { + val colorAccuracy = true + val targetWidth = if (colorAccuracy) (bitmap.width / 4).coerceAtMost(256) else 16 + val targetHeight = if (colorAccuracy) (bitmap.height / 4).coerceAtMost(256) else 16 + val scaledBitmap = bitmap.scale(targetWidth, targetHeight, false) + + val options = DynamicColorsOptions.Builder() + .setContentBasedSource(scaledBitmap) + .build() + + DynamicColors.applyToActivitiesIfAvailable(this, options) + bitmap.recycle() + } + } + }else{ + DynamicColors.applyToActivitiesIfAvailable( + this, + DynamicColorsOptions.Builder() + .setContentBasedSource(this.primaryColor) + .build() + ) + } } super.onCreate() diff --git a/app/src/main/java/io/legato/kazusa/base/BaseActivity.kt b/app/src/main/java/io/legato/kazusa/base/BaseActivity.kt index d58727926..6ad080f9b 100644 --- a/app/src/main/java/io/legato/kazusa/base/BaseActivity.kt +++ b/app/src/main/java/io/legato/kazusa/base/BaseActivity.kt @@ -3,6 +3,7 @@ package io.legato.kazusa.base import android.annotation.SuppressLint import android.content.Context import android.content.res.Configuration +import android.graphics.BitmapFactory import android.graphics.Color import android.os.Build import android.os.Bundle @@ -14,6 +15,7 @@ import android.view.View import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity import androidx.core.graphics.drawable.toDrawable +import androidx.core.graphics.scale import androidx.core.view.WindowCompat import androidx.viewbinding.ViewBinding import com.google.android.material.appbar.AppBarLayout @@ -22,6 +24,7 @@ import com.google.android.material.color.DynamicColorsOptions import io.legato.kazusa.R import io.legato.kazusa.constant.AppLog import io.legato.kazusa.constant.EventBus +import io.legato.kazusa.constant.PreferKey import io.legato.kazusa.constant.Theme import io.legato.kazusa.help.config.AppConfig import io.legato.kazusa.help.config.ThemeConfig @@ -38,6 +41,7 @@ import io.legato.kazusa.utils.fullScreen import io.legato.kazusa.utils.setNavigationBarColorAuto import io.legato.kazusa.utils.setStatusBarColorAuto import io.legato.kazusa.utils.themeColor +import java.io.File abstract class BaseActivity( @@ -169,11 +173,36 @@ abstract class BaseActivity( "11" -> { if (AppConfig.customMode == "accent") setTheme(R.style.ThemeOverlay_WhiteBackground) - DynamicColors.applyToActivitiesIfAvailable(application, DynamicColorsOptions.Builder() - .setContentBasedSource(application.primaryColor) - .build()) + val colorImagePath = getPrefString(PreferKey.colorImage) + if (!colorImagePath.isNullOrBlank()) { + val file = File(colorImagePath) + if (file.exists()) { + val bitmap = BitmapFactory.decodeFile(file.absolutePath) + if (bitmap != null) { + val colorAccuracy = true + val targetWidth = if (colorAccuracy) (bitmap.width / 4).coerceAtMost(256) else 16 + val targetHeight = if (colorAccuracy) (bitmap.height / 4).coerceAtMost(256) else 16 + val scaledBitmap = bitmap.scale(targetWidth, targetHeight, false) + + val options = DynamicColorsOptions.Builder() + .setContentBasedSource(scaledBitmap) + .build() + + DynamicColors.applyToActivitiesIfAvailable(application, options) + bitmap.recycle() + } + } + }else{ + DynamicColors.applyToActivitiesIfAvailable( + application, + DynamicColorsOptions.Builder() + .setContentBasedSource(application.primaryColor) + .build() + ) + } } + "12" -> setTheme(R.style.AppTheme_Transparent) } diff --git a/app/src/main/java/io/legato/kazusa/constant/PreferKey.kt b/app/src/main/java/io/legato/kazusa/constant/PreferKey.kt index a5d2749ab..b2b5c02b2 100644 --- a/app/src/main/java/io/legato/kazusa/constant/PreferKey.kt +++ b/app/src/main/java/io/legato/kazusa/constant/PreferKey.kt @@ -205,4 +205,6 @@ object PreferKey { const val pureBlack = "pure_black" const val labelVisibilityMode = "labelVisibilityMode" const val menuAlpha = "menuAlpha" + + const val colorImage = "colorImage" } diff --git a/app/src/main/java/io/legato/kazusa/lib/prefs/ImagePreviewPreference.kt b/app/src/main/java/io/legato/kazusa/lib/prefs/ImagePreviewPreference.kt new file mode 100644 index 000000000..d6ab942e6 --- /dev/null +++ b/app/src/main/java/io/legato/kazusa/lib/prefs/ImagePreviewPreference.kt @@ -0,0 +1,53 @@ +package io.legato.kazusa.lib.prefs + +import android.content.Context +import android.graphics.BitmapFactory +import android.net.Uri +import android.util.AttributeSet +import android.view.View +import android.widget.ImageView +import androidx.preference.PreferenceViewHolder +import io.legato.kazusa.R +import java.io.File + +class ImagePreviewPreference(context: Context, attrs: AttributeSet) : Preference(context, attrs) { + + private var preview: ImageView? = null + + init { + layoutResource = R.layout.view_preference + widgetLayoutResource = R.layout.view_icon + } + + override fun onBindViewHolder(holder: PreferenceViewHolder) { + super.onBindViewHolder(holder) + val v = Preference.bindView( + context, holder, icon, title, summary, + widgetLayoutResource, + R.id.preview, + 50, + 50 + ) + + preview = v + updatePreview() + } + + fun updatePreview() { + val path = sharedPreferences?.getString(key, null) + val view = preview ?: return + + if (!path.isNullOrBlank()) { + val file = File(path) + if (file.exists()) { + val bitmap = BitmapFactory.decodeFile(path) + view.setImageBitmap(bitmap) + view.visibility = View.VISIBLE + return + } + } + + view.setImageDrawable(null) + view.visibility = View.GONE + } +} diff --git a/app/src/main/java/io/legato/kazusa/ui/config/ThemeConfigFragment.kt b/app/src/main/java/io/legato/kazusa/ui/config/ThemeConfigFragment.kt index 6c558864e..9d32fb92e 100644 --- a/app/src/main/java/io/legato/kazusa/ui/config/ThemeConfigFragment.kt +++ b/app/src/main/java/io/legato/kazusa/ui/config/ThemeConfigFragment.kt @@ -58,10 +58,12 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener //MenuProvider { + private val requestCodeBgLight = 121 + private val requestCodeBgDark = 122 - private val requestCodeBgLight = 121 - private val requestCodeBgDark = 122 - private val selectImage = registerForActivityResult(SelectImageContract()) { + private val requestCodeColorImage = 123 + + private val selectImage = registerForActivityResult(SelectImageContract()) { it.uri?.let { uri -> when (it.requestCode) { requestCodeBgLight -> setBgFromUri(uri, PreferKey.bgImage) { @@ -71,6 +73,10 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), requestCodeBgDark -> setBgFromUri(uri, PreferKey.bgImageN) { upTheme(true) } + + requestCodeColorImage -> setBgFromUri(uri, PreferKey.colorImage) { + upPreferenceSummary(PreferKey.colorImage, getPrefString(PreferKey.colorImage)) + } } } } @@ -78,8 +84,6 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { addPreferencesFromResource(R.xml.pref_config_theme) - upPreferenceSummary(PreferKey.bgImage, getPrefString(PreferKey.bgImage)) - upPreferenceSummary(PreferKey.bgImageN, getPrefString(PreferKey.bgImageN)) upPreferenceSummary(PreferKey.fontScale) findPreference(PreferKey.cBackground)?.let { @@ -129,6 +133,18 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), customMode?.isVisible = newValue == "11" true } + + val hasColorImage = !getPrefString(PreferKey.colorImage).isNullOrBlank() + colorPrimary?.isEnabled = !hasColorImage + if (hasColorImage) + { + upPreferenceSummary("colorPrimary", getString(R.string.seed_photo_alart)) + upPreferenceSummary(PreferKey.colorImage, getString(R.string.click_to_delete)) + } + if (!getPrefString(PreferKey.bgImage).isNullOrBlank()) + upPreferenceSummary(PreferKey.bgImage, getString(R.string.click_to_delete)) + if (!getPrefString(PreferKey.bgImageN).isNullOrBlank()) + upPreferenceSummary(PreferKey.bgImageN, getString(R.string.click_to_delete)) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { @@ -200,6 +216,11 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), //recreateActivities() } + PreferKey.colorImage -> + Handler(Looper.getMainLooper()).postDelayed({ + requireContext().restart() + }, 100) + PreferKey.bgImage, PreferKey.bgImageN -> { upPreferenceSummary(key, getPrefString(key)) @@ -232,6 +253,8 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), PreferKey.bgImage -> selectBgAction(false) PreferKey.bgImageN -> selectBgAction(true) + "colorImage" -> selectBgAction(null) + "themeList" -> ThemeListDialog().show(childFragmentManager, "themeList") "saveDayTheme", "saveNightTheme" -> alertSaveTheme(key) @@ -267,39 +290,60 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), } } - private fun selectBgAction(isNight: Boolean) { - val bgKey = if (isNight) PreferKey.bgImageN else PreferKey.bgImage - val blurringKey = if (isNight) PreferKey.bgImageNBlurring else PreferKey.bgImageBlurring - val actions = arrayListOf( - getString(R.string.background_image_blurring), - getString(R.string.select_image) - ) - if (!getPrefString(bgKey).isNullOrEmpty()) { - actions.add(getString(R.string.delete)) - } - context?.selector(items = actions) { _, i -> - when (i) { - 0 -> alertImageBlurring(blurringKey) { - upTheme(isNight) - } + private fun selectBgAction(isNight: Boolean?) { + val bgKey = when (isNight) { + true -> PreferKey.bgImageN + false -> PreferKey.bgImage + null -> PreferKey.colorImage + } + val blurringKey = when (isNight) { + true -> PreferKey.bgImageNBlurring + false -> PreferKey.bgImageBlurring + else -> null + } - 1 -> { - if (isNight) { - selectImage.launch(requestCodeBgDark) - } else { - selectImage.launch(requestCodeBgLight) + val actions = mutableListOf() + + if (isNight != null) { + actions.add(getString(R.string.background_image_blurring)) + } + + actions.add(getString(R.string.select_image)) + + if (!getPrefString(bgKey).isNullOrEmpty()) { + actions.add(getString(R.string.delete)) + } + + context?.selector(items = actions) { _, i -> + when { + isNight != null && i == 0 -> { + alertImageBlurring(blurringKey!!) { + upTheme(isNight) + } } - } - 2 -> { - removePref(bgKey) - upTheme(isNight) + (isNight == null && i == 0) || (isNight != null && i == 1) -> { + when (isNight) { + true -> selectImage.launch(requestCodeBgDark) + false -> selectImage.launch(requestCodeBgLight) + null -> selectImage.launch(requestCodeColorImage) + } + } + + (isNight == null && i == 1) || (isNight != null && i == 2) -> { + removePref(bgKey) + if (isNight != null) { + upTheme(isNight) + } else { + upPreferenceSummary(PreferKey.colorImage, getPrefString(PreferKey.colorImage)) + } + } } } } - } - private fun alertImageBlurring(preferKey: String, success: () -> Unit) { + + private fun alertImageBlurring(preferKey: String, success: () -> Unit) { alert(R.string.background_image_blurring) { val alertBinding = DialogImageBlurringBinding.inflate(layoutInflater).apply { getPrefInt(preferKey, 0).let { diff --git a/app/src/main/res/layout/view_icon.xml b/app/src/main/res/layout/view_icon.xml index 989476275..dbff9b579 100644 --- a/app/src/main/res/layout/view_icon.xml +++ b/app/src/main/res/layout/view_icon.xml @@ -1,5 +1,13 @@ - \ No newline at end of file + android:layout_height="48dp" + xmlns:app="http://schemas.android.com/apk/res-auto" + app:cardBackgroundColor="@android:color/transparent" + app:strokeWidth="0dp"> + + \ No newline at end of file diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index d06e32550..86986d4df 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -1270,7 +1270,7 @@ 深色 纯黑深色模式 透明主题请于设置背景图片后设置 - 使用种子颜色创建主题 + 使用种子颜色生成主题颜色 种子色 自定义模式 仍可滑动切换页面 @@ -1293,4 +1293,7 @@ 新增书籍到书架 快速添加到书架 书籍已添加到书架 + 使用图片生成主题颜色 + 若需使用种子颜色,请取消种子图片选择 + 点击以删除或更改 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7f0552b67..0179950cb 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1294,4 +1294,7 @@ 新增书籍到书架 快速添加到书架 书籍已添加到书架 + 使用图片生成主题颜色 + 若需使用种子颜色,请取消种子图片选择 + Click to delete or change diff --git a/app/src/main/res/xml/pref_config_theme.xml b/app/src/main/res/xml/pref_config_theme.xml index 416574da0..02e6c54e8 100644 --- a/app/src/main/res/xml/pref_config_theme.xml +++ b/app/src/main/res/xml/pref_config_theme.xml @@ -37,6 +37,11 @@ app:cpv_dialogType="preset" app:iconSpaceReserved="false" /> + + - -