Commit 53f1cc90 authored by gaodapeng's avatar gaodapeng

fix bugs

parent 2aa1f9b3
...@@ -9,8 +9,8 @@ android { ...@@ -9,8 +9,8 @@ android {
defaultConfig { defaultConfig {
minSdkVersion 18 minSdkVersion 18
targetSdkVersion 28 targetSdkVersion 28
versionCode 1 versionCode 101
versionName "1.0" versionName "1.01"
consumerProguardFiles "consumer-rules.pro" consumerProguardFiles "consumer-rules.pro"
} }
...@@ -43,7 +43,7 @@ ext { ...@@ -43,7 +43,7 @@ ext {
//仓库地址 //仓库地址
publishURL = "https://nexus.infra.miyatech.com/repository/shualianzhifu/" publishURL = "https://nexus.infra.miyatech.com/repository/shualianzhifu/"
publishVersion = 1.0 publishVersion = 1.01
//skin-lib是androidx版本的。skin-lib-compat是非androidx版本的 //skin-lib是androidx版本的。skin-lib-compat是非androidx版本的
publishArtifactId = "skin-lib" publishArtifactId = "skin-lib"
publishGroupId = 'com.miya.iot' publishGroupId = 'com.miya.iot'
......
...@@ -9,6 +9,7 @@ import android.widget.TextView; ...@@ -9,6 +9,7 @@ import android.widget.TextView;
import androidx.core.view.ViewCompat; import androidx.core.view.ViewCompat;
import com.miya.skin_lib.utils.SkinResources;
import com.miya.skin_lib.utils.SkinThemeUtils; import com.miya.skin_lib.utils.SkinThemeUtils;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -110,7 +111,7 @@ public class SkinAttribute { ...@@ -110,7 +111,7 @@ public class SkinAttribute {
Drawable left = null, top = null, right = null, bottom = null; Drawable left = null, top = null, right = null, bottom = null;
switch (skinPair.attributeName) { switch (skinPair.attributeName) {
case "background": case "background":
Object background = SkinThemeUtils.SkinResources.getInstance().getBackground(skinPair Object background = SkinResources.getInstance().getBackground(skinPair
.resId); .resId);
//背景可能是 @color 也可能是 @drawable //背景可能是 @color 也可能是 @drawable
if (background instanceof Integer) { if (background instanceof Integer) {
...@@ -120,7 +121,7 @@ public class SkinAttribute { ...@@ -120,7 +121,7 @@ public class SkinAttribute {
} }
break; break;
case "src": case "src":
background = SkinThemeUtils.SkinResources.getInstance().getBackground(skinPair.resId); background = SkinResources.getInstance().getBackground(skinPair.resId);
if (background instanceof Integer) { if (background instanceof Integer) {
((ImageView) view).setImageDrawable(new ColorDrawable((Integer) ((ImageView) view).setImageDrawable(new ColorDrawable((Integer)
background)); background));
...@@ -129,20 +130,20 @@ public class SkinAttribute { ...@@ -129,20 +130,20 @@ public class SkinAttribute {
} }
break; break;
case "textColor": case "textColor":
((TextView) view).setTextColor(SkinThemeUtils.SkinResources.getInstance().getColorStateList ((TextView) view).setTextColor(SkinResources.getInstance().getColorStateList
(skinPair.resId)); (skinPair.resId));
break; break;
case "drawableLeft": case "drawableLeft":
left = SkinThemeUtils.SkinResources.getInstance().getDrawable(skinPair.resId); left = SkinResources.getInstance().getDrawable(skinPair.resId);
break; break;
case "drawableTop": case "drawableTop":
top = SkinThemeUtils.SkinResources.getInstance().getDrawable(skinPair.resId); top = SkinResources.getInstance().getDrawable(skinPair.resId);
break; break;
case "drawableRight": case "drawableRight":
right = SkinThemeUtils.SkinResources.getInstance().getDrawable(skinPair.resId); right = SkinResources.getInstance().getDrawable(skinPair.resId);
break; break;
case "drawableBottom": case "drawableBottom":
bottom = SkinThemeUtils.SkinResources.getInstance().getDrawable(skinPair.resId); bottom = SkinResources.getInstance().getDrawable(skinPair.resId);
break; break;
default: default:
break; break;
......
...@@ -10,6 +10,7 @@ import android.text.TextUtils; ...@@ -10,6 +10,7 @@ import android.text.TextUtils;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import com.miya.skin_lib.utils.SkinResources;
import com.miya.skin_lib.utils.SkinThemeUtils; import com.miya.skin_lib.utils.SkinThemeUtils;
import java.io.File; import java.io.File;
...@@ -30,7 +31,7 @@ public class SkinManager extends Observable { ...@@ -30,7 +31,7 @@ public class SkinManager extends Observable {
private SkinManager(Application application) { private SkinManager(Application application) {
this.application = application; this.application = application;
SkinPreference.init(application.getApplicationContext()); SkinPreference.init(application.getApplicationContext());
SkinThemeUtils.SkinResources.init(application); SkinResources.init(application);
application.registerActivityLifecycleCallbacks(new ActivityLifeCycleCallback(this)); application.registerActivityLifecycleCallbacks(new ActivityLifeCycleCallback(this));
loadSkin(SkinPreference.getInstance().getSkin()); loadSkin(SkinPreference.getInstance().getSkin());
} }
...@@ -61,7 +62,7 @@ public class SkinManager extends Observable { ...@@ -61,7 +62,7 @@ public class SkinManager extends Observable {
public void loadSkin(String skinPath) { public void loadSkin(String skinPath) {
if (TextUtils.isEmpty(skinPath) && !new File(skinPath).exists()) {//如果为空或者不存在该文件,则不用管 if (TextUtils.isEmpty(skinPath) && !new File(skinPath).exists()) {//如果为空或者不存在该文件,则不用管
SkinPreference.getInstance().reset(); SkinPreference.getInstance().reset();
SkinThemeUtils.SkinResources.getInstance().reset(); SkinResources.getInstance().reset();
} else { } else {
try { try {
Resources appResource = application.getResources(); Resources appResource = application.getResources();
...@@ -72,7 +73,7 @@ public class SkinManager extends Observable { ...@@ -72,7 +73,7 @@ public class SkinManager extends Observable {
Resources skinResources = new Resources(assetManager, appResource.getDisplayMetrics(), appResource.getConfiguration()); Resources skinResources = new Resources(assetManager, appResource.getDisplayMetrics(), appResource.getConfiguration());
PackageManager packageManager = application.getPackageManager(); PackageManager packageManager = application.getPackageManager();
PackageInfo info = packageManager.getPackageArchiveInfo(skinPath, PackageManager.GET_ACTIVITIES); PackageInfo info = packageManager.getPackageArchiveInfo(skinPath, PackageManager.GET_ACTIVITIES);
SkinThemeUtils.SkinResources.getInstance().applySkin(skinResources, info.packageName); SkinResources.getInstance().applySkin(skinResources, info.packageName);
SkinPreference.getInstance().setSkin(skinPath); SkinPreference.getInstance().setSkin(skinPath);
} catch (Exception e) { } catch (Exception e) {
...@@ -88,7 +89,7 @@ public class SkinManager extends Observable { ...@@ -88,7 +89,7 @@ public class SkinManager extends Observable {
*/ */
public void reset() { public void reset() {
SkinPreference.getInstance().reset(); SkinPreference.getInstance().reset();
SkinThemeUtils.SkinResources.getInstance().reset(); SkinResources.getInstance().reset();
setChanged(); setChanged();
notifyObservers(); notifyObservers();
} }
......
...@@ -2,14 +2,8 @@ package com.miya.skin_lib.utils; ...@@ -2,14 +2,8 @@ package com.miya.skin_lib.utils;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build; import android.os.Build;
import android.text.TextUtils;
import androidx.core.content.res.ResourcesCompat;
/** /**
* 修改主题信息 * 修改主题信息
...@@ -75,120 +69,4 @@ public class SkinThemeUtils { ...@@ -75,120 +69,4 @@ public class SkinThemeUtils {
activity.getWindow().setNavigationBarColor(color); activity.getWindow().setNavigationBarColor(color);
} }
} }
public static class SkinResources {
//皮肤包的包名
private String skinPkgName;
//是否使用默认的皮肤,用原始的内容
private boolean isDefaultSkin = true;
//app原始的resource
private Resources appResources;
//皮肤包的resources
private Resources skinResources;
private static volatile SkinResources instance;
private SkinResources(Context context) {
appResources = context.getResources();
}
public static void init(Context context) {
if (null == instance) {
synchronized (SkinResources.class) {
if (null == instance) {
instance = new SkinResources(context);
}
}
}
}
public static SkinResources getInstance() {
return instance;
}
public void reset() {
skinResources = null;
skinPkgName = null;
isDefaultSkin = true;
}
public void applySkin(Resources resources, String pkgName) {
skinResources = resources;
skinPkgName = pkgName;
//是否使用默认皮肤
isDefaultSkin = TextUtils.isEmpty(pkgName) || resources == null;
}
/**
* 1. 通过原始app中的resId获取到自己的名字(R.color.icon_up,获取到的就是icon_up)和类型
* 2. 根据名字和类型获取皮肤包中的ID
*/
public int getIdentifier(int resId) {
if (isDefaultSkin) {
return resId;
}
//获取名称,如:icon_up
String resName = appResources.getResourceEntryName(resId);
//获取类型,如:drawable
String resType = appResources.getResourceTypeName(resId);
int skinId = skinResources.getIdentifier(resName, resType, skinPkgName);
return skinId;
}
/**
* 输入主app的id,获取皮肤包中的
*/
public int getColor(int resId) {
if (isDefaultSkin) {
return appResources.getColor(resId);
}
int skinId = getIdentifier(resId);
if (skinId == 0) {
return appResources.getColor(resId);
}
return skinResources.getColor(skinId);
}
public Drawable getDrawable(int resId) {
if (isDefaultSkin) {
return ResourcesCompat.getDrawable(appResources, resId, null);
}
int skinId = getIdentifier(resId);
if (skinId == 0) {
return ResourcesCompat.getDrawable(appResources, resId, null);
}
return ResourcesCompat.getDrawable(skinResources, skinId, null);
}
public ColorStateList getColorStateList(int resId) {
if (isDefaultSkin) {
return ResourcesCompat.getColorStateList(appResources, resId, null);
}
int skinId = getIdentifier(resId);
if (skinId == 0) {
return ResourcesCompat.getColorStateList(appResources, resId, null);
}
return ResourcesCompat.getColorStateList(skinResources, skinId, null);
}
/**
* 可能是Color 也可能是drawable
*
* @return
*/
public Object getBackground(int resId) {
String resourceTypeName = appResources.getResourceTypeName(resId);
if ("color".equals(resourceTypeName)) {
return getColor(resId);
} else {
// drawable
return getDrawable(resId);
}
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment