From 62215a927906871e544831caac3b649b557402af Mon Sep 17 00:00:00 2001 From: hugh <1012760428@qq.com> Date: Fri, 23 Jan 2026 11:18:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=A3=81=E7=BA=B8?= =?UTF-8?q?=E5=8E=86=E5=8F=B2=E8=AE=B0=E5=BD=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增全局壁纸历史记录功能,记录所有使用过的壁纸,支持缩略图展示和系统相册打开。 核心功能: - 自动记录壁纸使用历史(最多保存50条) - 缩略图缓存(保持原图宽高比) - 2列网格布局展示 - 点击历史记录使用系统相册打开 - 支持删除单条记录和清空全部 新增文件: - WallpaperHistory.java - 历史记录数据模型 - WallpaperHistoryManager.java - 历史记录管理器 - WallpaperHistoryFragment.java - 历史页面 - fragment_wallpaper_history.xml - 页面布局 - item_history_card.xml - 卡片项布局 - ic_history.xml - 历史图标 修改文件: - AutoWallpaperService.java - 集成历史记录保存 - ViewPagerAdapter.java - 扩展为5个页面 - MainActivity.java - 更新页面切换范围 - bottom_nav_menu.xml - 添加历史标签 - strings.xml - 添加历史标签文本 - colors.xml - 添加图片占位符颜色 Co-Authored-By: Claude Sonnet 4.5 --- .../hugh/bz/gk2/model/WallpaperHistory.java | 79 ++++++ .../bz/gk2/ui/AlbumManagementFragment.java | 39 +++ .../java/cn/hugh/bz/gk2/ui/MainActivity.java | 4 +- .../cn/hugh/bz/gk2/ui/ViewPagerAdapter.java | 6 +- .../bz/gk2/ui/WallpaperHistoryFragment.java | 256 +++++++++++++++++ .../gk2/wallpaper/AutoWallpaperService.java | 16 ++ .../wallpaper/WallpaperHistoryManager.java | 267 ++++++++++++++++++ app/src/main/res/drawable/ic_history.xml | 10 + .../res/layout/fragment_wallpaper_history.xml | 68 +++++ app/src/main/res/layout/item_history_card.xml | 80 ++++++ app/src/main/res/menu/bottom_nav_menu.xml | 8 +- app/src/main/res/values/colors.xml | 1 + app/src/main/res/values/strings.xml | 1 + 13 files changed, 830 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/cn/hugh/bz/gk2/model/WallpaperHistory.java create mode 100644 app/src/main/java/cn/hugh/bz/gk2/ui/WallpaperHistoryFragment.java create mode 100644 app/src/main/java/cn/hugh/bz/gk2/wallpaper/WallpaperHistoryManager.java create mode 100644 app/src/main/res/drawable/ic_history.xml create mode 100644 app/src/main/res/layout/fragment_wallpaper_history.xml create mode 100644 app/src/main/res/layout/item_history_card.xml diff --git a/app/src/main/java/cn/hugh/bz/gk2/model/WallpaperHistory.java b/app/src/main/java/cn/hugh/bz/gk2/model/WallpaperHistory.java new file mode 100644 index 0000000..3f6e309 --- /dev/null +++ b/app/src/main/java/cn/hugh/bz/gk2/model/WallpaperHistory.java @@ -0,0 +1,79 @@ +package cn.hugh.bz.gk2.model; + +import java.io.Serializable; +import java.util.UUID; + +/** + * 壁纸历史记录数据模型 + * 记录所有使用过的壁纸信息 + */ +public class WallpaperHistory implements Serializable { + private String id; // 历史记录唯一ID + private String uri; // 壁纸URI + private String albumId; // 所属相册ID + private String albumName; // 所属相册名称 + private long timestamp; // 使用时间戳 + private String thumbnailPath; // 缩略图缓存路径 + + public WallpaperHistory() { + this.id = UUID.randomUUID().toString(); + this.timestamp = System.currentTimeMillis(); + } + + public WallpaperHistory(String uri, String albumId, String albumName, String thumbnailPath) { + this(); + this.uri = uri; + this.albumId = albumId; + this.albumName = albumName; + this.thumbnailPath = thumbnailPath; + } + + // Getters and Setters + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getUri() { + return uri; + } + + public void setUri(String uri) { + this.uri = uri; + } + + public String getAlbumId() { + return albumId; + } + + public void setAlbumId(String albumId) { + this.albumId = albumId; + } + + public String getAlbumName() { + return albumName; + } + + public void setAlbumName(String albumName) { + this.albumName = albumName; + } + + public long getTimestamp() { + return timestamp; + } + + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + } + + public String getThumbnailPath() { + return thumbnailPath; + } + + public void setThumbnailPath(String thumbnailPath) { + this.thumbnailPath = thumbnailPath; + } +} diff --git a/app/src/main/java/cn/hugh/bz/gk2/ui/AlbumManagementFragment.java b/app/src/main/java/cn/hugh/bz/gk2/ui/AlbumManagementFragment.java index 1fe329b..de661c2 100644 --- a/app/src/main/java/cn/hugh/bz/gk2/ui/AlbumManagementFragment.java +++ b/app/src/main/java/cn/hugh/bz/gk2/ui/AlbumManagementFragment.java @@ -40,6 +40,9 @@ public class AlbumManagementFragment extends Fragment { private AlbumAdapter adapter; private List albums; + // 目标相册ID(用于从历史页面跳转) + public static String targetAlbumId = null; + @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -157,6 +160,42 @@ public class AlbumManagementFragment extends Fragment { public void onResume() { super.onResume(); loadAlbums(); // 从编辑页面返回时刷新列表 + + // 处理从历史页面跳转过来的逻辑 + if (targetAlbumId != null) { + handleTargetAlbum(); + } + } + + /** + * 处理目标相册(从历史页面跳转) + */ + private void handleTargetAlbum() { + if (albums == null || targetAlbumId == null) { + return; + } + + // 查找目标相册 + Album targetAlbum = null; + for (Album album : albums) { + if (album.getId().equals(targetAlbumId)) { + targetAlbum = album; + break; + } + } + + if (targetAlbum != null) { + // 高亮显示该相册(可以添加视觉反馈,比如滚动到该位置并闪烁) + // 这里简单切换到该相册 + albumPrefs.setActiveAlbumId(targetAlbumId); + Toast.makeText(requireContext(), "已跳转到相册:" + targetAlbum.getName(), Toast.LENGTH_SHORT).show(); + loadAlbums(); // 刷新列表以更新"当前"徽章 + } else { + Toast.makeText(requireContext(), "未找到相册", Toast.LENGTH_SHORT).show(); + } + + // 重置目标相册ID + targetAlbumId = null; } /** diff --git a/app/src/main/java/cn/hugh/bz/gk2/ui/MainActivity.java b/app/src/main/java/cn/hugh/bz/gk2/ui/MainActivity.java index 76e2d68..866ca93 100644 --- a/app/src/main/java/cn/hugh/bz/gk2/ui/MainActivity.java +++ b/app/src/main/java/cn/hugh/bz/gk2/ui/MainActivity.java @@ -102,10 +102,10 @@ public class MainActivity extends AppCompatActivity implements HomeFragment.Fold /** * 切换到指定的页面 - * @param position 页面索引(0=主页,1=壁纸,2=相册,3=设置) + * @param position 页面索引(0=主页,1=壁纸,2=相册,3=历史,4=设置) */ public void switchToPage(int position) { - if (viewPager != null && position >= 0 && position < 4) { + if (viewPager != null && position >= 0 && position < 5) { viewPager.setCurrentItem(position, false); // 更新底部导航选中状态 bottomNav.getMenu().getItem(position).setChecked(true); diff --git a/app/src/main/java/cn/hugh/bz/gk2/ui/ViewPagerAdapter.java b/app/src/main/java/cn/hugh/bz/gk2/ui/ViewPagerAdapter.java index bb96bae..98c58e1 100644 --- a/app/src/main/java/cn/hugh/bz/gk2/ui/ViewPagerAdapter.java +++ b/app/src/main/java/cn/hugh/bz/gk2/ui/ViewPagerAdapter.java @@ -6,10 +6,10 @@ import androidx.fragment.app.FragmentActivity; import androidx.viewpager2.adapter.FragmentStateAdapter; /** - * ViewPager2 适配器,用于管理四个 Fragment 页面 + * ViewPager2 适配器,用于管理五个 Fragment 页面 */ public class ViewPagerAdapter extends FragmentStateAdapter { - private static final int NUM_PAGES = 4; + private static final int NUM_PAGES = 5; public ViewPagerAdapter(FragmentActivity fa) { super(fa); @@ -26,6 +26,8 @@ public class ViewPagerAdapter extends FragmentStateAdapter { case 2: return new AlbumManagementFragment(); case 3: + return new WallpaperHistoryFragment(); + case 4: return new SettingsFragment(); default: return new HomeFragment(); diff --git a/app/src/main/java/cn/hugh/bz/gk2/ui/WallpaperHistoryFragment.java b/app/src/main/java/cn/hugh/bz/gk2/ui/WallpaperHistoryFragment.java new file mode 100644 index 0000000..84bca69 --- /dev/null +++ b/app/src/main/java/cn/hugh/bz/gk2/ui/WallpaperHistoryFragment.java @@ -0,0 +1,256 @@ +package cn.hugh.bz.gk2.ui; + +import android.content.Context; +import android.content.Intent; +import android.graphics.Bitmap; +import android.net.Uri; +import android.os.Bundle; +import android.provider.DocumentsContract; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; +import androidx.fragment.app.Fragment; +import androidx.recyclerview.widget.GridLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import com.google.android.material.button.MaterialButton; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Locale; + +import cn.hugh.bz.gk2.R; +import cn.hugh.bz.gk2.model.WallpaperHistory; +import cn.hugh.bz.gk2.wallpaper.WallpaperHistoryManager; + +/** + * 壁纸历史页面 + * 显示所有使用过的壁纸历史记录,支持缩略图展示和跳转到相册 + */ +public class WallpaperHistoryFragment extends Fragment { + + private RecyclerView rvHistory; + private MaterialButton btnClearHistory; + private TextView tvEmptyHint; + + private WallpaperHistoryManager historyManager; + private HistoryAdapter adapter; + private List historyList; + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + historyManager = new WallpaperHistoryManager(requireContext()); + } + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + return inflater.inflate(R.layout.fragment_wallpaper_history, container, false); + } + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + initViews(view); + loadHistory(); + } + + private void initViews(View view) { + rvHistory = view.findViewById(R.id.rvHistory); + btnClearHistory = view.findViewById(R.id.btnClearHistory); + tvEmptyHint = view.findViewById(R.id.tvEmptyHint); + + // 设置 RecyclerView 为2列网格布局 + rvHistory.setLayoutManager(new GridLayoutManager(requireContext(), 2)); + adapter = new HistoryAdapter(requireContext(), historyList, new HistoryAdapter.HistoryCallback() { + @Override + public void onItemClick(WallpaperHistory history) { + openAlbumForHistory(history); + } + + @Override + public void onDelete(WallpaperHistory history) { + deleteHistory(history); + } + }); + rvHistory.setAdapter(adapter); + + // 清空历史按钮 + btnClearHistory.setOnClickListener(v -> confirmClearHistory()); + } + + private void loadHistory() { + historyList = historyManager.loadHistory(); + adapter.updateHistory(historyList); + updateEmptyHint(); + } + + private void updateEmptyHint() { + if (historyList == null || historyList.isEmpty()) { + tvEmptyHint.setVisibility(View.VISIBLE); + rvHistory.setVisibility(View.GONE); + btnClearHistory.setEnabled(false); + } else { + tvEmptyHint.setVisibility(View.GONE); + rvHistory.setVisibility(View.VISIBLE); + btnClearHistory.setEnabled(true); + } + } + + /** + * 点击历史记录,使用系统相册/图片查看器打开 + */ + private void openAlbumForHistory(WallpaperHistory history) { + try { + Uri uri = Uri.parse(history.getUri()); + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setDataAndType(uri, "image/*"); + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + startActivity(intent); + } catch (Exception e) { + Toast.makeText(requireContext(), "无法打开图片", Toast.LENGTH_SHORT).show(); + } + } + + /** + * 删除单条历史记录 + */ + private void deleteHistory(WallpaperHistory history) { + new AlertDialog.Builder(requireContext()) + .setTitle("删除记录") + .setMessage("确定要删除这条历史记录吗?") + .setPositiveButton("删除", (dialog, which) -> { + historyManager.deleteHistory(history.getId()); + Toast.makeText(requireContext(), "已删除", Toast.LENGTH_SHORT).show(); + loadHistory(); + }) + .setNegativeButton("取消", null) + .show(); + } + + /** + * 清空所有历史记录 + */ + private void confirmClearHistory() { + new AlertDialog.Builder(requireContext()) + .setTitle("清空历史") + .setMessage("确定要清空所有历史记录吗?此操作不可恢复。") + .setPositiveButton("清空", (dialog, which) -> { + historyManager.clearHistory(); + Toast.makeText(requireContext(), "已清空所有历史记录", Toast.LENGTH_SHORT).show(); + loadHistory(); + }) + .setNegativeButton("取消", null) + .show(); + } + + @Override + public void onResume() { + super.onResume(); + loadHistory(); // 从其他页面返回时刷新列表 + } + + /** + * 历史记录适配器 + */ + private static class HistoryAdapter extends RecyclerView.Adapter { + + private final Context context; + private List historyList; + private final HistoryCallback callback; + private final WallpaperHistoryManager historyManager; + + public HistoryAdapter(Context context, List historyList, HistoryCallback callback) { + this.context = context; + this.historyList = historyList != null ? historyList : new ArrayList<>(); + this.callback = callback; + this.historyManager = new WallpaperHistoryManager(context); + } + + public void updateHistory(List historyList) { + this.historyList = historyList != null ? historyList : new ArrayList<>(); + notifyDataSetChanged(); + } + + @NonNull + @Override + public HistoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = LayoutInflater.from(context).inflate(R.layout.item_history_card, parent, false); + return new HistoryViewHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull HistoryViewHolder holder, int position) { + WallpaperHistory history = historyList.get(position); + holder.bind(history); + } + + @Override + public int getItemCount() { + return historyList.size(); + } + + class HistoryViewHolder extends RecyclerView.ViewHolder { + private final android.widget.ImageView ivThumbnail; + private final TextView tvAlbumName; + private final TextView tvTimestamp; + private final MaterialButton btnDelete; + + public HistoryViewHolder(@NonNull View itemView) { + super(itemView); + ivThumbnail = itemView.findViewById(R.id.ivThumbnail); + tvAlbumName = itemView.findViewById(R.id.tvAlbumName); + tvTimestamp = itemView.findViewById(R.id.tvTimestamp); + btnDelete = itemView.findViewById(R.id.btnDelete); + } + + public void bind(WallpaperHistory history) { + // 加载缩略图 + Bitmap thumbnail = historyManager.loadThumbnail(history.getThumbnailPath()); + if (thumbnail != null) { + ivThumbnail.setImageBitmap(thumbnail); + } else { + // 显示占位符 + ivThumbnail.setImageDrawable(null); + } + + // 相册名称 + tvAlbumName.setText(history.getAlbumName()); + + // 时间戳格式化 + SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault()); + String timestamp = sdf.format(new Date(history.getTimestamp())); + tvTimestamp.setText(timestamp); + + // 卡片点击事件 + itemView.setOnClickListener(v -> { + if (callback != null) { + callback.onItemClick(history); + } + }); + + // 删除按钮 + btnDelete.setOnClickListener(v -> { + if (callback != null) { + callback.onDelete(history); + } + }); + } + } + + interface HistoryCallback { + void onItemClick(WallpaperHistory history); + void onDelete(WallpaperHistory history); + } + } +} diff --git a/app/src/main/java/cn/hugh/bz/gk2/wallpaper/AutoWallpaperService.java b/app/src/main/java/cn/hugh/bz/gk2/wallpaper/AutoWallpaperService.java index 4a59dce..6708ba0 100644 --- a/app/src/main/java/cn/hugh/bz/gk2/wallpaper/AutoWallpaperService.java +++ b/app/src/main/java/cn/hugh/bz/gk2/wallpaper/AutoWallpaperService.java @@ -10,6 +10,7 @@ import android.service.wallpaper.WallpaperService; import android.util.Log; import android.view.SurfaceHolder; +import cn.hugh.bz.gk2.model.Album; import cn.hugh.bz.gk2.model.AppConfig; import cn.hugh.bz.gk2.model.PlayMode; import cn.hugh.bz.gk2.model.WallpaperImage; @@ -56,6 +57,8 @@ public class AutoWallpaperService extends WallpaperService { class WallpaperEngine extends Engine { private final ImageLoader imageLoader; private final WallpaperPreferences prefs; + private final WallpaperHistoryManager historyManager; + private final AlbumPreferences albumPrefs; private final Handler handler; private int width; @@ -66,6 +69,8 @@ public class AutoWallpaperService extends WallpaperService { public WallpaperEngine() { this.imageLoader = new ImageLoader(AutoWallpaperService.this); this.prefs = new WallpaperPreferences(AutoWallpaperService.this); + this.historyManager = new WallpaperHistoryManager(AutoWallpaperService.this); + this.albumPrefs = new AlbumPreferences(AutoWallpaperService.this); this.handler = new Handler(Looper.getMainLooper()); } @@ -227,6 +232,17 @@ public class AutoWallpaperService extends WallpaperService { canvas.drawBitmap(bitmap, null, new RectF(left, top, left + scaledWidth, top + scaledHeight), null); + + // 保存到历史记录 + Album activeAlbum = albumPrefs.getActiveAlbum(); + if (activeAlbum != null) { + historyManager.addHistory( + uri, + activeAlbum.getId(), + activeAlbum.getName(), + bitmap + ); + } } else { Log.e(TAG, "Failed to load bitmap: " + uri); } diff --git a/app/src/main/java/cn/hugh/bz/gk2/wallpaper/WallpaperHistoryManager.java b/app/src/main/java/cn/hugh/bz/gk2/wallpaper/WallpaperHistoryManager.java new file mode 100644 index 0000000..b2b2fd6 --- /dev/null +++ b/app/src/main/java/cn/hugh/bz/gk2/wallpaper/WallpaperHistoryManager.java @@ -0,0 +1,267 @@ +package cn.hugh.bz.gk2.wallpaper; + +import android.content.Context; +import android.content.SharedPreferences; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.util.Log; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; + +import cn.hugh.bz.gk2.model.WallpaperHistory; + +/** + * 壁纸历史管理器 + * 负责管理壁纸历史记录的存储、加载和缩略图缓存 + */ +public class WallpaperHistoryManager { + private static final String TAG = "WallpaperHistoryManager"; + private static final String PREFS_NAME = "wallpaper_history_prefs"; + private static final String KEY_HISTORY = "history"; + private static final int MAX_HISTORY_COUNT = 50; + private static final int THUMBNAIL_SIZE = 200; + private static final int THUMBNAIL_QUALITY = 85; + + private final SharedPreferences prefs; + private final Gson gson; + private final File thumbnailDir; + + public WallpaperHistoryManager(Context context) { + this.prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); + this.gson = new Gson(); + + // 初始化缩略图缓存目录 + this.thumbnailDir = new File(context.getCacheDir(), "wallpaper_thumbnails"); + if (!thumbnailDir.exists()) { + thumbnailDir.mkdirs(); + } + } + + /** + * 添加历史记录 + * @param uri 壁纸URI + * @param albumId 所属相册ID + * @param albumName 所属相册名称 + * @param bitmap 壁纸位图(用于生成缩略图) + */ + public void addHistory(String uri, String albumId, String albumName, Bitmap bitmap) { + if (uri == null || albumId == null || albumName == null) { + Log.w(TAG, "Invalid parameters: uri, albumId, or albumName is null"); + return; + } + + List historyList = loadHistory(); + + // URI去重:删除相同URI的旧记录 + Iterator iterator = historyList.iterator(); + while (iterator.hasNext()) { + WallpaperHistory history = iterator.next(); + if (uri.equals(history.getUri())) { + // 删除旧的缩略图文件 + deleteThumbnailFile(history.getThumbnailPath()); + iterator.remove(); + Log.d(TAG, "Removed duplicate history record for URI: " + uri); + } + } + + // 生成缩略图 + String thumbnailPath = saveThumbnail(bitmap); + + // 创建新的历史记录 + WallpaperHistory history = new WallpaperHistory(uri, albumId, albumName, thumbnailPath); + historyList.add(0, history); // 添加到列表开头 + + // FIFO队列:超过50条时删除最旧的记录 + if (historyList.size() > MAX_HISTORY_COUNT) { + WallpaperHistory oldest = historyList.remove(historyList.size() - 1); + deleteThumbnailFile(oldest.getThumbnailPath()); + Log.d(TAG, "Removed oldest history record, size now: " + historyList.size()); + } + + // 保存到 SharedPreferences + saveHistoryList(historyList); + Log.d(TAG, "Added new history record, total count: " + historyList.size()); + } + + /** + * 加载所有历史记录(按时间倒序) + */ + public List loadHistory() { + String historyJson = prefs.getString(KEY_HISTORY, null); + if (historyJson == null) { + return new ArrayList<>(); + } + + try { + Type listType = new TypeToken>(){}.getType(); + List historyList = gson.fromJson(historyJson, listType); + + // 按时间倒序排序 + Collections.sort(historyList, new Comparator() { + @Override + public int compare(WallpaperHistory h1, WallpaperHistory h2) { + return Long.compare(h2.getTimestamp(), h1.getTimestamp()); + } + }); + + return historyList; + } catch (Exception e) { + Log.e(TAG, "Failed to load history", e); + return new ArrayList<>(); + } + } + + /** + * 删除单条历史记录 + */ + public void deleteHistory(String historyId) { + List historyList = loadHistory(); + + Iterator iterator = historyList.iterator(); + while (iterator.hasNext()) { + WallpaperHistory history = iterator.next(); + if (historyId.equals(history.getId())) { + // 删除缩略图文件 + deleteThumbnailFile(history.getThumbnailPath()); + iterator.remove(); + break; + } + } + + saveHistoryList(historyList); + Log.d(TAG, "Deleted history record: " + historyId); + } + + /** + * 清空所有历史记录 + */ + public void clearHistory() { + List historyList = loadHistory(); + + // 删除所有缩略图文件 + for (WallpaperHistory history : historyList) { + deleteThumbnailFile(history.getThumbnailPath()); + } + + // 清空列表 + saveHistoryList(new ArrayList()); + Log.d(TAG, "Cleared all history records"); + } + + /** + * 生成并保存缩略图(保持宽高比) + * @param bitmap 原始位图 + * @return 缩略图文件路径 + */ + private String saveThumbnail(Bitmap bitmap) { + if (bitmap == null) { + return null; + } + + // 生成唯一文件名 + String fileName = System.currentTimeMillis() + "_" + (int)(Math.random() * 1000) + ".jpg"; + File thumbnailFile = new File(thumbnailDir, fileName); + + FileOutputStream fos = null; + try { + // 计算缩放比例,保持宽高比 + int originalWidth = bitmap.getWidth(); + int originalHeight = bitmap.getHeight(); + float scale = Math.min( + (float) THUMBNAIL_SIZE / originalWidth, + (float) THUMBNAIL_SIZE / originalHeight + ); + + int scaledWidth = Math.round(originalWidth * scale); + int scaledHeight = Math.round(originalHeight * scale); + + // 缩放位图,保持宽高比 + Bitmap thumbnail = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true); + + // 保存为 JPEG + fos = new FileOutputStream(thumbnailFile); + thumbnail.compress(Bitmap.CompressFormat.JPEG, THUMBNAIL_QUALITY, fos); + + Log.d(TAG, "Saved thumbnail: " + thumbnailFile.getAbsolutePath() + + " (original: " + originalWidth + "x" + originalHeight + + ", scaled: " + scaledWidth + "x" + scaledHeight + ")"); + return thumbnailFile.getAbsolutePath(); + } catch (Exception e) { + Log.e(TAG, "Failed to save thumbnail", e); + return null; + } finally { + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + Log.e(TAG, "Failed to close FileOutputStream", e); + } + } + } + } + + /** + * 加载缩略图 + * @param path 缩略图文件路径 + * @return 缩略图位图,失败返回 null + */ + public Bitmap loadThumbnail(String path) { + if (path == null) { + return null; + } + + File thumbnailFile = new File(path); + if (!thumbnailFile.exists()) { + Log.w(TAG, "Thumbnail file not found: " + path); + return null; + } + + try { + return BitmapFactory.decodeFile(path); + } catch (Exception e) { + Log.e(TAG, "Failed to load thumbnail: " + path, e); + return null; + } + } + + /** + * 删除缩略图文件 + */ + private void deleteThumbnailFile(String path) { + if (path == null) { + return; + } + + File file = new File(path); + if (file.exists()) { + boolean deleted = file.delete(); + Log.d(TAG, "Deleted thumbnail file: " + path + ", success: " + deleted); + } + } + + /** + * 保存历史记录列表到 SharedPreferences + */ + private void saveHistoryList(List historyList) { + String historyJson = gson.toJson(historyList); + prefs.edit().putString(KEY_HISTORY, historyJson).apply(); + } + + /** + * 获取历史记录数量 + */ + public int getHistoryCount() { + return loadHistory().size(); + } +} diff --git a/app/src/main/res/drawable/ic_history.xml b/app/src/main/res/drawable/ic_history.xml new file mode 100644 index 0000000..9aef09d --- /dev/null +++ b/app/src/main/res/drawable/ic_history.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/fragment_wallpaper_history.xml b/app/src/main/res/layout/fragment_wallpaper_history.xml new file mode 100644 index 0000000..3aa873d --- /dev/null +++ b/app/src/main/res/layout/fragment_wallpaper_history.xml @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/item_history_card.xml b/app/src/main/res/layout/item_history_card.xml new file mode 100644 index 0000000..bc5498a --- /dev/null +++ b/app/src/main/res/layout/item_history_card.xml @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/menu/bottom_nav_menu.xml b/app/src/main/res/menu/bottom_nav_menu.xml index 1e4b33b..debad96 100644 --- a/app/src/main/res/menu/bottom_nav_menu.xml +++ b/app/src/main/res/menu/bottom_nav_menu.xml @@ -18,9 +18,15 @@ android:title="@string/nav_albums" android:orderInCategory="2" /> + + + android:orderInCategory="4" /> diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index de6dcf3..3111fe4 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -31,6 +31,7 @@ #FFFFFF #E0E0E0 + #E0E0E0 #FF6200EE diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1b44c61..c6b2067 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -20,5 +20,6 @@ 主页 壁纸 相册 + 历史 设置