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

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

No files matched your search

@@ -6,11 +6,11 @@ import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.provider.OpenableColumns;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
@@ -18,6 +18,9 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
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.utils.BackgroundHelper;
import cn.hugh.bz.gk2.utils.ImageLoader;
@@ -30,13 +33,16 @@ import cn.hugh.bz.gk2.wallpaper.WallpaperPreferences;
public class CurrentWallpaperFragment extends Fragment {
private ImageView ivWallpaper;
private TextView tvWallpaperPath;
private TextView tvWallpaperSize;
private Button btnNext;
private View touchOverlay;
private TextView tvWallpaperName;
private TextView tvResolution;
private TextView tvFileSize;
private TextView tvPath;
private MaterialButton btnNext;
private MaterialButton btnOpenGallery;
private WallpaperPreferences prefs;
private ImageLoader imageLoader;
private cn.hugh.bz.gk2.utils.ImageScanner scanner;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@@ -49,7 +55,6 @@ public class CurrentWallpaperFragment extends Fragment {
prefs = new WallpaperPreferences(requireContext());
imageLoader = new ImageLoader(requireContext());
scanner = new cn.hugh.bz.gk2.utils.ImageScanner(requireContext());
initViews(view);
loadCurrentWallpaper();
@@ -60,12 +65,17 @@ public class CurrentWallpaperFragment extends Fragment {
private void initViews(View view) {
ivWallpaper = view.findViewById(R.id.ivWallpaper);
tvWallpaperPath = view.findViewById(R.id.tvWallpaperPath);
tvWallpaperSize = view.findViewById(R.id.tvWallpaperSize);
touchOverlay = view.findViewById(R.id.touchOverlay);
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);
btnOpenGallery = view.findViewById(R.id.btnOpenGallery);
btnNext.setOnClickListener(v -> showNextWallpaper());
ivWallpaper.setOnClickListener(v -> openInGallery());
btnOpenGallery.setOnClickListener(v -> openInGallery());
touchOverlay.setOnClickListener(v -> openInGallery());
}
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(() -> {
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);
requireActivity().runOnUiThread(() -> {
if (bitmap != null) {
ivWallpaper.setImageBitmap(bitmap);
tvWallpaperPath.setText(getDisplayName(currentUri));
tvWallpaperSize.setText(bitmap.getWidth() + " x " + bitmap.getHeight());
tvResolution.setText(bitmap.getWidth() + " x " + bitmap.getHeight());
btnNext.setEnabled(true);
} else {
showError();
}
@@ -95,55 +118,74 @@ public class CurrentWallpaperFragment extends Fragment {
}).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() {
ivWallpaper.setImageDrawable(null);
tvWallpaperPath.setText("尚未设置壁纸");
tvWallpaperSize.setText("");
tvWallpaperName.setText("未设置壁纸");
tvResolution.setText("--");
tvFileSize.setText("--");
tvPath.setText("");
btnNext.setEnabled(false);
btnOpenGallery.setEnabled(false);
}
private void showError() {
ivWallpaper.setImageDrawable(null);
tvWallpaperPath.setText("加载壁纸失败");
tvWallpaperSize.setText("");
btnNext.setEnabled(false);
tvWallpaperName.setText("加载失败");
btnNext.setEnabled(true); // 允许尝试下一张
}
private void showNextWallpaper() {
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();
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() &&
(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;
}
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();
Intent intent = new Intent("cn.hugh.bz.gk2.ACTION_CHANGE_WALLPAPER");
intent.putExtra("force_change", true); // 用户主动触发
intent.putExtra("scheduled", false); // 非定时触发
intent.putExtra("force_change", true);
intent.putExtra("scheduled", false);
receiver.onReceive(requireContext(), intent);
Toast.makeText(requireContext(), "已切换到下一张", Toast.LENGTH_SHORT).show();
Toast.makeText(requireContext(), "已切换", Toast.LENGTH_SHORT).show();
// 延迟刷新预览
// 延迟刷新
new Handler(Looper.getMainLooper()).postDelayed(() -> {
loadCurrentWallpaper();
}, 500);
btnNext.setText("切换下一张");
btnNext.setEnabled(true);
}, 800);
}
private void openInGallery() {
@@ -155,28 +197,38 @@ public class CurrentWallpaperFragment extends Fragment {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
} catch (Exception e) {
Toast.makeText(requireContext(), "无法打开相册", Toast.LENGTH_SHORT).show();
Toast.makeText(requireContext(), "无法打开图片", Toast.LENGTH_SHORT).show();
}
}
}
private String getDisplayName(String uriString) {
if (uriString == null || uriString.isEmpty()) {
return "未知壁纸";
return "未知图片";
}
try {
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();
if (path != null) {
// 处理 content://com.android.externalstorage.documents/tree/primary%3ADCIM 这种格式
if (path.contains(":")) {
String[] parts = path.split(":");
if (parts.length > 1) {
path = parts[1];
}
}
// 移除文件扩展名
if (path.contains("/")) {
String[] segments = path.split("/");
path = segments[segments.length - 1];
@@ -187,7 +239,7 @@ public class CurrentWallpaperFragment extends Fragment {
// Ignore
}
return "当前壁纸";
return "未命名图片";
}
@Override
@@ -195,19 +247,9 @@ public class CurrentWallpaperFragment extends Fragment {
super.onResume();
loadCurrentWallpaper();
// 重新应用背景图片
View rootView = getView();
if (rootView != null) {
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"?>
<androidx.core.widget.NestedScrollView
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_primary"
android:fillViewport="true">
android:background="@color/bg_primary_modern">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
<!-- 顶部标题 -->
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/spacing_md"
tools:context=".ui.CurrentWallpaperFragment">
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="wrap_content"
app:cardElevation="@dimen/card_elevation"
app:cardCornerRadius="@dimen/corner_radius_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
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 壁纸图片 -->
<ImageView
android:id="@+id/ivWallpaper"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:background="@color/bg_secondary"
android:adjustViewBounds="true"
app:layout_constraintTop_toTopOf="parent"
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_constraintDimensionRatio="9:16"
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="当前壁纸预览" />
<!-- 渐变遮罩信息层 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/bg_gradient_overlay"
android:padding="@dimen/spacing_md"
app:layout_constraintBottom_toBottomOf="@id/ivWallpaper"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<TextView
android:id="@+id/tvWallpaperPath"
<!-- 点击提示遮罩 -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="当前壁纸路径"
android:textSize="@dimen/text_size_sm"
android:textStyle="bold"
android:textColor="@color/text_primary"
android:maxLines="2"
android:ellipsize="middle" />
<TextView
android:id="@+id/tvWallpaperSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="尺寸"
android:textSize="@dimen/text_size_xs"
android:textColor="@color/text_secondary"
android:layout_marginTop="@dimen/spacing_xs" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
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_height="wrap_content"
android:orientation="vertical"
android:paddingHorizontal="@dimen/spacing_lg"
android:paddingBottom="100dp"
app:layout_constraintBottom_toBottomOf="parent">
<!-- 壁纸信息 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="@dimen/spacing_lg">
<TextView
android:id="@+id/tvWallpaperName"
android:layout_width="match_parent"
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
android:layout_width="@dimen/icon_size_xs"
android:layout_height="@dimen/icon_size_xs"
android:src="@drawable/ic_image"
app:tint="@color/text_secondary_modern" />
<TextView
android:id="@+id/tvResolution"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1080 x 1920"
android:textSize="@dimen/text_size_sm"
android:textColor="@color/text_secondary_modern"
android:layout_marginStart="4dp" />
<View
android:layout_width="1dp"
android:layout_height="12dp"
android:background="@color/text_hint_modern"
android:layout_marginHorizontal="8dp" />
<TextView
android:id="@+id/tvFileSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2.5 MB"
android:textSize="@dimen/text_size_sm"
android:textColor="@color/text_secondary_modern" />
</LinearLayout>
<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" />
</LinearLayout>
<!-- 操作按钮行 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<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" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btnNext"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_marginTop="@dimen/spacing_lg"
android:text="下一张"
android:layout_height="56dp"
android:layout_weight="1.5"
android:text="切换下一张"
android:textSize="@dimen/text_size_md"
android:backgroundTint="@color/accent_primary"
android:textStyle="bold"
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" />
app:backgroundTint="@color/sky_blue_primary"
app:cornerRadius="@dimen/corner_radius_pill"
style="@style/Widget.Material3.Button.Icon"
android:layout_marginStart="@dimen/spacing_sm" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>