优化当前壁纸页信息与交互

This commit is contained in:
hughhuang committed 2026-01-26 00:59:09 +08:00
1 parent 4347fa92b2
commit 19e433c402
2 files changed
+251 -131

No files matched your search

@@ -6,11 +6,11 @@ import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.provider.OpenableColumns;
import android.database.Cursor;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@@ -18,6 +18,9 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import com.google.android.material.button.MaterialButton;
import android.widget.ImageView;
import cn.hugh.bz.gk2.R; import cn.hugh.bz.gk2.R;
import cn.hugh.bz.gk2.utils.BackgroundHelper; import cn.hugh.bz.gk2.utils.BackgroundHelper;
import cn.hugh.bz.gk2.utils.ImageLoader; import cn.hugh.bz.gk2.utils.ImageLoader;
@@ -30,13 +33,16 @@ import cn.hugh.bz.gk2.wallpaper.WallpaperPreferences;
public class CurrentWallpaperFragment extends Fragment { public class CurrentWallpaperFragment extends Fragment {
private ImageView ivWallpaper; private ImageView ivWallpaper;
private TextView tvWallpaperPath; private View touchOverlay;
private TextView tvWallpaperSize; private TextView tvWallpaperName;
private Button btnNext; private TextView tvResolution;
private TextView tvFileSize;
private TextView tvPath;
private MaterialButton btnNext;
private MaterialButton btnOpenGallery;
private WallpaperPreferences prefs; private WallpaperPreferences prefs;
private ImageLoader imageLoader; private ImageLoader imageLoader;
private cn.hugh.bz.gk2.utils.ImageScanner scanner;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@@ -49,7 +55,6 @@ public class CurrentWallpaperFragment extends Fragment {
prefs = new WallpaperPreferences(requireContext()); prefs = new WallpaperPreferences(requireContext());
imageLoader = new ImageLoader(requireContext()); imageLoader = new ImageLoader(requireContext());
scanner = new cn.hugh.bz.gk2.utils.ImageScanner(requireContext());
initViews(view); initViews(view);
loadCurrentWallpaper(); loadCurrentWallpaper();
@@ -60,12 +65,17 @@ public class CurrentWallpaperFragment extends Fragment {
private void initViews(View view) { private void initViews(View view) {
ivWallpaper = view.findViewById(R.id.ivWallpaper); ivWallpaper = view.findViewById(R.id.ivWallpaper);
tvWallpaperPath = view.findViewById(R.id.tvWallpaperPath); touchOverlay = view.findViewById(R.id.touchOverlay);
tvWallpaperSize = view.findViewById(R.id.tvWallpaperSize); tvWallpaperName = view.findViewById(R.id.tvWallpaperName);
tvResolution = view.findViewById(R.id.tvResolution);
tvFileSize = view.findViewById(R.id.tvFileSize);
tvPath = view.findViewById(R.id.tvPath);
btnNext = view.findViewById(R.id.btnNext); btnNext = view.findViewById(R.id.btnNext);
btnOpenGallery = view.findViewById(R.id.btnOpenGallery);
btnNext.setOnClickListener(v -> showNextWallpaper()); btnNext.setOnClickListener(v -> showNextWallpaper());
ivWallpaper.setOnClickListener(v -> openInGallery()); btnOpenGallery.setOnClickListener(v -> openInGallery());
touchOverlay.setOnClickListener(v -> openInGallery());
} }
private void loadCurrentWallpaper() { private void loadCurrentWallpaper() {
@@ -77,17 +87,30 @@ public class CurrentWallpaperFragment extends Fragment {
} }
// 显示加载状态 // 显示加载状态
tvWallpaperPath.setText("正在加载壁纸..."); tvWallpaperName.setText("加载中...");
tvResolution.setText("-- x --");
tvFileSize.setText("-- MB");
tvPath.setText(currentUri);
btnNext.setEnabled(false);
// 在后台线程加载图片 // 获取文件大小和名称
new Thread(() -> { new Thread(() -> {
String fileSizeStr = getFileSize(Uri.parse(currentUri));
String displayName = getDisplayName(currentUri);
requireActivity().runOnUiThread(() -> {
tvFileSize.setText(fileSizeStr);
tvWallpaperName.setText(displayName);
});
// 加载图片
Bitmap bitmap = imageLoader.loadImage(currentUri, 1080, 1920); Bitmap bitmap = imageLoader.loadImage(currentUri, 1080, 1920);
requireActivity().runOnUiThread(() -> { requireActivity().runOnUiThread(() -> {
if (bitmap != null) { if (bitmap != null) {
ivWallpaper.setImageBitmap(bitmap); ivWallpaper.setImageBitmap(bitmap);
tvWallpaperPath.setText(getDisplayName(currentUri)); tvResolution.setText(bitmap.getWidth() + " x " + bitmap.getHeight());
tvWallpaperSize.setText(bitmap.getWidth() + " x " + bitmap.getHeight()); btnNext.setEnabled(true);
} else { } else {
showError(); showError();
} }
@@ -95,55 +118,74 @@ public class CurrentWallpaperFragment extends Fragment {
}).start(); }).start();
} }
private String getFileSize(Uri uri) {
try (Cursor cursor = requireContext().getContentResolver().query(uri, null, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
if (sizeIndex != -1) {
long size = cursor.getLong(sizeIndex);
return formatFileSize(size);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return "未知大小";
}
private String formatFileSize(long size) {
if (size <= 0) return "0 B";
final String[] units = new String[]{"B", "KB", "MB", "GB", "TB"};
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
return String.format("%.1f %s", size / Math.pow(1024, digitGroups), units[digitGroups]);
}
private void showNoWallpaper() { private void showNoWallpaper() {
ivWallpaper.setImageDrawable(null); ivWallpaper.setImageDrawable(null);
tvWallpaperPath.setText("尚未设置壁纸"); tvWallpaperName.setText("未设置壁纸");
tvWallpaperSize.setText(""); tvResolution.setText("--");
tvFileSize.setText("--");
tvPath.setText("");
btnNext.setEnabled(false); btnNext.setEnabled(false);
btnOpenGallery.setEnabled(false);
} }
private void showError() { private void showError() {
ivWallpaper.setImageDrawable(null); ivWallpaper.setImageDrawable(null);
tvWallpaperPath.setText("加载壁纸失败"); tvWallpaperName.setText("加载失败");
tvWallpaperSize.setText(""); btnNext.setEnabled(true); // 允许尝试下一张
btnNext.setEnabled(false);
} }
private void showNextWallpaper() { private void showNextWallpaper() {
android.util.Log.i("CurrentWallpaperFragment", "showNextWallpaper() called"); android.util.Log.i("CurrentWallpaperFragment", "showNextWallpaper() called");
cn.hugh.bz.gk2.utils.FileLogger.getInstance(requireContext()).log("CurrentWallpaperFragment", "showNextWallpaper() called");
cn.hugh.bz.gk2.model.AppConfig config = prefs.loadConfig(); cn.hugh.bz.gk2.model.AppConfig config = prefs.loadConfig();
android.util.Log.i("CurrentWallpaperFragment", "showNextWallpaper: Selected folders count: " + config.getSelectedFolders().size());
cn.hugh.bz.gk2.utils.FileLogger.getInstance(requireContext()).log("CurrentWallpaperFragment", "showNextWallpaper: Selected folders count: " + config.getSelectedFolders().size());
android.util.Log.i("CurrentWallpaperFragment", "showNextWallpaper: Image URIs count: " + (config.getImageUris() != null ? config.getImageUris().size() : 0));
cn.hugh.bz.gk2.utils.FileLogger.getInstance(requireContext()).log("CurrentWallpaperFragment", "showNextWallpaper: Image URIs count: " + (config.getImageUris() != null ? config.getImageUris().size() : 0));
if (config.getSelectedFolders().isEmpty() && if (config.getSelectedFolders().isEmpty() &&
(config.getImageUris() == null || config.getImageUris().isEmpty())) { (config.getImageUris() == null || config.getImageUris().isEmpty())) {
android.util.Log.w("CurrentWallpaperFragment", "showNextWallpaper: No folders or images selected, showing toast"); Toast.makeText(requireContext(), "请先在相册页面添加图片", Toast.LENGTH_SHORT).show();
Toast.makeText(requireContext(), "请先选择文件夹或图片", Toast.LENGTH_SHORT).show();
return; return;
} }
android.util.Log.i("CurrentWallpaperFragment", "showNextWallpaper: sending broadcast"); // 禁用按钮防止重复点击
cn.hugh.bz.gk2.utils.FileLogger.getInstance(requireContext()).log("CurrentWallpaperFragment", "showNextWallpaper: sending broadcast"); btnNext.setEnabled(false);
btnNext.setText("切换中...");
// 直接实例化 BroadcastReceiver 并调用 onReceive(避免隐式广播限制) // 发送广播
cn.hugh.bz.gk2.receiver.WallpaperChangeReceiver receiver = new cn.hugh.bz.gk2.receiver.WallpaperChangeReceiver(); cn.hugh.bz.gk2.receiver.WallpaperChangeReceiver receiver = new cn.hugh.bz.gk2.receiver.WallpaperChangeReceiver();
Intent intent = new Intent("cn.hugh.bz.gk2.ACTION_CHANGE_WALLPAPER"); Intent intent = new Intent("cn.hugh.bz.gk2.ACTION_CHANGE_WALLPAPER");
intent.putExtra("force_change", true); // 用户主动触发 intent.putExtra("force_change", true);
intent.putExtra("scheduled", false); // 非定时触发 intent.putExtra("scheduled", false);
receiver.onReceive(requireContext(), intent); receiver.onReceive(requireContext(), intent);
Toast.makeText(requireContext(), "已切换到下一张", Toast.LENGTH_SHORT).show(); Toast.makeText(requireContext(), "已切换", Toast.LENGTH_SHORT).show();
// 延迟刷新预览 // 延迟刷新
new Handler(Looper.getMainLooper()).postDelayed(() -> { new Handler(Looper.getMainLooper()).postDelayed(() -> {
loadCurrentWallpaper(); loadCurrentWallpaper();
}, 500); btnNext.setText("切换下一张");
btnNext.setEnabled(true);
}, 800);
} }
private void openInGallery() { private void openInGallery() {
@@ -155,28 +197,38 @@ public class CurrentWallpaperFragment extends Fragment {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent); startActivity(intent);
} catch (Exception e) { } catch (Exception e) {
Toast.makeText(requireContext(), "无法打开相册", Toast.LENGTH_SHORT).show(); Toast.makeText(requireContext(), "无法打开图片", Toast.LENGTH_SHORT).show();
} }
} }
} }
private String getDisplayName(String uriString) { private String getDisplayName(String uriString) {
if (uriString == null || uriString.isEmpty()) { if (uriString == null || uriString.isEmpty()) {
return "未知壁纸"; return "未知图片";
} }
try { try {
Uri uri = Uri.parse(uriString); Uri uri = Uri.parse(uriString);
// 尝试从 ContentResolver 获取文件名
try (Cursor cursor = requireContext().getContentResolver().query(uri, null, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
if (nameIndex != -1) {
return cursor.getString(nameIndex);
}
}
}
// 如果失败,回退到路径解析
String path = uri.getLastPathSegment(); String path = uri.getLastPathSegment();
if (path != null) { if (path != null) {
// 处理 content://com.android.externalstorage.documents/tree/primary%3ADCIM 这种格式
if (path.contains(":")) { if (path.contains(":")) {
String[] parts = path.split(":"); String[] parts = path.split(":");
if (parts.length > 1) { if (parts.length > 1) {
path = parts[1]; path = parts[1];
} }
} }
// 移除文件扩展名
if (path.contains("/")) { if (path.contains("/")) {
String[] segments = path.split("/"); String[] segments = path.split("/");
path = segments[segments.length - 1]; path = segments[segments.length - 1];
@@ -187,7 +239,7 @@ public class CurrentWallpaperFragment extends Fragment {
// Ignore // Ignore
} }
return "当前壁纸"; return "未命名图片";
} }
@Override @Override
@@ -195,19 +247,9 @@ public class CurrentWallpaperFragment extends Fragment {
super.onResume(); super.onResume();
loadCurrentWallpaper(); loadCurrentWallpaper();
// 重新应用背景图片
View rootView = getView(); View rootView = getView();
if (rootView != null) { if (rootView != null) {
BackgroundHelper.applyBackground(requireContext(), rootView); BackgroundHelper.applyBackground(requireContext(), rootView);
} }
} }
@Override
public void onDestroyView() {
super.onDestroyView();
// 释放图片资源
if (ivWallpaper != null) {
ivWallpaper.setImageDrawable(null);
}
}
} }
@@ -1,105 +1,183 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView <androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="@color/bg_primary" android:background="@color/bg_primary_modern">
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout <!-- 顶部标题 -->
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="当前壁纸"
android:textSize="@dimen/text_size_xl"
android:textStyle="bold"
android:textColor="@color/text_primary_modern"
android:layout_marginTop="@dimen/spacing_lg"
android:layout_marginStart="@dimen/spacing_lg"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<!-- 壁纸预览卡片 (占据主要空间) -->
<com.google.android.material.card.MaterialCardView
android:id="@+id/cardWallpaper"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginHorizontal="@dimen/spacing_lg"
android:layout_marginTop="@dimen/spacing_md"
android:layout_marginBottom="@dimen/spacing_lg"
app:cardElevation="0dp"
app:cardCornerRadius="@dimen/corner_radius_lg"
app:cardBackgroundColor="@color/bg_secondary_modern"
app:strokeColor="@color/bg_secondary_modern"
app:strokeWidth="1dp"
app:layout_constraintTop_toBottomOf="@id/tvTitle"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@id/infoContainer">
<ImageView
android:id="@+id/ivWallpaper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:contentDescription="当前壁纸预览" />
<!-- 点击提示遮罩 -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:id="@+id/touchOverlay" />
</com.google.android.material.card.MaterialCardView>
<!-- 底部信息和操作区 -->
<LinearLayout
android:id="@+id/infoContainer"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:padding="@dimen/spacing_md" android:orientation="vertical"
tools:context=".ui.CurrentWallpaperFragment"> android:paddingHorizontal="@dimen/spacing_lg"
android:paddingBottom="100dp"
app:layout_constraintBottom_toBottomOf="parent">
<!-- 壁纸预览卡片 --> <!-- 壁纸信息 -->
<com.google.android.material.card.MaterialCardView <LinearLayout
android:id="@+id/cardWallpaper" android:layout_width="match_parent"
android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:cardElevation="@dimen/card_elevation" android:orientation="vertical"
app:cardCornerRadius="@dimen/corner_radius_lg" android:layout_marginBottom="@dimen/spacing_lg">
app:cardBackgroundColor="@color/bg_card"
app:strokeColor="@color/glass_border"
app:strokeWidth="@dimen/glass_border_width"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout <TextView
android:id="@+id/tvWallpaperName"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content"
android:text="Loading..."
android:textSize="@dimen/text_size_lg"
android:textStyle="bold"
android:textColor="@color/text_primary_modern"
android:maxLines="1"
android:ellipsize="end" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="@dimen/spacing_xs"
android:gravity="center_vertical">
<!-- 壁纸图片 -->
<ImageView <ImageView
android:id="@+id/ivWallpaper" android:layout_width="@dimen/icon_size_xs"
android:layout_width="0dp" android:layout_height="@dimen/icon_size_xs"
android:layout_height="0dp" android:src="@drawable/ic_image"
android:scaleType="centerCrop" app:tint="@color/text_secondary_modern" />
android:background="@color/bg_secondary"
android:adjustViewBounds="true"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintDimensionRatio="9:16"
android:contentDescription="当前壁纸预览" />
<!-- 渐变遮罩信息层 --> <TextView
<LinearLayout android:id="@+id/tvResolution"
android:layout_width="0dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:text="1080 x 1920"
android:background="@drawable/bg_gradient_overlay" android:textSize="@dimen/text_size_sm"
android:padding="@dimen/spacing_md" android:textColor="@color/text_secondary_modern"
app:layout_constraintBottom_toBottomOf="@id/ivWallpaper" android:layout_marginStart="4dp" />
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<TextView <View
android:id="@+id/tvWallpaperPath" android:layout_width="1dp"
android:layout_width="match_parent" android:layout_height="12dp"
android:layout_height="wrap_content" android:background="@color/text_hint_modern"
android:text="当前壁纸路径" android:layout_marginHorizontal="8dp" />
android:textSize="@dimen/text_size_sm"
android:textStyle="bold"
android:textColor="@color/text_primary"
android:maxLines="2"
android:ellipsize="middle" />
<TextView <TextView
android:id="@+id/tvWallpaperSize" android:id="@+id/tvFileSize"
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="尺寸" android:text="2.5 MB"
android:textSize="@dimen/text_size_xs" android:textSize="@dimen/text_size_sm"
android:textColor="@color/text_secondary" android:textColor="@color/text_secondary_modern" />
android:layout_marginTop="@dimen/spacing_xs" />
</LinearLayout> </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> <TextView
android:id="@+id/tvPath"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="/storage/emulated/0/..."
android:textSize="@dimen/text_size_xs"
android:textColor="@color/text_hint_modern"
android:layout_marginTop="@dimen/spacing_sm"
android:maxLines="1"
android:ellipsize="middle"
android:background="@drawable/badge_background"
android:backgroundTint="@color/bg_secondary_modern"
android:paddingHorizontal="8dp"
android:paddingVertical="4dp" />
</com.google.android.material.card.MaterialCardView> </LinearLayout>
<!-- 操作按钮 --> <!-- 操作按钮行 -->
<com.google.android.material.button.MaterialButton <LinearLayout
android:id="@+id/btnNext" android:layout_width="match_parent"
android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_height="@dimen/button_height" android:orientation="horizontal">
android:layout_marginTop="@dimen/spacing_lg"
android:text="下一张"
android:textSize="@dimen/text_size_md"
android:backgroundTint="@color/accent_primary"
app:icon="@drawable/ic_arrow_forward"
app:iconGravity="textEnd"
app:cornerRadius="@dimen/button_corner_radius"
style="@style/Widget.Material3.Button"
app:layout_constraintTop_toBottomOf="@id/cardWallpaper"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> <com.google.android.material.button.MaterialButton
android:id="@+id/btnOpenGallery"
android:layout_width="0dp"
android:layout_height="56dp"
android:layout_weight="1"
android:text="查看原图"
android:textSize="@dimen/text_size_md"
android:textColor="@color/text_primary_modern"
app:icon="@drawable/ic_image"
app:iconTint="@color/text_primary_modern"
app:backgroundTint="@color/bg_secondary_modern"
app:cornerRadius="@dimen/corner_radius_pill"
style="@style/Widget.Material3.Button.Icon"
android:layout_marginEnd="@dimen/spacing_sm" />
</androidx.core.widget.NestedScrollView> <com.google.android.material.button.MaterialButton
android:id="@+id/btnNext"
android:layout_width="0dp"
android:layout_height="56dp"
android:layout_weight="1.5"
android:text="切换下一张"
android:textSize="@dimen/text_size_md"
android:textStyle="bold"
app:icon="@drawable/ic_arrow_forward"
app:iconGravity="textEnd"
app:backgroundTint="@color/sky_blue_primary"
app:cornerRadius="@dimen/corner_radius_pill"
style="@style/Widget.Material3.Button.Icon"
android:layout_marginStart="@dimen/spacing_sm" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>