From 1987462338fb7d89a76da6b13edf44387cbefed7 Mon Sep 17 00:00:00 2001 From: hugh <1012760428@qq.com> Date: Sun, 25 Jan 2026 15:09:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E8=83=8C=E6=99=AF=E5=9B=BE=E7=89=87=E4=B8=8D=E7=94=9F=E6=95=88?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题:用户在设置页面选择背景图片后,主页背景不会更新 解决方案: 1. 在 HomeFragment.onResume() 中重新应用背景图片 2. 改进 BackgroundHelper.applyBackground() 使用异步加载(Glide CustomTarget) 3. 当背景被禁用时自动清除背景(rootView.setBackground(null)) 技术细节: - 使用 Glide 异步加载背景图片,避免阻塞主线程 - 使用 CustomTarget 监听加载完成 - 检查 rootView.isAttachedToWindow() 确保视图仍然有效 - 在 onLoadCleared 时清除背景 现在背景图片会: - 选择后立即生效(在 Glide 加载完成后) - 从设置页面返回主页时自动刷新 - 禁用时自动清除 Co-Authored-By: Claude Sonnet 4.5 --- .../java/cn/hugh/bz/gk2/ui/HomeFragment.java | 6 ++ .../hugh/bz/gk2/utils/BackgroundHelper.java | 84 +++++-------------- 2 files changed, 28 insertions(+), 62 deletions(-) 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 b31f57e..92a3ee5 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 @@ -291,5 +291,11 @@ public class HomeFragment extends Fragment { config = prefs.loadConfig(); updateUI(); updateAlbumInfo(); + + // 重新应用背景图片(从设置页面返回时可能需要更新) + View rootView = getView(); + if (rootView != null) { + BackgroundHelper.applyBackground(requireContext(), rootView); + } } } 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 index 7aa21ee..61805da 100644 --- a/app/src/main/java/cn/hugh/bz/gk2/utils/BackgroundHelper.java +++ b/app/src/main/java/cn/hugh/bz/gk2/utils/BackgroundHelper.java @@ -4,7 +4,6 @@ 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; @@ -31,6 +30,8 @@ public class BackgroundHelper { // 检查是否启用了背景图片 if (!prefs.hasValidBackground()) { + // 清除背景(如果之前有背景) + rootView.setBackground(null); return; } @@ -39,71 +40,30 @@ public class BackgroundHelper { 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) + // 使用 Glide 异步加载背景图片 + com.bumptech.glide.Glide.with(context) .asDrawable() .load(uri) .centerCrop() - .submit(1080, 1920) - .get(); + .into(new com.bumptech.glide.request.target.CustomTarget() { + @Override + public void onResourceReady(@NonNull Drawable resource, @Nullable com.bumptech.glide.request.transition.Transition transition) { + if (resource != null && rootView.isAttachedToWindow()) { + // 设置透明度 + resource.setAlpha(alpha); + rootView.setBackground(resource); + } + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + // 清除背景 + rootView.setBackground(null); + } + }); + } catch (Exception e) { - android.util.Log.e("BackgroundHelper", "Error loading background drawable", e); - return null; + android.util.Log.e("BackgroundHelper", "Error applying background", e); } }