diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..76fde3e --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +FindMaimaiUltra \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml index b589d56..b86273d 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index f2a715a..d403a80 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -4,7 +4,7 @@ - + diff --git a/app/src/main/java/org/astral/findmaimaiultra/adapter/PlaceAdapter.java b/app/src/main/java/org/astral/findmaimaiultra/adapter/PlaceAdapter.java index c07be33..5c89106 100644 --- a/app/src/main/java/org/astral/findmaimaiultra/adapter/PlaceAdapter.java +++ b/app/src/main/java/org/astral/findmaimaiultra/adapter/PlaceAdapter.java @@ -18,7 +18,7 @@ import org.astral.findmaimaiultra.been.Place; import java.util.List; public class PlaceAdapter extends RecyclerView.Adapter { - + private String theme; private List placeList; private OnItemClickListener listener; @@ -26,9 +26,10 @@ public class PlaceAdapter extends RecyclerView.Adapter placeList, OnItemClickListener listener) { + public PlaceAdapter(List placeList,String theme, OnItemClickListener listener) { this.placeList = placeList; this.listener = listener; + this.theme = theme; } @NonNull @@ -69,6 +70,40 @@ public class PlaceAdapter extends RecyclerView.Adapter showBaiduMapDialog()); + + LinearLayout background = findViewById(R.id.background); + + + if (preferences.getString("image_uri", null) != null ) { + try { + File backgroundFile = FileUtils.getBackground(this, "background.jpg"); + + if (!backgroundFile.exists()) { + Toast.makeText(this, "文件不存在,请先设置背景图片", Toast.LENGTH_SHORT).show(); + } + + Bitmap bitmap = BitmapFactory.decodeFile(backgroundFile.getAbsolutePath()); + + if (bitmap != null) { + // 获取RecyclerView的尺寸 + int recyclerViewWidth = 0; + int recyclerViewHeight = 0; + recyclerViewWidth = background.getWidth(); + recyclerViewHeight = background.getHeight(); + if (recyclerViewWidth > 0 && recyclerViewHeight > 0) { + // 计算缩放比例 + float scaleWidth = ((float) recyclerViewWidth) / bitmap.getWidth(); + float scaleHeight = ((float) recyclerViewHeight) / bitmap.getHeight(); + + // 选择较大的缩放比例以保持图片的原始比例 + float scaleFactor = Math.max(scaleWidth, scaleHeight); + + // 计算新的宽度和高度 + int newWidth = (int) (bitmap.getWidth() * scaleFactor); + int newHeight = (int) (bitmap.getHeight() * scaleFactor); + + // 缩放图片 + Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true); + + // 计算裁剪区域 + int xT = (scaledBitmap.getWidth() - recyclerViewWidth) / 2; + int yT = (scaledBitmap.getHeight() - recyclerViewHeight) / 2; + + // 处理x和y为负数的情况 + xT = Math.max(xT, 0); + yT = Math.max(yT, 0); + + // 裁剪图片 + Bitmap croppedBitmap = Bitmap.createBitmap(scaledBitmap, xT, yT, recyclerViewWidth, recyclerViewHeight); + + // 创建一个新的 Bitmap,与裁剪后的 Bitmap 大小相同 + Bitmap transparentBitmap = Bitmap.createBitmap(croppedBitmap.getWidth(), croppedBitmap.getHeight(), croppedBitmap.getConfig()); + + // 创建一个 Canvas 对象,用于在新的 Bitmap 上绘制 + Canvas canvas = new Canvas(transparentBitmap); + + // 创建一个 Paint 对象,并设置透明度 + Paint paint = new Paint(); + paint.setAlpha(128); // 设置透明度为 50% (255 * 0.5 = 128) + + // 将裁剪后的 Bitmap 绘制到新的 Bitmap 上,并应用透明度 + canvas.drawBitmap(croppedBitmap, 0, 0, paint); + + // 创建BitmapDrawable并设置其边界为RecyclerView的尺寸 + BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), transparentBitmap); + + // 设置recyclerView的背景 + background.setBackground(bitmapDrawable); + + } else { + // 如果RecyclerView的尺寸未确定,可以使用ViewTreeObserver来监听尺寸变化 + background.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { + @Override + public void onGlobalLayout() { + background.getViewTreeObserver().removeOnGlobalLayoutListener(this); + int recyclerViewWidth = 0; + int recyclerViewHeight = 0; + recyclerViewWidth = background.getWidth(); + recyclerViewHeight = background.getHeight(); + + // 计算缩放比例 + float scaleWidth = ((float) recyclerViewWidth) / bitmap.getWidth(); + float scaleHeight = ((float) recyclerViewHeight) / bitmap.getHeight(); + + // 选择较大的缩放比例以保持图片的原始比例 + float scaleFactor = Math.max(scaleWidth, scaleHeight); + + // 计算新的宽度和高度 + int newWidth = (int) (bitmap.getWidth() * scaleFactor); + int newHeight = (int) (bitmap.getHeight() * scaleFactor); + + // 缩放图片 + Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true); + + // 计算裁剪区域 + int x = (scaledBitmap.getWidth() - recyclerViewWidth) / 2; + int y = (scaledBitmap.getHeight() - recyclerViewHeight) / 2; + + // 处理x和y为负数的情况 + x = Math.max(x, 0); + y = Math.max(y, 0); + + // 裁剪图片 + Bitmap croppedBitmap = Bitmap.createBitmap(scaledBitmap, x, y, recyclerViewWidth, recyclerViewHeight); + + // 创建一个新的 Bitmap,与裁剪后的 Bitmap 大小相同 + Bitmap transparentBitmap = Bitmap.createBitmap(croppedBitmap.getWidth(), croppedBitmap.getHeight(), croppedBitmap.getConfig()); + + // 创建一个 Canvas 对象,用于在新的 Bitmap 上绘制 + Canvas canvas = new Canvas(transparentBitmap); + + // 创建一个 Paint 对象,并设置透明度 + Paint paint = new Paint(); + paint.setAlpha(128); // 设置透明度为 50% (255 * 0.5 = 128) + + // 将裁剪后的 Bitmap 绘制到新的 Bitmap 上,并应用透明度 + canvas.drawBitmap(croppedBitmap, 0, 0, paint); + + // 创建BitmapDrawable并设置其边界为RecyclerView的尺寸 + BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), transparentBitmap); + background.setBackground(bitmapDrawable); + + } + }); + } + } + + } catch (Exception e) { + e.printStackTrace(); + + Toast.makeText(this, "图片加载失败,权限出错!", Toast.LENGTH_SHORT).show(); + } + } } private void showBaiduMapDialog() { // 加载弹窗布局 @@ -477,6 +648,9 @@ public class PageActivity extends AppCompatActivity { .setTitle("选择新位置") .create(); mapDialog.show(); + + + } private void updateMapLocation() { mapView.onDestroy(); diff --git a/app/src/main/java/org/astral/findmaimaiultra/ui/home/HomeFragment.java b/app/src/main/java/org/astral/findmaimaiultra/ui/home/HomeFragment.java index 172f1c4..85a299b 100644 --- a/app/src/main/java/org/astral/findmaimaiultra/ui/home/HomeFragment.java +++ b/app/src/main/java/org/astral/findmaimaiultra/ui/home/HomeFragment.java @@ -85,7 +85,7 @@ public class HomeFragment extends Fragment { private SharedPreferences shoucang; private SharedPreferences settingProperties; private SharedPreferences settingProperties2; - + private String selectedTheme; private FragmentHomeBinding binding; private SharedViewModel sharedViewModel; @Override @@ -120,7 +120,7 @@ public class HomeFragment extends Fragment { List placeList = new ArrayList<>(); recyclerView.setAdapter(adapter); requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},0x123); - + selectedTheme = settingProperties2.getString("selected_theme", "Theme.FindMaimaiUltra"); // 示例:读取 SharedPreferences 中的数据 if (shoucang != null) { String savedData = shoucang.getString("key_name", "default_value"); @@ -372,7 +372,7 @@ public class HomeFragment extends Fragment { getActivity().runOnUiThread(new Runnable() { @Override public void run() { - adapter = new PlaceAdapter(a, new PlaceAdapter.OnItemClickListener() { + adapter = new PlaceAdapter(a,selectedTheme, new PlaceAdapter.OnItemClickListener() { @Override public void onItemClick(Place place) { Intent intent = new Intent(context, PageActivity.class); diff --git a/app/src/main/java/org/astral/findmaimaiultra/ui/slideshow/SlideshowFragment.java b/app/src/main/java/org/astral/findmaimaiultra/ui/slideshow/SlideshowFragment.java index ef56097..9fb5c2a 100644 --- a/app/src/main/java/org/astral/findmaimaiultra/ui/slideshow/SlideshowFragment.java +++ b/app/src/main/java/org/astral/findmaimaiultra/ui/slideshow/SlideshowFragment.java @@ -2,6 +2,7 @@ package org.astral.findmaimaiultra.ui.slideshow; import android.Manifest; import android.annotation.SuppressLint; +import android.app.UiModeManager; import android.content.*; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; @@ -14,9 +15,11 @@ import android.view.ViewGroup; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ImageView; +import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; +import androidx.cardview.widget.CardView; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; @@ -24,8 +27,11 @@ import androidx.lifecycle.ViewModelProvider; import com.bumptech.glide.Glide; import com.google.android.material.button.MaterialButton; import com.google.android.material.radiobutton.MaterialRadioButton; +import com.google.android.material.snackbar.Snackbar; import com.google.android.material.switchmaterial.SwitchMaterial; import com.google.android.material.textfield.TextInputEditText; + +import org.astral.findmaimaiultra.R; import org.astral.findmaimaiultra.been.Release; import org.astral.findmaimaiultra.databinding.FragmentSlideshowBinding; import org.astral.findmaimaiultra.service.GitHubApiService; @@ -54,7 +60,7 @@ public class SlideshowFragment extends Fragment { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE }; - + private String theme; @Override public void onAttach(@NonNull Context context) { super.onAttach(context); @@ -76,6 +82,7 @@ public class SlideshowFragment extends Fragment { } else { requestPermissions(); } + theme = settingProperties.getString("selected_theme", "Theme.FindMaimaiUltra"); } private void show(String text) { @@ -101,6 +108,7 @@ public class SlideshowFragment extends Fragment { ); } + @SuppressLint("ResourceAsColor") @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -178,9 +186,83 @@ public class SlideshowFragment extends Fragment { .into(user_avatar); TextView user_name = binding.username; user_name.setText(username); + + themeClick(root); + + + if (theme.contains("Pink")) { + // 全部设置颜色 + user_name.setTextColor(ContextCompat.getColor(requireContext(), R.color.colorPrimary)); + binding.desc.setTextColor(ContextCompat.getColor(requireContext(), R.color.colorPrimary)); + binding.vits.setTextColor(ContextCompat.getColor(requireContext(), R.color.colorPrimary)); + binding.themeText.setTextColor(ContextCompat.getColor(requireContext(), R.color.colorPrimary)); + } else if (theme.contains("Blue")) { + user_name.setTextColor(ContextCompat.getColor(requireContext(), R.color.textcolorPrimary2)); + binding.desc.setTextColor(ContextCompat.getColor(requireContext(), R.color.textcolorPrimary2)); + binding.vits.setTextColor(ContextCompat.getColor(requireContext(), R.color.textcolorPrimary2)); + binding.themeText.setTextColor(ContextCompat.getColor(requireContext(), R.color.textcolorPrimary2)); + } else if (theme.contains("Green")) { + user_name.setTextColor(ContextCompat.getColor(requireContext(), R.color.lineBaseGreen)); + binding.desc.setTextColor(ContextCompat.getColor(requireContext(), R.color.lineBaseGreen)); + binding.vits.setTextColor(ContextCompat.getColor(requireContext(), R.color.lineBaseGreen)); + binding.themeText.setTextColor(ContextCompat.getColor(requireContext(), R.color.lineBaseGreen)); + }else if (theme.contains("White")) { + user_name.setTextColor(ContextCompat.getColor(requireContext(), R.color.white)); + binding.desc.setTextColor(ContextCompat.getColor(requireContext(), R.color.white)); + binding.vits.setTextColor(ContextCompat.getColor(requireContext(), R.color.white)); + binding.themeText.setTextColor(ContextCompat.getColor(requireContext(), R.color.white)); + }else if (theme.contains("Gray")) { + user_name.setTextColor(ContextCompat.getColor(requireContext(), R.color.black)); + binding.desc.setTextColor(ContextCompat.getColor(requireContext(), R.color.black)); + binding.vits.setTextColor(ContextCompat.getColor(requireContext(), R.color.black)); + binding.themeText.setTextColor(ContextCompat.getColor(requireContext(), R.color.black)); + } + + binding.view2.setBackgroundColor(R.color.black); + binding.view3.setBackgroundColor(R.color.black); return root; } + + public boolean isDarkMode(Context context) { + UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE); + return uiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_YES; + } + private void themeClick(View root) { + CardView cardPink = root.findViewById(R.id.cardPink); + CardView cardGreen = root.findViewById(R.id.cardGreen); + CardView cardBlue = root.findViewById(R.id.cardBlue); + CardView cardWhite = root.findViewById(R.id.cardWhite); + CardView cardGray = root.findViewById(R.id.cardGray); + + View.OnClickListener themeClickListener = v -> { + String selectedTheme = "Theme.FindMaimaiUltra"; + int id = v.getId(); + if (id == R.id.cardPink) { + selectedTheme = "Theme.FindMaimaiUltra.Pink"; + } else if (id == R.id.cardGreen) { + selectedTheme = "Theme.FindMaimaiUltra.Green"; + } else if (id == R.id.cardBlue) { + selectedTheme = "Theme.FindMaimaiUltra.Blue"; + } else if (id == R.id.cardWhite) { + selectedTheme = "Theme.FindMaimaiUltra.White"; + } else if (id == R.id.cardGray) { + selectedTheme = "Theme.FindMaimaiUltra.Gray"; + } + SharedPreferences.Editor editor = settingProperties.edit(); + editor.putString("selected_theme", selectedTheme); + editor.apply(); + Snackbar .make(binding.getRoot(), "文本主题已更改", Snackbar.LENGTH_SHORT).show(); + + }; + + cardPink.setOnClickListener(themeClickListener); + cardGreen.setOnClickListener(themeClickListener); + cardBlue.setOnClickListener(themeClickListener); + cardWhite.setOnClickListener(themeClickListener); + cardGray.setOnClickListener(themeClickListener); + } + private String getAppVersionName() { try { PackageInfo packageInfo = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(), 0); diff --git a/app/src/main/res/layout/activity_page.xml b/app/src/main/res/layout/activity_page.xml index 46789e7..181cfee 100644 --- a/app/src/main/res/layout/activity_page.xml +++ b/app/src/main/res/layout/activity_page.xml @@ -8,7 +8,7 @@ @@ -16,7 +16,7 @@ + + + + + + + + + + + + + + + + + - #1DF687 #C2F6C4 #D5C4ED + + #2196F3 + #2196F3 + #000000 #FFFFFF diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index eb8488e..0da8c8b 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -22,7 +22,6 @@ @color/white @color/teal_200 @color/black - #D5C4ED @@ -31,19 +30,16 @@ @color/white @color/teal_200 @color/black - @color/lineBaseGreen - @@ -53,7 +49,6 @@ @color/black @color/white @color/black - @color/white @@ -63,7 +58,6 @@ @color/white @color/dividerColor @color/black - @color/dividerColor