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); } }