refactor: 简化主题系统并重构底部导航组件

- 移除多主题切换功能,简化主题配置
- 将 BottomNavigationView 替换为自定义 LinearLayout + TextView,解决 Material Components 主题兼容性问题
- 从设置页面移除主题选择功能
- 简化主题配置,只保留基本的 MaterialComponents 主题属性
- 添加底部导航颜色选择器资源

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
hughhuangandClaude Sonnet 4.5 committed 2026-01-25 16:06:45 +08:00
1 parent 1987462338
commit 8b570f0d34
11 files changed
+228 -93

No files matched your search

@@ -76,6 +76,9 @@ public class AlbumEditorActivity extends AppCompatActivity {
private ProgressDialog progressDialog;
private Handler mainHandler;
// 文件夹图片数量缓存
private final java.util.Map<String, Integer> folderImageCountCache = new java.util.HashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -231,6 +234,9 @@ public class AlbumEditorActivity extends AppCompatActivity {
@Override
public void onComplete(List<WallpaperImage> images) {
mainHandler.post(() -> {
// 缓存扫描结果
folderImageCountCache.put(folderUri, images.size());
dismissProgressDialog();
// 添加文件夹到列表
@@ -307,7 +313,7 @@ public class AlbumEditorActivity extends AppCompatActivity {
// 文件夹列表
rvFolders.setLayoutManager(new LinearLayoutManager(this));
folderAdapter = new FolderAdapter(this, folders, this::removeFolder);
folderAdapter = new FolderAdapter(this, folders, this::removeFolder, folderImageCountCache);
rvFolders.setAdapter(folderAdapter);
// 图片列表
@@ -348,6 +354,9 @@ public class AlbumEditorActivity extends AppCompatActivity {
// 返回桌面更换
switchChangeOnReturn.setChecked(album.isChangeOnReturnToHome());
// 预扫描所有文件夹(后台线程)
preloadFolderImageCounts();
}
} else {
// 创建新相册
@@ -362,6 +371,32 @@ public class AlbumEditorActivity extends AppCompatActivity {
}
}
/**
* 预加载所有文件夹的图片数量(编辑现有相册时)
* 在后台线程中并行扫描所有文件夹
*/
private void preloadFolderImageCounts() {
if (folders.isEmpty()) {
return;
}
new Thread(() -> {
ImageScanner scanner = new ImageScanner(this);
for (String folderUri : folders) {
// 检查是否已有缓存
if (!folderImageCountCache.containsKey(folderUri)) {
// 扫描文件夹
List<WallpaperImage> images = scanner.scanFolder(folderUri);
int count = images != null ? images.size() : 0;
// 缓存结果
folderImageCountCache.put(folderUri, count);
}
}
scanner.shutdown();
}).start();
}
private void openFolderPicker() {
// 使用 ActivityResultLauncher 打开文件夹选择器
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
@@ -383,6 +418,9 @@ public class AlbumEditorActivity extends AppCompatActivity {
private void removeFolder(String folderUri) {
folders.remove(folderUri);
folderAdapter.updateFolders(folders);
// 清除缓存
folderImageCountCache.remove(folderUri);
}
private void openImagePicker() {
@@ -488,18 +526,22 @@ public class AlbumEditorActivity extends AppCompatActivity {
}
/**
* 文件夹列表适配器(简化版)
* 文件夹列表适配器(优化版 - 避免重复扫描)
*/
private static class FolderAdapter extends RecyclerView.Adapter<FolderAdapter.ViewHolder> {
private final android.content.Context context;
private List<String> folders;
private final OnFolderRemoveListener removeListener;
private final java.util.Map<String, Integer> imageCountCache;
public FolderAdapter(android.content.Context context, List<String> folders, OnFolderRemoveListener removeListener) {
public FolderAdapter(android.content.Context context, List<String> folders,
OnFolderRemoveListener removeListener,
java.util.Map<String, Integer> imageCountCache) {
this.context = context;
this.folders = folders != null ? folders : new ArrayList<>();
this.removeListener = removeListener;
this.imageCountCache = imageCountCache != null ? imageCountCache : new java.util.HashMap<>();
}
public void updateFolders(List<String> folders) {
@@ -558,15 +600,25 @@ public class AlbumEditorActivity extends AppCompatActivity {
tvFolderPath.setText(folderUri);
}
// 初始显示"扫描中..."
// 优化:优先使用缓存,避免重复扫描
if (tvImageCount != null) {
// 检查缓存
Integer cachedCount = imageCountCache.get(folderUri);
if (cachedCount != null) {
// 使用缓存结果
tvImageCount.setText(cachedCount + " 张图片");
} else {
// 首次显示,标记为待扫描
tvImageCount.setText("扫描中...");
// 异步扫描图片数量
// 异步扫描(只扫描一次)
new Thread(() -> {
ImageScanner scanner = new ImageScanner(context);
List<WallpaperImage> images = scanner.scanFolder(folderUri);
int count = images != null ? images.size() : 0;
// 更新缓存
imageCountCache.put(folderUri, count);
scanner.shutdown();
// 更新 UI
@@ -575,6 +627,7 @@ public class AlbumEditorActivity extends AppCompatActivity {
});
}).start();
}
}
btnRemove.setOnClickListener(v -> {
if (removeListener != null) {
@@ -25,6 +25,7 @@ import cn.hugh.bz.gk2.R;
import cn.hugh.bz.gk2.model.Album;
import cn.hugh.bz.gk2.model.PlayMode;
import cn.hugh.bz.gk2.model.WallpaperImage;
import cn.hugh.bz.gk2.utils.BackgroundHelper;
import cn.hugh.bz.gk2.utils.ImageScanner;
import cn.hugh.bz.gk2.wallpaper.AlbumPreferences;
@@ -63,6 +64,9 @@ public class AlbumManagementFragment extends Fragment {
initViews(view);
loadAlbums();
// 应用背景图片
BackgroundHelper.applyBackground(requireContext(), view);
}
private void initViews(View view) {
@@ -163,6 +167,12 @@ public class AlbumManagementFragment extends Fragment {
super.onResume();
loadAlbums(); // 从编辑页面返回时刷新列表
// 重新应用背景图片
View rootView = getView();
if (rootView != null) {
BackgroundHelper.applyBackground(requireContext(), rootView);
}
// 处理从历史页面跳转过来的逻辑
if (targetAlbumId != null) {
handleTargetAlbum();
@@ -19,6 +19,7 @@ import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import cn.hugh.bz.gk2.R;
import cn.hugh.bz.gk2.utils.BackgroundHelper;
import cn.hugh.bz.gk2.utils.ImageLoader;
import cn.hugh.bz.gk2.wallpaper.WallpaperPreferences;
@@ -52,6 +53,9 @@ public class CurrentWallpaperFragment extends Fragment {
initViews(view);
loadCurrentWallpaper();
// 应用背景图片
BackgroundHelper.applyBackground(requireContext(), view);
}
private void initViews(View view) {
@@ -190,6 +194,12 @@ public class CurrentWallpaperFragment extends Fragment {
public void onResume() {
super.onResume();
loadCurrentWallpaper();
// 重新应用背景图片
View rootView = getView();
if (rootView != null) {
BackgroundHelper.applyBackground(requireContext(), rootView);
}
}
@Override
@@ -11,8 +11,8 @@ import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.hugh.bz.gk2.R;
@@ -41,7 +41,8 @@ public class MainActivity extends AppCompatActivity {
}
private ViewPager2 viewPager;
private BottomNavigationView bottomNav;
private LinearLayout bottomNav;
private TextView[] navItems;
private ViewPagerAdapter adapter;
// Folder picker launcher
@@ -130,13 +131,38 @@ public class MainActivity extends AppCompatActivity {
}
private void setupBottomNav() {
bottomNav.setOnItemSelectedListener(item -> {
int position = item.getOrder();
// 初始化导航项数组
navItems = new TextView[]{
findViewById(R.id.navHome),
findViewById(R.id.navWallpaper),
findViewById(R.id.navAlbum),
findViewById(R.id.navHistory),
findViewById(R.id.navSettings)
};
// 设置点击监听器
for (int i = 0; i < navItems.length; i++) {
final int position = i;
navItems[i].setOnClickListener(v -> {
viewPager.setCurrentItem(position, false);
return true;
updateNavSelection(position);
});
}
// 默认选中第一项
updateNavSelection(0);
}
private void updateNavSelection(int position) {
for (int i = 0; i < navItems.length; i++) {
if (i == position) {
navItems[i].setTextColor(getColor(R.color.accent_primary));
} else {
navItems[i].setTextColor(getColor(android.R.color.darker_gray));
}
}
}
/**
* 启动文件夹选择器
*/
@@ -165,7 +191,7 @@ public class MainActivity extends AppCompatActivity {
if (viewPager != null && position >= 0 && position < 5) {
viewPager.setCurrentItem(position, false);
// 更新底部导航选中状态
bottomNav.getMenu().getItem(position).setChecked(true);
updateNavSelection(position);
}
}
}
@@ -4,7 +4,6 @@ import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;
@@ -20,6 +19,8 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.material.button.MaterialButton;
import cn.hugh.bz.gk2.R;
import cn.hugh.bz.gk2.wallpaper.BackgroundPreferences;
@@ -31,8 +32,8 @@ public class SettingsFragment extends Fragment {
// 背景图片相关控件
private Switch switchBackground;
private Button btnSelectBackground;
private Button btnClearBackground;
private MaterialButton btnSelectBackground;
private MaterialButton btnClearBackground;
private SeekBar seekBarBackgroundAlpha;
private TextView tvBackgroundAlpha;
@@ -161,9 +162,10 @@ public class SettingsFragment extends Fragment {
* 选择背景图片
*/
private void pickBackgroundImage() {
Intent intent = new Intent(Intent.ACTION_PICK);
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
pickImageLauncher.launch(Intent.createChooser(intent, "选择背景图片"));
pickImageLauncher.launch(intent);
}
/**
@@ -29,6 +29,7 @@ import java.util.Locale;
import cn.hugh.bz.gk2.R;
import cn.hugh.bz.gk2.model.WallpaperHistory;
import cn.hugh.bz.gk2.utils.BackgroundHelper;
import cn.hugh.bz.gk2.wallpaper.WallpaperHistoryManager;
/**
@@ -63,6 +64,9 @@ public class WallpaperHistoryFragment extends Fragment {
initViews(view);
loadHistory();
// 应用背景图片
BackgroundHelper.applyBackground(requireContext(), view);
}
private void initViews(View view) {
@@ -158,6 +162,12 @@ public class WallpaperHistoryFragment extends Fragment {
public void onResume() {
super.onResume();
loadHistory(); // 从其他页面返回时刷新列表
// 重新应用背景图片
View rootView = getView();
if (rootView != null) {
BackgroundHelper.applyBackground(requireContext(), rootView);
}
}
/**
@@ -7,6 +7,9 @@ import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import cn.hugh.bz.gk2.wallpaper.BackgroundPreferences;
/**
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#FF7C3AED" android:state_checked="true" />
<item android:color="#FF6B7280" />
</selector>
+69 -9
View File
@@ -5,7 +5,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_primary"
android:background="?android:colorBackground"
tools:context=".ui.MainActivity">
<androidx.viewpager2.widget.ViewPager2
@@ -17,17 +17,77 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
<LinearLayout
android:id="@+id/bottomNav"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/bg_card"
app:itemIconTint="@color/accent_primary"
app:itemTextColor="@color/text_secondary"
app:labelVisibilityMode="labeled"
app:menu="@menu/bottom_nav_menu"
android:layout_height="56dp"
android:orientation="horizontal"
android:background="@android:color/white"
android:elevation="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
app:layout_constraintEnd_toEndOf="parent">
<TextView
android:id="@+id/navHome"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="主页"
android:textSize="12sp"
android:textColor="@color/bottom_nav_color"
android:clickable="true"
android:focusable="true" />
<TextView
android:id="@+id/navWallpaper"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="壁纸"
android:textSize="12sp"
android:textColor="@color/bottom_nav_color"
android:clickable="true"
android:focusable="true" />
<TextView
android:id="@+id/navAlbum"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="相册"
android:textSize="12sp"
android:textColor="@color/bottom_nav_color"
android:clickable="true"
android:focusable="true" />
<TextView
android:id="@+id/navHistory"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="历史"
android:textSize="12sp"
android:textColor="@color/bottom_nav_color"
android:clickable="true"
android:focusable="true" />
<TextView
android:id="@+id/navSettings"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="设置"
android:textSize="12sp"
android:textColor="@color/bottom_nav_color"
android:clickable="true"
android:focusable="true" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
+18 -18
View File
@@ -9,30 +9,30 @@
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<!-- Dark Luxury Theme - Background Colors -->
<color name="bg_primary">#FF0D0D0F</color> <!-- 深邃黑 -->
<color name="bg_secondary">#FF161618</color> <!-- 炭灰 -->
<color name="bg_card">#FF1E1E22</color> <!-- 暗灰 -->
<!-- Light Luxury Theme - Background Colors -->
<color name="bg_primary">#FFFFFFFF</color> <!-- 纯白 -->
<color name="bg_secondary">#FFF5F5F7</color> <!-- 浅灰 -->
<color name="bg_card">#FFFFFFFF</color> <!-- 白色卡片 -->
<!-- Dark Luxury Theme - Accent Colors -->
<color name="accent_primary">#FF8B5CF6</color> <!-- 皇家紫 -->
<color name="accent_secondary">#FFA78BFA</color><!-- 薰衣草紫 -->
<color name="gradient_start">#FF7C3AED</color> <!-- 深紫 -->
<color name="gradient_end">#FFEC4899</color> <!-- 玫红 -->
<!-- Light Luxury Theme - Accent Colors -->
<color name="accent_primary">#FF7C3AED</color> <!-- 深紫 -->
<color name="accent_secondary">#FF8B5CF6</color><!-- 皇家紫 -->
<color name="gradient_start">#FF6366F1</color> <!-- 靛蓝 -->
<color name="gradient_end">#FFA855F7</color> <!-- 紫色 -->
<!-- Dark Luxury Theme - Status Colors -->
<!-- Light Luxury Theme - Status Colors -->
<color name="status_active">#FF10B981</color> <!-- 翡翠绿 -->
<color name="status_inactive">#FF6B7280</color> <!-- 暗银 -->
<color name="status_inactive">#FF9CA3AF</color> <!-- 银灰 -->
<color name="color_error">#FFEF4444</color> <!-- 珊瑚红 -->
<!-- Dark Luxury Theme - Text Colors -->
<color name="text_primary">#FFF9FAFB</color> <!-- 近白 -->
<color name="text_secondary">#FF9CA3AF</color> <!-- 银灰 -->
<color name="text_hint">#FF6B7280</color> <!-- 暗银 -->
<!-- Light Luxury Theme - Text Colors -->
<color name="text_primary">#FF111827</color> <!-- 深黑 -->
<color name="text_secondary">#FF6B7280</color> <!-- 中灰 -->
<color name="text_hint">#FF9CA3AF</color> <!-- 浅灰 -->
<!-- Dark Luxury Theme - Glass Effect -->
<color name="glass_bg">#14FFFFFF</color> <!-- 8% 白色 -->
<color name="glass_border">#1FFFFFFF</color> <!-- 12% 白色 -->
<!-- Light Luxury Theme - Glass Effect -->
<color name="glass_bg">#33000000</color> <!-- 20% 黑色 -->
<color name="glass_border">#1A000000</color> <!-- 10% 黑色 -->
<!-- Legacy theme colors (mapped to new theme) -->
<color name="primary">@color/accent_primary</color>
+5 -49
View File
@@ -1,53 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme - Dark Luxury -->
<style name="Theme.BzForGk2" parent="Theme.Material3.Dark.NoActionBar">
<!-- Primary brand color -->
<item name="colorPrimary">@color/accent_primary</item>
<item name="colorPrimaryVariant">@color/gradient_start</item>
<item name="colorOnPrimary">@color/text_primary</item>
<!-- Secondary brand color -->
<item name="colorSecondary">@color/accent_secondary</item>
<item name="colorSecondaryVariant">@color/gradient_end</item>
<item name="colorOnSecondary">@color/text_primary</item>
<!-- Background colors -->
<item name="android:colorBackground">@color/bg_primary</item>
<item name="colorSurface">@color/bg_card</item>
<item name="colorOnSurface">@color/text_primary</item>
<!-- Status bar color -->
<item name="android:statusBarColor">@color/bg_primary</item>
<item name="android:navigationBarColor">@color/bg_primary</item>
<!-- Text colors -->
<item name="android:textColorPrimary">@color/text_primary</item>
<item name="android:textColorSecondary">@color/text_secondary</item>
<item name="android:textColorHint">@color/text_hint</item>
<!-- Error color -->
<item name="colorError">@color/color_error</item>
<!-- Material 3 shape -->
<item name="shapeAppearanceSmallComponent">@style/ShapeAppearance.BzForGk2.SmallComponent</item>
<item name="shapeAppearanceMediumComponent">@style/ShapeAppearance.BzForGk2.MediumComponent</item>
<item name="shapeAppearanceLargeComponent">@style/ShapeAppearance.BzForGk2.LargeComponent</item>
</style>
<!-- Shape appearances for rounded corners -->
<style name="ShapeAppearance.BzForGk2.SmallComponent" parent="ShapeAppearance.Material3.SmallComponent">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">12dp</item>
</style>
<style name="ShapeAppearance.BzForGk2.MediumComponent" parent="ShapeAppearance.Material3.MediumComponent">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">16dp</item>
</style>
<style name="ShapeAppearance.BzForGk2.LargeComponent" parent="ShapeAppearance.Material3.LargeComponent">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">24dp</item>
<!-- 默认主题 -->
<style name="Theme.BzForGk2" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">#FF7C3AED</item>
<item name="colorPrimaryDark">#FF6366F1</item>
<item name="colorAccent">#FF8B5CF6</item>
</style>
</resources>