Skip to content

Commit

Permalink
ToastUtil:
Browse files Browse the repository at this point in the history
1, 重构, 代码
Other:
1, 优化, Context获取方式, 统一改为Application
2, 适配, AS4.1, 移除Library的versionCode及versionName
Project:
1, 更新, 版号
  • Loading branch information
zuilintan committed Oct 31, 2020
1 parent a84a94c commit f1abaa1
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 49 deletions.
2 changes: 1 addition & 1 deletion config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ext {
minSdk : 28,
targetSdk : 28,
versionCode : 1,
versionName : "1.1.7",
versionName : "1.1.8",

storeFile : '',
storePassword: '',
Expand Down
2 changes: 0 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ android {
defaultConfig {
minSdkVersion rootProject.ext.android.minSdk
targetSdkVersion rootProject.ext.android.targetSdk
versionCode rootProject.ext.android.versionCode
versionName rootProject.ext.android.versionName
consumerProguardFiles 'consumer-rules.pro'
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public <T extends View> T findViewById(int viewId) {
return (T) view;
}//findViewById,并复用

public Context getContext() {
return ContextUtil.getInstance().getContext();
public Context getAppContext() {
return ContextUtil.getInstance().getApplication();
}

public BaseViewHolder setText(int viewId, CharSequence text) {
Expand All @@ -79,19 +79,19 @@ public BaseViewHolder setTextSize(int viewId, @DimenRes int dimenId) {

public BaseViewHolder setTextSize(int viewId, @DimenRes int dimenId, int unit) {
TextView view = findViewById(viewId);
view.setTextSize(unit, getContext().getResources().getDimension(dimenId));
view.setTextSize(unit, getAppContext().getResources().getDimension(dimenId));
return this;
}//设置文本大小

public BaseViewHolder setTextColor(int viewId, @ColorRes int colorId) {
TextView view = findViewById(viewId);
view.setTextColor(ContextCompat.getColor(getContext(), colorId));
view.setTextColor(ContextCompat.getColor(getAppContext(), colorId));
return this;
}//设置文本颜色

public BaseViewHolder setTextColorStateList(int viewId, @ColorRes int colorId) {
TextView view = findViewById(viewId);
view.setTextColor(ContextCompat.getColorStateList(getContext(), colorId));
view.setTextColor(ContextCompat.getColorStateList(getAppContext(), colorId));
return this;
}//设置文本颜色

Expand All @@ -100,23 +100,23 @@ public BaseViewHolder setImageDrawable(int viewId, @DrawableRes int drawableId)
if (drawableId == RES_EMPTY_ID) {
view.setImageDrawable(null);
} else {
view.setImageDrawable(ContextCompat.getDrawable(getContext(), drawableId));
view.setImageDrawable(ContextCompat.getDrawable(getAppContext(), drawableId));
}
return this;
}//设置图片资源

public BaseViewHolder setImageDrawableTint(int viewId, @DrawableRes int drawableId, @ColorRes int colorId) {
ImageView view = findViewById(viewId);
Drawable drawable = ContextCompat.getDrawable(getContext(), drawableId);
DrawableCompat.setTint(Objects.requireNonNull(drawable), ContextCompat.getColor(getContext(), colorId));
Drawable drawable = ContextCompat.getDrawable(getAppContext(), drawableId);
DrawableCompat.setTint(Objects.requireNonNull(drawable), ContextCompat.getColor(getAppContext(), colorId));
view.setImageDrawable(drawable);
return this;
}//设置图片资源

public BaseViewHolder setImageDrawableTintList(int viewId, @DrawableRes int drawableId, @ColorRes int colorId) {
ImageView view = findViewById(viewId);
Drawable drawable = ContextCompat.getDrawable(getContext(), drawableId);
DrawableCompat.setTintList(Objects.requireNonNull(drawable), ContextCompat.getColorStateList(getContext(), colorId));
Drawable drawable = ContextCompat.getDrawable(getAppContext(), drawableId);
DrawableCompat.setTintList(Objects.requireNonNull(drawable), ContextCompat.getColorStateList(getAppContext(), colorId));
view.setImageDrawable(drawable);
return this;
}//设置图片资源
Expand All @@ -126,14 +126,14 @@ public BaseViewHolder setBackground(int viewId, @DrawableRes int drawableId) {
if (drawableId == RES_EMPTY_ID) {
view.setBackground(null);
} else {
view.setBackground(ContextCompat.getDrawable(getContext(), drawableId));
view.setBackground(ContextCompat.getDrawable(getAppContext(), drawableId));
}
return this;
}//设置背景资源

public BaseViewHolder setBackgroundColor(int viewId, @ColorRes int colorId) {
View view = findViewById(viewId);
view.setBackgroundColor(ContextCompat.getColor(getContext(), colorId));
view.setBackgroundColor(ContextCompat.getColor(getAppContext(), colorId));
return this;
}//设置背景颜色

Expand Down
8 changes: 4 additions & 4 deletions library/src/main/java/com/lt/library/util/DensityUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,29 @@ private DensityUtil() {

public static int dp2px(float dpValue) {
float density = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue,
ContextUtil.getInstance().getContext()
ContextUtil.getInstance().getApplication()
.getResources()
.getDisplayMetrics());
return (int) (density + 0.5F);//加0.5F以四舍五入,eg: 1.4+0.5=1.9转为int是1,而1.5 + 0.5 = 2.0转换成int后就是2
}//dp转px

public static int px2dp(float pxValue) {
float scale = ContextUtil.getInstance().getContext()
float scale = ContextUtil.getInstance().getApplication()
.getResources()
.getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5F);//加0.5F以四舍五入,eg: 1.4+0.5=1.9转为int是1,而1.5 + 0.5 = 2.0转换成int后就是2
}//px转dp

public static int sp2px(float spValue) {
float density = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spValue,
ContextUtil.getInstance().getContext()
ContextUtil.getInstance().getApplication()
.getResources()
.getDisplayMetrics());
return (int) (density + 0.5F);//加0.5F以四舍五入,eg: 1.4+0.5=1.9转为int是1,而1.5 + 0.5 = 2.0转换成int后就是2
}//sp转px

public static int px2sp(float pxValue) {
float scale = ContextUtil.getInstance().getContext()
float scale = ContextUtil.getInstance().getApplication()
.getResources()
.getDisplayMetrics().scaledDensity;
return (int) (pxValue / scale + 0.5F);//加0.5F以四舍五入,eg: 1.4+0.5=1.9转为int是1,而1.5 + 0.5 = 2.0转换成int后就是2
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/java/com/lt/library/util/LogUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

public class LogUtil {
private static AtomicBoolean sIsEnabled = new AtomicBoolean(true);//默认启用LogUtil
private static final AtomicBoolean sIsEnabled = new AtomicBoolean(true);//默认启用

private LogUtil() {
throw new UnsupportedOperationException("cannot be instantiated");
Expand Down
12 changes: 6 additions & 6 deletions library/src/main/java/com/lt/library/util/SPUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private SPUtil() {
}

public static void put(String key, Object value) {
SharedPreferences sp = ContextUtil.getInstance().getContext()
SharedPreferences sp = ContextUtil.getInstance().getApplication()
.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
String typeName = value.getClass().getSimpleName();
Expand All @@ -53,7 +53,7 @@ public static void put(String key, Object value) {
}//存放数据,通过反射获取value的类型,然后调用对应的方法存值

public static Object get(String key, Object defaultValue) {
SharedPreferences sp = ContextUtil.getInstance().getContext()
SharedPreferences sp = ContextUtil.getInstance().getApplication()
.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
String typeName = defaultValue.getClass().getSimpleName();
Object result = null;
Expand All @@ -80,29 +80,29 @@ public static Object get(String key, Object defaultValue) {
}//获取数据,通过反射获取value的类型,然后调用对应的方法取值

public static void remove(String key) {
SharedPreferences sp = ContextUtil.getInstance().getContext()
SharedPreferences sp = ContextUtil.getInstance().getApplication()
.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.remove(key);
SharedPreferencesCompat.apply(editor);
}//移除某个key值已经对应的值

public static void clear() {
SharedPreferences sp = ContextUtil.getInstance().getContext()
SharedPreferences sp = ContextUtil.getInstance().getApplication()
.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.clear();
SharedPreferencesCompat.apply(editor);
}//清除所有数据

public static boolean contains(String key) {
SharedPreferences sp = ContextUtil.getInstance().getContext()
SharedPreferences sp = ContextUtil.getInstance().getApplication()
.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
return sp.contains(key);
}//查询某个key是否已经存在

public static Map<String, ?> getAll() {
SharedPreferences sp = ContextUtil.getInstance().getContext()
SharedPreferences sp = ContextUtil.getInstance().getApplication()
.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
return sp.getAll();
}//返回所有的键值对
Expand Down
8 changes: 4 additions & 4 deletions library/src/main/java/com/lt/library/util/ScreenUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static void hideStatusBar(Activity activity) {
}//隐藏StatusBar

public static int getScreenWidth() {
WindowManager windowManager = (WindowManager) ContextUtil.getInstance().getContext()
WindowManager windowManager = (WindowManager) ContextUtil.getInstance().getApplication()
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics displayMetrics = new DisplayMetrics();
windowManager.getDefaultDisplay()
Expand All @@ -60,7 +60,7 @@ public static int getScreenWidth() {
}//获取屏幕宽度

public static int getScreenHeight() {
WindowManager windowManager = (WindowManager) ContextUtil.getInstance().getContext()
WindowManager windowManager = (WindowManager) ContextUtil.getInstance().getApplication()
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics displayMetrics = new DisplayMetrics();
windowManager.getDefaultDisplay()
Expand All @@ -78,9 +78,9 @@ public static int getStatusBarHeight() {
//Object statusBarHeightObj = ReflectionUtil.getField("com.android.internal.R$dimen", "status_bar_height");
//int resId = Integer.parseInt(Objects.requireNonNull(statusBarHeightObj).toString());
//Plan B
int resId = ContextUtil.getInstance().getContext()
int resId = ContextUtil.getInstance().getApplication()
.getResources().getIdentifier("status_bar_height", "dimen", "android");
return ContextUtil.getInstance().getContext()
return ContextUtil.getInstance().getApplication()
.getResources().getDimensionPixelSize(resId);
}//获取状态栏高度

Expand Down
53 changes: 34 additions & 19 deletions library/src/main/java/com/lt/library/util/ToastUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.lt.library.util;

import android.annotation.SuppressLint;
import android.support.annotation.IntDef;
import android.support.annotation.StringRes;
import android.view.Gravity;
Expand All @@ -10,6 +9,8 @@

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* @作者: LinTan
Expand All @@ -21,68 +22,82 @@
*/

public class ToastUtil {
private static final AtomicBoolean sIsEnabled = new AtomicBoolean(true);//默认启用
private static Toast sToast;
private static boolean sIsEnabled = true;//默认启用LogUtil

private ToastUtil() {
throw new UnsupportedOperationException("cannot be instantiated");
}

public static void isEnabled(boolean isEnabled) {
sIsEnabled = isEnabled;
}//全局控制是否启用ToastUtil

public static void show(@StringRes int stringId) {
if (!sIsEnabled) return;
generateToast(stringId, Gravity.CENTER, Toast.LENGTH_SHORT).show();
if (isEnable()) {
genStdToast(stringId, Gravity.CENTER, Toast.LENGTH_SHORT).show();
}
}

public static void show(String string) {
generateToast(string, Gravity.CENTER, Toast.LENGTH_SHORT).show();
if (isEnable()) {
genStdToast(string, Gravity.CENTER, Toast.LENGTH_SHORT).show();
}
}

public static void show(@StringRes int stringId, @GravityDef int gravity) {
generateToast(stringId, gravity, Toast.LENGTH_SHORT).show();
if (isEnable()) {
genStdToast(stringId, gravity, Toast.LENGTH_SHORT).show();
}
}

public static void show(String string, @GravityDef int gravity) {
generateToast(string, gravity, Toast.LENGTH_SHORT).show();
if (isEnable()) {
genStdToast(string, gravity, Toast.LENGTH_SHORT).show();
}
}

public static void show(@StringRes int stringId, @GravityDef int gravity, @DurationDef int duration) {
generateToast(stringId, gravity, duration).show();
if (isEnable()) {
genStdToast(stringId, gravity, duration).show();
}
}

public static void show(String string, @GravityDef int gravity, @DurationDef int duration) {
generateToast(string, gravity, duration).show();
if (isEnable()) {
genStdToast(string, gravity, duration).show();
}
}

public static void cancel() {
if (sIsEnabled && sToast != null) {
if (isEnable() && Objects.nonNull(sToast)) {
sToast.cancel();
sToast = null;
}
}

@SuppressLint("ShowToast")
private static Toast generateToast(Object object, @GravityDef int gravity, @DurationDef int duration) {
public static boolean isEnable() {
return sIsEnabled.get();
}

public static void setEnable(boolean isEnabled) {
sIsEnabled.set(isEnabled);
}

private static Toast genStdToast(Object object, @GravityDef int gravity, @DurationDef int duration) {
String text = null;
String typeName = object.getClass().getSimpleName();
switch (typeName) {
case "String":
text = (String) object;
break;
case "Integer":
text = ContextUtil.getInstance().getContext().getString((Integer) object);
text = ContextUtil.getInstance().getApplication().getString((Integer) object);
break;
default:
break;
}
if (text == null) {
if (Objects.isNull(text)) {
text = "";
}
cancel();//如果当前Toast没有消失,则取消该Toast
sToast = Toast.makeText(ContextUtil.getInstance().getContext(), text, duration);
sToast = Toast.makeText(ContextUtil.getInstance().getApplication(), text, duration);
sToast.setGravity(gravity, 0, 0);//仅初次创建时有效
return sToast;
}
Expand Down

0 comments on commit f1abaa1

Please sign in to comment.