Skip to content

Commit

Permalink
增加SPUtils,修改主题界面为圆角图片, 修改主页面主题
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerey-Jobs committed Jun 8, 2017
1 parent 6ab893f commit eec5380
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
android:name=".MainActivity"
android:label="@string/study"
android:launchMode="singleTask"
android:theme="@style/MainTheme"
android:theme="@style/AppTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
Expand Down
53 changes: 53 additions & 0 deletions app/src/main/java/com/jerey/keepgank/View/GlideRoundTransform.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -142,6 +178,7 @@ private void updateUI() {
theme = THEME_DEFAULT;
}
updateUIByName(theme);

}

private static final String THEME_NIGHT = "com.jerey.theme_night";
Expand Down Expand Up @@ -178,7 +215,5 @@ public void onViewClicked(View view) {
onThemeOceanClicked();
break;
}


}
}
171 changes: 171 additions & 0 deletions app/src/main/java/com/jerey/keepgank/utils/SPUtils.java
Original file line number Diff line number Diff line change
@@ -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 <T> 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> 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<String, ?> 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();
}
}

}
3 changes: 3 additions & 0 deletions app/src/main/res/layout/activity_theme_choose.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
android:orientation="vertical">

<ImageView
android:id="@+id/theme_default_img"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@drawable/theme_defalut"/>
Expand All @@ -58,6 +59,7 @@
android:orientation="vertical">

<ImageView
android:id="@+id/theme_night_img"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@drawable/theme_dark"/>
Expand All @@ -81,6 +83,7 @@
android:orientation="vertical">

<ImageView
android:id="@+id/theme_ocean_img"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@drawable/theme_ocean"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_day.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:skin="http://schemas.android.com/android/skin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/translucent_grey_300_96p"
android:background="@color/normal_background_grey"
skin:enable="true">

<android.support.design.widget.AppBarLayout
Expand Down

0 comments on commit eec5380

Please sign in to comment.