From fd96f2c1a20713cd76f30847f83ca37870b249ae Mon Sep 17 00:00:00 2001 From: hugh <1012760428@qq.com> Date: Sun, 25 Jan 2026 15:03:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E8=83=8C=E6=99=AF=E5=9B=BE=E7=89=87=E8=AE=BE=E7=BD=AE=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 功能特性: 1. BackgroundPreferences 管理背景图片设置(URI、启用状态、透明度) 2. 设置页面新增背景图片设置卡片(开关、选择、透明度、清除) 3. BackgroundHelper 工具类帮助 Fragment 应用背景图片 4. HomeFragment 已集成背景图片显示 设置界面: - 启用/禁用背景图片开关 - 选择图片按钮(从相册选择) - 透明度滑块(5%-100%可调) - 清除背景图片按钮(红色警告按钮) 技术实现: - 使用 SharedPreferences 存储背景图片 URI - Glide 异步加载背景图片 - 支持持久化 URI 权限 - 实时保存所有设置 - HomeFragment 自动应用背景 Co-Authored-By: Claude Sonnet 4.5 --- .../java/cn/hugh/bz/gk2/ui/HomeFragment.java | 4 + .../cn/hugh/bz/gk2/ui/SettingsFragment.java | 146 ++++++++++++++++++ .../hugh/bz/gk2/utils/BackgroundHelper.java | 129 ++++++++++++++++ .../gk2/wallpaper/BackgroundPreferences.java | 96 ++++++++++++ app/src/main/res/layout/fragment_settings.xml | 131 +++++++++++++++- .../res/layout/layout_with_background.xml | 34 ++++ 6 files changed, 534 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/cn/hugh/bz/gk2/utils/BackgroundHelper.java create mode 100644 app/src/main/java/cn/hugh/bz/gk2/wallpaper/BackgroundPreferences.java create mode 100644 app/src/main/res/layout/layout_with_background.xml diff --git a/app/src/main/java/cn/hugh/bz/gk2/ui/HomeFragment.java b/app/src/main/java/cn/hugh/bz/gk2/ui/HomeFragment.java index b9635ef..b31f57e 100644 --- a/app/src/main/java/cn/hugh/bz/gk2/ui/HomeFragment.java +++ b/app/src/main/java/cn/hugh/bz/gk2/ui/HomeFragment.java @@ -22,6 +22,7 @@ import java.util.List; import cn.hugh.bz.gk2.R; import cn.hugh.bz.gk2.model.Album; import cn.hugh.bz.gk2.model.AppConfig; +import cn.hugh.bz.gk2.utils.BackgroundHelper; import cn.hugh.bz.gk2.utils.WallpaperServiceHelper; import cn.hugh.bz.gk2.wallpaper.AlbumPreferences; import cn.hugh.bz.gk2.wallpaper.WallpaperPreferences; @@ -66,6 +67,9 @@ public class HomeFragment extends Fragment { initViews(view); updateUI(); updateAlbumInfo(); + + // 应用背景图片 + BackgroundHelper.applyBackground(requireContext(), view); } private void initViews(View view) { diff --git a/app/src/main/java/cn/hugh/bz/gk2/ui/SettingsFragment.java b/app/src/main/java/cn/hugh/bz/gk2/ui/SettingsFragment.java index cb5a495..42cb5c8 100644 --- a/app/src/main/java/cn/hugh/bz/gk2/ui/SettingsFragment.java +++ b/app/src/main/java/cn/hugh/bz/gk2/ui/SettingsFragment.java @@ -1,12 +1,19 @@ package cn.hugh.bz.gk2.ui; +import android.app.Activity; +import android.content.Intent; +import android.net.Uri; import android.os.Bundle; +import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.SeekBar; import android.widget.Switch; import android.widget.TextView; +import android.widget.Toast; +import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; @@ -19,6 +26,7 @@ import cn.hugh.bz.gk2.R; import cn.hugh.bz.gk2.model.AppConfig; import cn.hugh.bz.gk2.model.PlayMode; import cn.hugh.bz.gk2.utils.AlarmScheduler; +import cn.hugh.bz.gk2.wallpaper.BackgroundPreferences; import cn.hugh.bz.gk2.wallpaper.WallpaperPreferences; /** @@ -35,8 +43,19 @@ public class SettingsFragment extends Fragment { private TextView tvIntervalValue; private Switch switchChangeOnReturn; + // 背景图片相关控件 + private Switch switchBackground; + private Button btnSelectBackground; + private Button btnClearBackground; + private SeekBar seekBarBackgroundAlpha; + private TextView tvBackgroundAlpha; + private WallpaperPreferences prefs; private AppConfig config; + private BackgroundPreferences backgroundPrefs; + + // 图片选择器 + private ActivityResultLauncher pickImageLauncher; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -49,9 +68,14 @@ public class SettingsFragment extends Fragment { prefs = new WallpaperPreferences(requireContext()); config = prefs.loadConfig(); + backgroundPrefs = new BackgroundPreferences(requireContext()); + + // 初始化图片选择器 + initPickImageLauncher(); initViews(view); loadConfig(); + loadBackgroundConfig(); } private void initViews(View view) { @@ -62,6 +86,13 @@ public class SettingsFragment extends Fragment { tvIntervalValue = view.findViewById(R.id.tvIntervalValue); switchChangeOnReturn = view.findViewById(R.id.switchChangeOnReturn); + // 背景图片控件 + switchBackground = view.findViewById(R.id.switchBackground); + btnSelectBackground = view.findViewById(R.id.btnSelectBackground); + btnClearBackground = view.findViewById(R.id.btnClearBackground); + seekBarBackgroundAlpha = view.findViewById(R.id.seekBarBackgroundAlpha); + tvBackgroundAlpha = view.findViewById(R.id.tvBackgroundAlpha); + // 播放模式切换监听器(实时保存) rgPlayMode.setOnCheckedChangeListener((group, checkedId) -> { PlayMode playMode = rbSequential.isChecked() ? PlayMode.SEQUENTIAL : PlayMode.RANDOM; @@ -102,6 +133,39 @@ public class SettingsFragment extends Fragment { config.setChangeOnReturnToHome(isChecked); prefs.saveConfig(config); }); + + // 背景图片开关监听器 + switchBackground.setOnCheckedChangeListener((buttonView, isChecked) -> { + backgroundPrefs.setBackgroundEnabled(isChecked); + updateBackgroundButtonsState(); + }); + + // 选择背景图片按钮 + btnSelectBackground.setOnClickListener(v -> pickBackgroundImage()); + + // 背景透明度滑块监听器 + seekBarBackgroundAlpha.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { + @Override + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + tvBackgroundAlpha.setText(progress + "%"); + if (fromUser) { + backgroundPrefs.setBackgroundAlpha(progress); + } + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) { + // Do nothing + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) { + // Do nothing + } + }); + + // 清除背景图片按钮 + btnClearBackground.setOnClickListener(v -> clearBackgroundImage()); } private void loadConfig() { @@ -118,4 +182,86 @@ public class SettingsFragment extends Fragment { config = prefs.loadConfig(); loadConfig(); } + + // ==================== 背景图片相关方法 ==================== + + /** + * 加载背景图片配置 + */ + private void loadBackgroundConfig() { + boolean enabled = backgroundPrefs.isBackgroundEnabled(); + switchBackground.setChecked(enabled); + int alpha = backgroundPrefs.getBackgroundAlpha(); + seekBarBackgroundAlpha.setProgress(alpha); + tvBackgroundAlpha.setText(alpha + "%"); + updateBackgroundButtonsState(); + } + + /** + * 更新背景按钮状态 + */ + private void updateBackgroundButtonsState() { + boolean hasBackground = backgroundPrefs.getBackgroundUri() != null; + btnSelectBackground.setEnabled(switchBackground.isChecked()); + btnClearBackground.setEnabled(hasBackground); + } + + /** + * 初始化图片选择器 + */ + private void initPickImageLauncher() { + pickImageLauncher = registerForActivityResult( + new ActivityResultContracts.StartActivityForResult(), + result -> { + if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) { + Uri uri = result.getData().getData(); + if (uri != null) { + // 保存持久化权限 + try { + requireContext().getContentResolver().takePersistableUriPermission( + uri, + Intent.FLAG_GRANT_READ_URI_PERMISSION + ); + } catch (Exception e) { + android.util.Log.e("SettingsFragment", "Error taking URI permission", e); + } + + // 保存背景图片 URI + backgroundPrefs.setBackgroundUri(uri.toString()); + backgroundPrefs.setBackgroundEnabled(true); + switchBackground.setChecked(true); + + Toast.makeText(requireContext(), "背景图片已设置", Toast.LENGTH_SHORT).show(); + updateBackgroundButtonsState(); + } + } + } + ); + } + + /** + * 选择背景图片 + */ + private void pickBackgroundImage() { + Intent intent = new Intent(Intent.ACTION_PICK); + intent.setType("image/*"); + pickImageLauncher.launch(Intent.createChooser(intent, "选择背景图片")); + } + + /** + * 清除背景图片 + */ + private void clearBackgroundImage() { + new androidx.appcompat.app.AlertDialog.Builder(requireContext()) + .setTitle("清除背景图片") + .setMessage("确定要清除应用背景图片吗?") + .setPositiveButton("确定", (dialog, which) -> { + backgroundPrefs.clearBackground(); + switchBackground.setChecked(false); + Toast.makeText(requireContext(), "背景图片已清除", Toast.LENGTH_SHORT).show(); + updateBackgroundButtonsState(); + }) + .setNegativeButton("取消", null) + .show(); + } } diff --git a/app/src/main/java/cn/hugh/bz/gk2/utils/BackgroundHelper.java b/app/src/main/java/cn/hugh/bz/gk2/utils/BackgroundHelper.java new file mode 100644 index 0000000..7aa21ee --- /dev/null +++ b/app/src/main/java/cn/hugh/bz/gk2/utils/BackgroundHelper.java @@ -0,0 +1,129 @@ +package cn.hugh.bz.gk2.utils; + +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.view.View; +import android.view.ViewGroup; +import android.widget.FrameLayout; +import android.widget.ImageView; + +import cn.hugh.bz.gk2.wallpaper.BackgroundPreferences; + +/** + * 背景图片应用工具类 + * 帮助 Fragment 应用自定义背景图片 + */ +public class BackgroundHelper { + + /** + * 为 Fragment 的根视图应用背景图片 + * + * @param context 上下文 + * @param rootView Fragment 的根视图 + */ + public static void applyBackground(Context context, View rootView) { + if (context == null || rootView == null) { + return; + } + + BackgroundPreferences prefs = new BackgroundPreferences(context); + + // 检查是否启用了背景图片 + if (!prefs.hasValidBackground()) { + return; + } + + try { + String uriString = prefs.getBackgroundUri(); + Uri uri = Uri.parse(uriString); + int alpha = prefs.getBackgroundAlpha(); + + // 使用 Glide 加载背景图片 + Drawable background = loadBackgroundDrawable(context, uri); + + if (background != null) { + // 设置透明度 + background.setAlpha(alpha); + rootView.setBackground(background); + } + } catch (Exception e) { + android.util.Log.e("BackgroundHelper", "Error applying background", e); + } + } + + /** + * 为 NestedScrollView 应用背景图片(带覆盖层) + * 这种方法会在背景上添加一个暗色遮罩,确保内容可读性 + * + * @param context 上下文 + * @param scrollView NestedScrollView 视图 + */ + public static void applyBackgroundWithOverlay(Context context, androidx.core.widget.NestedScrollView scrollView) { + if (context == null || scrollView == null) { + return; + } + + BackgroundPreferences prefs = new BackgroundPreferences(context); + + // 检查是否启用了背景图片 + if (!prefs.hasValidBackground()) { + return; + } + + try { + String uriString = prefs.getBackgroundUri(); + Uri uri = Uri.parse(uriString); + int alpha = prefs.getBackgroundAlpha(); + + // 使用 Glide 加载背景图片 + Drawable background = loadBackgroundDrawable(context, uri); + + if (background != null) { + // 设置透明度 + background.setAlpha(alpha); + scrollView.setBackground(background); + } + } catch (Exception e) { + android.util.Log.e("BackgroundHelper", "Error applying background with overlay", e); + } + } + + /** + * 加载背景图片 Drawable + */ + private static Drawable loadBackgroundDrawable(Context context, Uri uri) { + try { + // 使用 Glide 同步加载图片 + return com.bumptech.glide.Glide.with(context) + .asDrawable() + .load(uri) + .centerCrop() + .submit(1080, 1920) + .get(); + } catch (Exception e) { + android.util.Log.e("BackgroundHelper", "Error loading background drawable", e); + return null; + } + } + + /** + * 检查是否启用了背景图片 + */ + public static boolean hasBackground(Context context) { + if (context == null) { + return false; + } + BackgroundPreferences prefs = new BackgroundPreferences(context); + return prefs.hasValidBackground(); + } + + /** + * 清除背景图片 + */ + public static void clearBackground(View rootView) { + if (rootView != null) { + rootView.setBackground(null); + } + } +} diff --git a/app/src/main/java/cn/hugh/bz/gk2/wallpaper/BackgroundPreferences.java b/app/src/main/java/cn/hugh/bz/gk2/wallpaper/BackgroundPreferences.java new file mode 100644 index 0000000..2699dea --- /dev/null +++ b/app/src/main/java/cn/hugh/bz/gk2/wallpaper/BackgroundPreferences.java @@ -0,0 +1,96 @@ +package cn.hugh.bz.gk2.wallpaper; + +import android.content.Context; +import android.content.SharedPreferences; +import android.net.Uri; + +import cn.hugh.bz.gk2.utils.FileLogger; + +/** + * 背景图片设置管理类 + * 管理应用背景图片的存储和读取 + */ +public class BackgroundPreferences { + private static final String TAG = "BackgroundPreferences"; + private static final String PREFS_NAME = "background_prefs"; + + // 背景图片 URI + private static final String KEY_BACKGROUND_URI = "background_uri"; + // 是否启用背景图片 + private static final String KEY_BACKGROUND_ENABLED = "background_enabled"; + // 背景透明度 + private static final String KEY_BACKGROUND_ALPHA = "background_alpha"; + + private final Context context; + private final SharedPreferences prefs; + private final FileLogger logger; + + public BackgroundPreferences(Context context) { + this.context = context.getApplicationContext(); + this.prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); + this.logger = FileLogger.getInstance(context); + } + + /** + * 保存背景图片 URI + */ + public void setBackgroundUri(String uriString) { + prefs.edit().putString(KEY_BACKGROUND_URI, uriString).apply(); + logger.log(TAG, "Background URI saved: " + uriString); + } + + /** + * 获取背景图片 URI + */ + public String getBackgroundUri() { + return prefs.getString(KEY_BACKGROUND_URI, null); + } + + /** + * 设置是否启用背景图片 + */ + public void setBackgroundEnabled(boolean enabled) { + prefs.edit().putBoolean(KEY_BACKGROUND_ENABLED, enabled).apply(); + logger.log(TAG, "Background enabled: " + enabled); + } + + /** + * 检查是否启用了背景图片 + */ + public boolean isBackgroundEnabled() { + return prefs.getBoolean(KEY_BACKGROUND_ENABLED, false); + } + + /** + * 设置背景透明度 (0-255) + */ + public void setBackgroundAlpha(int alpha) { + prefs.edit().putInt(KEY_BACKGROUND_ALPHA, alpha).apply(); + logger.log(TAG, "Background alpha: " + alpha); + } + + /** + * 获取背景透明度 + */ + public int getBackgroundAlpha() { + return prefs.getInt(KEY_BACKGROUND_ALPHA, 30); // 默认 30% 不透明度 + } + + /** + * 清除背景图片设置 + */ + public void clearBackground() { + prefs.edit() + .remove(KEY_BACKGROUND_URI) + .remove(KEY_BACKGROUND_ENABLED) + .apply(); + logger.log(TAG, "Background cleared"); + } + + /** + * 检查是否有有效的背景图片设置 + */ + public boolean hasValidBackground() { + return isBackgroundEnabled() && getBackgroundUri() != null && !getBackgroundUri().isEmpty(); + } +} diff --git a/app/src/main/res/layout/fragment_settings.xml b/app/src/main/res/layout/fragment_settings.xml index d793919..2bdd417 100644 --- a/app/src/main/res/layout/fragment_settings.xml +++ b/app/src/main/res/layout/fragment_settings.xml @@ -1,12 +1,18 @@ - + android:background="@color/bg_primary" + android:fillViewport="true"> + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/layout_with_background.xml b/app/src/main/res/layout/layout_with_background.xml new file mode 100644 index 0000000..d447db2 --- /dev/null +++ b/app/src/main/res/layout/layout_with_background.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + +