新增功能:自定义背景 #22

Merged
p6vxzahlf merged 1 commits from wangyijia_branch into master 1 month ago

@ -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();
}
}
}

@ -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;
}

Loading…
Cancel
Save