From eec538070489da87a8a802b5be4bc2431e8ecd23 Mon Sep 17 00:00:00 2001 From: Xiamin <610315802@qq.com> Date: Thu, 8 Jun 2017 21:36:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0SPUtils=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=B8=BB=E9=A2=98=E7=95=8C=E9=9D=A2=E4=B8=BA=E5=9C=86?= =?UTF-8?q?=E8=A7=92=E5=9B=BE=E7=89=87=EF=BC=8C=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=B8=BB=E9=A1=B5=E9=9D=A2=E4=B8=BB=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 2 +- .../keepgank/View/GlideRoundTransform.java | 53 ++++++ .../activity/ThemeChooseActivity.java | 41 ++++- .../com/jerey/keepgank/utils/SPUtils.java | 171 ++++++++++++++++++ .../main/res/layout/activity_theme_choose.xml | 3 + app/src/main/res/layout/fragment_day.xml | 2 +- 6 files changed, 267 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/com/jerey/keepgank/View/GlideRoundTransform.java create mode 100644 app/src/main/java/com/jerey/keepgank/utils/SPUtils.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 332747f..44846c7 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -19,7 +19,7 @@ android:name=".MainActivity" android:label="@string/study" android:launchMode="singleTask" - android:theme="@style/MainTheme" + android:theme="@style/AppTheme" > diff --git a/app/src/main/java/com/jerey/keepgank/View/GlideRoundTransform.java b/app/src/main/java/com/jerey/keepgank/View/GlideRoundTransform.java new file mode 100644 index 0000000..bf20f6f --- /dev/null +++ b/app/src/main/java/com/jerey/keepgank/View/GlideRoundTransform.java @@ -0,0 +1,53 @@ +package com.jerey.keepgank.View; + +import android.content.Context; +import android.content.res.Resources; +import android.graphics.Bitmap; +import android.graphics.BitmapShader; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.RectF; + +import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; +import com.bumptech.glide.load.resource.bitmap.BitmapTransformation; + +public class GlideRoundTransform extends BitmapTransformation { + + private static float radius = 0f; + + public GlideRoundTransform(Context context) { + this(context, 4); + } + + public GlideRoundTransform(Context context, int dp) { + super(context); + this.radius = Resources.getSystem().getDisplayMetrics().density * dp; + } + + @Override + protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { + return roundCrop(pool, toTransform); + } + + private static Bitmap roundCrop(BitmapPool pool, Bitmap source) { + if (source == null) return null; + + Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); + if (result == null) { + result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); + } + + Canvas canvas = new Canvas(result); + Paint paint = new Paint(); + paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); + paint.setAntiAlias(true); + RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight()); + canvas.drawRoundRect(rectF, radius, radius, paint); + return result; + } + + @Override + public String getId() { + return getClass().getName() + Math.round(radius); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/jerey/keepgank/activity/ThemeChooseActivity.java b/app/src/main/java/com/jerey/keepgank/activity/ThemeChooseActivity.java index 25aae20..1c75bd3 100644 --- a/app/src/main/java/com/jerey/keepgank/activity/ThemeChooseActivity.java +++ b/app/src/main/java/com/jerey/keepgank/activity/ThemeChooseActivity.java @@ -4,11 +4,15 @@ import android.support.v7.widget.Toolbar; import android.view.MenuItem; import android.view.View; +import android.widget.ImageView; import android.widget.RadioButton; import android.widget.Toast; +import com.bumptech.glide.Glide; import com.jerey.keepgank.R; +import com.jerey.keepgank.View.GlideRoundTransform; import com.jerey.keepgank.base.AppSwipeBackActivity; +import com.jerey.keepgank.utils.SPUtils; import com.jerey.loglib.LogTools; import com.jerey.themelib.SkinLoaderListener; import com.jerey.themelib.loader.SkinManager; @@ -22,6 +26,8 @@ */ public class ThemeChooseActivity extends AppSwipeBackActivity { + public static final String FONT_STRING = "current-font"; + public static final String FONT_DEFAULT = "font_default"; @Bind(R.id.theme_default) RadioButton mRadioThemeDefault; @@ -33,6 +39,12 @@ public class ThemeChooseActivity extends AppSwipeBackActivity { RadioButton mRadioFontDefault; @Bind(R.id.font_wryh) RadioButton mRadioFontWryh; + @Bind(R.id.theme_default_img) + ImageView mThemeDefaultImg; + @Bind(R.id.theme_night_img) + ImageView mThemeNightImg; + @Bind(R.id.theme_ocean_img) + ImageView mThemeOceanImg; Toolbar mToolbar; @@ -43,8 +55,26 @@ protected void onCreate(Bundle savedInstanceState) { ButterKnife.bind(this); mToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mToolbar); + Glide.with(this) + .load(R.drawable.theme_defalut) + .transform(new GlideRoundTransform(this, 5)) + .into(mThemeDefaultImg); + Glide.with(this) + .load(R.drawable.theme_dark) + .transform(new GlideRoundTransform(this, 5)) + .into(mThemeNightImg); + Glide.with(this) + .load(R.drawable.theme_ocean) + .transform(new GlideRoundTransform(this, 5)) + .into(mThemeOceanImg); updateUI(); - + if (SPUtils.get(this, FONT_DEFAULT, true)) { + mRadioFontDefault.setChecked(true); + mRadioFontWryh.setChecked(false); + } else { + mRadioFontDefault.setChecked(false); + mRadioFontWryh.setChecked(true); + } } @OnClick(R.id.theme_default) @@ -117,11 +147,17 @@ public void onProgress(int progress) { @OnClick(R.id.font_default) public void onFontDefaultClicked() { SkinManager.getInstance().loadFont(""); + SPUtils.put(this, FONT_DEFAULT, true); + mRadioFontWryh.setChecked(false); + mRadioFontDefault.setChecked(true); } @OnClick(R.id.font_wryh) public void onFontWryhClicked() { SkinManager.getInstance().loadFont("WRYHZT.ttf"); + SPUtils.put(this, FONT_DEFAULT, false); + mRadioFontWryh.setChecked(true); + mRadioFontDefault.setChecked(false); } @Override @@ -142,6 +178,7 @@ private void updateUI() { theme = THEME_DEFAULT; } updateUIByName(theme); + } private static final String THEME_NIGHT = "com.jerey.theme_night"; @@ -178,7 +215,5 @@ public void onViewClicked(View view) { onThemeOceanClicked(); break; } - - } } diff --git a/app/src/main/java/com/jerey/keepgank/utils/SPUtils.java b/app/src/main/java/com/jerey/keepgank/utils/SPUtils.java new file mode 100644 index 0000000..d35f440 --- /dev/null +++ b/app/src/main/java/com/jerey/keepgank/utils/SPUtils.java @@ -0,0 +1,171 @@ +package com.jerey.keepgank.utils; + +import android.content.Context; +import android.content.SharedPreferences; + +import com.jerey.loglib.LogTools; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Map; + +public class SPUtils { + /** + * 保存在手机里面的文件名 + */ + public static final String FILE_NAME = "setting_data"; + + /** + * 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法 + * @param context + * @param key + * @param object + */ + public static void put(Context context, String key, T object) { + + SharedPreferences sp = context.getSharedPreferences(FILE_NAME, + Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sp.edit(); + LogTools.i("put :" + object); + if (object instanceof Double) { + LogTools.i("put defaultObject instanceof Double"); + editor.putFloat(key, new Float((Double) object)); + } + + if (object instanceof String) { + editor.putString(key, (String) object); + } else if (object instanceof Integer) { + editor.putInt(key, (Integer) object); + } else if (object instanceof Boolean) { + editor.putBoolean(key, (Boolean) object); + } else if (object instanceof Float) { + LogTools.i("put defaultObject instanceof Float"); + editor.putFloat(key, (Float) object); + } else if (object instanceof Long) { + editor.putLong(key, (Long) object); + } else { + editor.putString(key, object.toString()); + } + + SharedPreferencesCompat.apply(editor); + } + + /** + * 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值 + * @param context + * @param key + * @param defaultObject + * @return + */ + public static T get(Context context, String key, T defaultObject) { + SharedPreferences sp = context.getSharedPreferences(FILE_NAME, + Context.MODE_PRIVATE); + + if (defaultObject instanceof String) { + return (T) sp.getString(key, (String) defaultObject); + } else if (defaultObject instanceof Integer) { + return (T) new Integer(sp.getInt(key, (Integer) defaultObject)); + } else if (defaultObject instanceof Boolean) { + return (T) new Boolean(sp.getBoolean(key, (Boolean) defaultObject)); + } else if (defaultObject instanceof Float) { + LogTools.i("defaultObject instanceof Float"); + return (T) new Float(sp.getFloat(key, (Float) defaultObject)); + } else if (defaultObject instanceof Double) { + LogTools.i("defaultObject instanceof double"); + return (T) new Double(sp.getFloat(key, Float.parseFloat(defaultObject.toString()))); + } else if (defaultObject instanceof Long) { + return (T) new Long(sp.getLong(key, (Long) defaultObject)); + } + + return null; + } + + /** + * 移除某个key值已经对应的值 + * @param context + * @param key + */ + public static void remove(Context context, String key) { + SharedPreferences sp = context.getSharedPreferences(FILE_NAME, + Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sp.edit(); + editor.remove(key); + SharedPreferencesCompat.apply(editor); + } + + /** + * 清除所有数据 + * @param context + */ + public static void clear(Context context) { + SharedPreferences sp = context.getSharedPreferences(FILE_NAME, + Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sp.edit(); + editor.clear(); + SharedPreferencesCompat.apply(editor); + } + + /** + * 查询某个key是否已经存在 + * @param context + * @param key + * @return + */ + public static boolean contains(Context context, String key) { + SharedPreferences sp = context.getSharedPreferences(FILE_NAME, + Context.MODE_PRIVATE); + return sp.contains(key); + } + + /** + * 返回所有的键值对 + * @param context + * @return + */ + public static Map getAll(Context context) { + SharedPreferences sp = context.getSharedPreferences(FILE_NAME, + Context.MODE_PRIVATE); + return sp.getAll(); + } + + /** + * 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类 + * @author zhy + */ + private static class SharedPreferencesCompat { + private static final Method sApplyMethod = findApplyMethod(); + + /** + * 反射查找apply的方法 + * @return + */ + @SuppressWarnings({"unchecked", "rawtypes"}) + private static Method findApplyMethod() { + try { + Class clz = SharedPreferences.Editor.class; + return clz.getMethod("apply"); + } catch (NoSuchMethodException e) { + } + + return null; + } + + /** + * 如果找到则使用apply执行,否则使用commit + * @param editor + */ + public static void apply(SharedPreferences.Editor editor) { + try { + if (sApplyMethod != null) { + sApplyMethod.invoke(editor); + return; + } + } catch (IllegalArgumentException e) { + } catch (IllegalAccessException e) { + } catch (InvocationTargetException e) { + } + editor.commit(); + } + } + +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_theme_choose.xml b/app/src/main/res/layout/activity_theme_choose.xml index 00ba44e..1b0ed18 100644 --- a/app/src/main/res/layout/activity_theme_choose.xml +++ b/app/src/main/res/layout/activity_theme_choose.xml @@ -35,6 +35,7 @@ android:orientation="vertical"> @@ -58,6 +59,7 @@ android:orientation="vertical"> @@ -81,6 +83,7 @@ android:orientation="vertical">