From 9add5d89cbd251cc912680f95bbc888ec0c7f7e1 Mon Sep 17 00:00:00 2001 From: white-yj8109 <19310195525@163.com> Date: Fri, 16 Jan 2026 20:57:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8A=9F=E8=83=BD=EF=BC=9A?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E8=83=8C=E6=99=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ui/BackgroundManager.java | 131 ++++++++++++++++++++++++++++++++++ src/ui/NotesListActivity.java | 71 +++++++++++++++++- 2 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 src/ui/BackgroundManager.java diff --git a/src/ui/BackgroundManager.java b/src/ui/BackgroundManager.java new file mode 100644 index 0000000..7b8d7b2 --- /dev/null +++ b/src/ui/BackgroundManager.java @@ -0,0 +1,131 @@ +package net.micode.notes.ui; + +import android.app.Activity; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.preference.PreferenceManager; +import android.widget.Toast; +import android.view.View; + +import net.micode.notes.R; + +import java.io.InputStream; + +public class BackgroundManager { + public static final int REQUEST_CODE_PICK_IMAGE = 2001; + + private static final String PREF_BG_TYPE = "pref_bg_type"; // 0=color,1=builtin,2=uri + private static final String PREF_BG_COLOR = "pref_bg_color"; + private static final String PREF_BG_RES_ID = "pref_bg_res_id"; + private static final String PREF_BG_URI = "pref_bg_uri"; + + private static final int BG_TYPE_COLOR = 0; + private static final int BG_TYPE_BUILTIN = 1; + private static final int BG_TYPE_URI = 2; + + private Activity mActivity; + private View mRootView; + + public BackgroundManager(Activity activity, int rootViewId) { + mActivity = activity; + mRootView = activity.findViewById(rootViewId); + if (mRootView == null) { + mRootView = activity.getWindow().getDecorView(); + } + } + + public void applyBackgroundFromPrefs() { + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mActivity); + int type = sp.getInt(PREF_BG_TYPE, -1); + if (type == BG_TYPE_COLOR) { + int color = sp.getInt(PREF_BG_COLOR, 0xFFFFFFFF); + applyColorBackground(color); + } else if (type == BG_TYPE_BUILTIN) { + int resId = sp.getInt(PREF_BG_RES_ID, R.drawable.list_background); + applyBuiltinBackground(resId); + } else if (type == BG_TYPE_URI) { + String uriStr = sp.getString(PREF_BG_URI, null); + if (uriStr != null) { + applyUriBackground(Uri.parse(uriStr)); + } + } else { + mRootView.setBackgroundResource(R.drawable.list_background); + } + } + + public void applyColorAndSave(int color) { + applyColorBackground(color); + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mActivity); + sp.edit().putInt(PREF_BG_TYPE, BG_TYPE_COLOR).putInt(PREF_BG_COLOR, color).commit(); + } + + public void applyBuiltinAndSave(int resId) { + applyBuiltinBackground(resId); + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mActivity); + sp.edit().putInt(PREF_BG_TYPE, BG_TYPE_BUILTIN).putInt(PREF_BG_RES_ID, resId).commit(); + } + + public void pickImageFromGallery() { + Intent intent = new Intent(Intent.ACTION_PICK); + intent.setType("image/*"); + mActivity.startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE); + } + + public boolean handleActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK && data != null) { + Uri uri = data.getData(); + if (uri != null) { + applyUriAndSave(uri); + } + return true; + } + return false; + } + + public void resetToDefaultAndClear() { + mRootView.setBackgroundResource(R.drawable.list_background); + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mActivity); + sp.edit().remove(PREF_BG_TYPE).remove(PREF_BG_COLOR).remove(PREF_BG_RES_ID).remove(PREF_BG_URI).commit(); + } + + private void applyColorBackground(int color) { + mRootView.setBackgroundColor(color); + } + + private void applyBuiltinBackground(int resId) { + mRootView.setBackgroundResource(resId); + } + + private void applyUriBackground(Uri uri) { + try { + InputStream is = mActivity.getContentResolver().openInputStream(uri); + if (is != null) { + Drawable d = Drawable.createFromStream(is, uri.toString()); + mRootView.setBackgroundDrawable(d); + is.close(); + } + } catch (Exception e) { + e.printStackTrace(); + Toast.makeText(mActivity, "无法加载图片", Toast.LENGTH_SHORT).show(); + } + } + + private void applyUriAndSave(Uri uri) { + try { + InputStream is = mActivity.getContentResolver().openInputStream(uri); + if (is != null) { + Drawable d = Drawable.createFromStream(is, uri.toString()); + mRootView.setBackgroundDrawable(d); + is.close(); + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mActivity); + sp.edit().putInt(PREF_BG_TYPE, BG_TYPE_URI).putString(PREF_BG_URI, uri.toString()).commit(); + } + } catch (Exception e) { + e.printStackTrace(); + Toast.makeText(mActivity, "无法加载图片", Toast.LENGTH_SHORT).show(); + } + } +} diff --git a/src/ui/NotesListActivity.java b/src/ui/NotesListActivity.java index 42bac1d..186fe6b 100644 --- a/src/ui/NotesListActivity.java +++ b/src/ui/NotesListActivity.java @@ -59,7 +59,7 @@ import android.widget.ListView; import android.widget.PopupMenu; import android.widget.TextView; import android.widget.Toast; - +import net.micode.notes.ui.BackgroundManager; import net.micode.notes.R; import net.micode.notes.data.Notes; import net.micode.notes.data.Notes.NoteColumns; @@ -72,6 +72,12 @@ import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute; import net.micode.notes.widget.NoteWidgetProvider_2x; import net.micode.notes.widget.NoteWidgetProvider_4x; +import android.net.Uri; +import android.app.Activity; +import android.content.Intent; +import android.view.Menu; +import android.view.MenuItem; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -156,6 +162,10 @@ public class NotesListActivity extends Activity implements OnClickListener, OnIt @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {// 处理子活动返回结果 + //如果是背景管理器的结果,交给背景管理器处理 + if (mBackgroundManager != null && mBackgroundManager.handleActivityResult(requestCode, resultCode, data)) { + return; + } if (resultCode == RESULT_OK && (requestCode == REQUEST_CODE_OPEN_NODE || requestCode == REQUEST_CODE_NEW_NODE)) { mNotesListAdapter.changeCursor(null); @@ -245,8 +255,62 @@ public class NotesListActivity extends Activity implements OnClickListener, OnIt mTitleBar = (TextView) findViewById(R.id.tv_title_bar); mState = ListEditState.NOTE_LIST; mModeCallBack = new ModeCallback(); + //初始化背景管理器 + mNotesRootView = findViewById(R.id.notes_root); + + mBackgroundManager = new BackgroundManager(this, R.id.notes_root); + mBackgroundManager.applyBackgroundFromPrefs(); + } + //显示背景设置对话框 + private void showBackgroundSettingsDialog() { + final String[] options = new String[] {"纯色背景", "系统内置图片", "从相册选择", "恢复默认"}; + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle("选择背景"); + builder.setItems(options, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + if (which == 0) { + showColorChoiceDialog(); + } else if (which == 1) { + showBuiltinChoiceDialog(); + } else if (which == 2) { + if (mBackgroundManager != null) mBackgroundManager.pickImageFromGallery(); + } else if (which == 3) { + if (mBackgroundManager != null) mBackgroundManager.resetToDefaultAndClear(); + } + } + }); + builder.show(); } + private void showColorChoiceDialog() { + final int[] colors = new int[] {0xFFFFFFFF, 0xFFFFFFCC, 0xFFFFF0F0, 0xFFE8FFF0, 0xFFDDE8FF}; + final String[] names = new String[] {"白色", "浅黄", "浅红", "浅绿", "浅蓝"}; + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle("选择纯色背景"); + builder.setItems(names, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + int color = colors[which]; + if (mBackgroundManager != null) mBackgroundManager.applyColorAndSave(color); + } + }); + builder.show(); + } + + private void showBuiltinChoiceDialog() { + final int[] resIds = new int[] {R.drawable.background_1, R.drawable.background_2, R.drawable.background_3, R.drawable.background_4}; + final String[] names = new String[] {"预设1", "预设2", "预设3", "预设4"}; + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle("选择内置背景"); + builder.setItems(names, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + int resId = resIds[which]; + if (mBackgroundManager != null) mBackgroundManager.applyBuiltinAndSave(resId); + } + }); + builder.show(); + } + + /** * 多选模式回调类,处理批量操作和菜单项点击事件 */ @@ -862,6 +926,11 @@ public class NotesListActivity extends Activity implements OnClickListener, OnIt onSearchRequested(); break; } + //如果是背景设置菜单项 + case R.id.menu_background_settings: { + showBackgroundSettingsDialog(); + break; + } default: break; }