Commit b1b64c32 authored by gaodapeng's avatar gaodapeng

fix bugs

parent 47c1673e
...@@ -11,8 +11,8 @@ android { ...@@ -11,8 +11,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"
} }
...@@ -39,7 +39,7 @@ ext { ...@@ -39,7 +39,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-compat表示兼容之前版本,skin-lib是androidx版本的 //skin-lib-compat表示兼容之前版本,skin-lib是androidx版本的
publishArtifactId = "skin-lib-compat" publishArtifactId = "skin-lib-compat"
publishGroupId = 'com.miya.iot' publishGroupId = 'com.miya.iot'
......
...@@ -8,6 +8,7 @@ import android.view.View; ...@@ -8,6 +8,7 @@ import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
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;
...@@ -109,7 +110,7 @@ public class SkinAttribute { ...@@ -109,7 +110,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) {
...@@ -119,7 +120,7 @@ public class SkinAttribute { ...@@ -119,7 +120,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));
...@@ -128,20 +129,20 @@ public class SkinAttribute { ...@@ -128,20 +129,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;
......
...@@ -9,6 +9,7 @@ import android.os.Build; ...@@ -9,6 +9,7 @@ import android.os.Build;
import android.support.annotation.RequiresApi; import android.support.annotation.RequiresApi;
import android.text.TextUtils; import android.text.TextUtils;
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;
...@@ -29,7 +30,7 @@ public class SkinManager extends Observable { ...@@ -29,7 +30,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());
} }
...@@ -60,7 +61,7 @@ public class SkinManager extends Observable { ...@@ -60,7 +61,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();
...@@ -71,7 +72,7 @@ public class SkinManager extends Observable { ...@@ -71,7 +72,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) {
...@@ -87,7 +88,7 @@ public class SkinManager extends Observable { ...@@ -87,7 +88,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();
} }
......
...@@ -76,119 +76,4 @@ public class SkinThemeUtils { ...@@ -76,119 +76,4 @@ public class SkinThemeUtils {
} }
} }
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