fix: 修复应用背景图片不生效的问题
问题:用户在设置页面选择背景图片后,主页背景不会更新 解决方案: 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 <noreply@anthropic.com>
This commit is contained in:
1 parent
8d1b592292
commit
1987462338
2 files changed
+28
-62
No files matched your search
@@ -291,5 +291,11 @@ public class HomeFragment extends Fragment {
|
|||||||
config = prefs.loadConfig();
|
config = prefs.loadConfig();
|
||||||
updateUI();
|
updateUI();
|
||||||
updateAlbumInfo();
|
updateAlbumInfo();
|
||||||
|
|
||||||
|
// 重新应用背景图片(从设置页面返回时可能需要更新)
|
||||||
|
View rootView = getView();
|
||||||
|
if (rootView != null) {
|
||||||
|
BackgroundHelper.applyBackground(requireContext(), rootView);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,7 +4,6 @@ import android.content.Context;
|
|||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
|
||||||
import android.widget.FrameLayout;
|
import android.widget.FrameLayout;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
|
|
||||||
@@ -31,6 +30,8 @@ public class BackgroundHelper {
|
|||||||
|
|
||||||
// 检查是否启用了背景图片
|
// 检查是否启用了背景图片
|
||||||
if (!prefs.hasValidBackground()) {
|
if (!prefs.hasValidBackground()) {
|
||||||
|
// 清除背景(如果之前有背景)
|
||||||
|
rootView.setBackground(null);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,71 +40,30 @@ public class BackgroundHelper {
|
|||||||
Uri uri = Uri.parse(uriString);
|
Uri uri = Uri.parse(uriString);
|
||||||
int alpha = prefs.getBackgroundAlpha();
|
int alpha = prefs.getBackgroundAlpha();
|
||||||
|
|
||||||
// 使用 Glide 加载背景图片
|
// 使用 Glide 异步加载背景图片
|
||||||
Drawable background = loadBackgroundDrawable(context, uri);
|
com.bumptech.glide.Glide.with(context)
|
||||||
|
|
||||||
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()
|
.asDrawable()
|
||||||
.load(uri)
|
.load(uri)
|
||||||
.centerCrop()
|
.centerCrop()
|
||||||
.submit(1080, 1920)
|
.into(new com.bumptech.glide.request.target.CustomTarget<Drawable>() {
|
||||||
.get();
|
@Override
|
||||||
|
public void onResourceReady(@NonNull Drawable resource, @Nullable com.bumptech.glide.request.transition.Transition<? super Drawable> 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) {
|
} catch (Exception e) {
|
||||||
android.util.Log.e("BackgroundHelper", "Error loading background drawable", e);
|
android.util.Log.e("BackgroundHelper", "Error applying background", e);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user