Commit 53f1cc90 authored by gaodapeng's avatar gaodapeng

fix bugs

parent 2aa1f9b3
......@@ -9,8 +9,8 @@ android {
defaultConfig {
minSdkVersion 18
targetSdkVersion 28
versionCode 1
versionName "1.0"
versionCode 101
versionName "1.01"
consumerProguardFiles "consumer-rules.pro"
}
......@@ -43,7 +43,7 @@ ext {
//仓库地址
publishURL = "https://nexus.infra.miyatech.com/repository/shualianzhifu/"
publishVersion = 1.0
publishVersion = 1.01
//skin-lib是androidx版本的。skin-lib-compat是非androidx版本的
publishArtifactId = "skin-lib"
publishGroupId = 'com.miya.iot'
......
......@@ -9,6 +9,7 @@ import android.widget.TextView;
import androidx.core.view.ViewCompat;
import com.miya.skin_lib.utils.SkinResources;
import com.miya.skin_lib.utils.SkinThemeUtils;
import java.util.ArrayList;
......@@ -110,7 +111,7 @@ public class SkinAttribute {
Drawable left = null, top = null, right = null, bottom = null;
switch (skinPair.attributeName) {
case "background":
Object background = SkinThemeUtils.SkinResources.getInstance().getBackground(skinPair
Object background = SkinResources.getInstance().getBackground(skinPair
.resId);
//背景可能是 @color 也可能是 @drawable
if (background instanceof Integer) {
......@@ -120,7 +121,7 @@ public class SkinAttribute {
}
break;
case "src":
background = SkinThemeUtils.SkinResources.getInstance().getBackground(skinPair.resId);
background = SkinResources.getInstance().getBackground(skinPair.resId);
if (background instanceof Integer) {
((ImageView) view).setImageDrawable(new ColorDrawable((Integer)
background));
......@@ -129,20 +130,20 @@ public class SkinAttribute {
}
break;
case "textColor":
((TextView) view).setTextColor(SkinThemeUtils.SkinResources.getInstance().getColorStateList
((TextView) view).setTextColor(SkinResources.getInstance().getColorStateList
(skinPair.resId));
break;
case "drawableLeft":
left = SkinThemeUtils.SkinResources.getInstance().getDrawable(skinPair.resId);
left = SkinResources.getInstance().getDrawable(skinPair.resId);
break;
case "drawableTop":
top = SkinThemeUtils.SkinResources.getInstance().getDrawable(skinPair.resId);
top = SkinResources.getInstance().getDrawable(skinPair.resId);
break;
case "drawableRight":
right = SkinThemeUtils.SkinResources.getInstance().getDrawable(skinPair.resId);
right = SkinResources.getInstance().getDrawable(skinPair.resId);
break;
case "drawableBottom":
bottom = SkinThemeUtils.SkinResources.getInstance().getDrawable(skinPair.resId);
bottom = SkinResources.getInstance().getDrawable(skinPair.resId);
break;
default:
break;
......
......@@ -10,6 +10,7 @@ import android.text.TextUtils;
import androidx.annotation.RequiresApi;
import com.miya.skin_lib.utils.SkinResources;
import com.miya.skin_lib.utils.SkinThemeUtils;
import java.io.File;
......@@ -30,7 +31,7 @@ public class SkinManager extends Observable {
private SkinManager(Application application) {
this.application = application;
SkinPreference.init(application.getApplicationContext());
SkinThemeUtils.SkinResources.init(application);
SkinResources.init(application);
application.registerActivityLifecycleCallbacks(new ActivityLifeCycleCallback(this));
loadSkin(SkinPreference.getInstance().getSkin());
}
......@@ -61,7 +62,7 @@ public class SkinManager extends Observable {
public void loadSkin(String skinPath) {
if (TextUtils.isEmpty(skinPath) && !new File(skinPath).exists()) {//如果为空或者不存在该文件,则不用管
SkinPreference.getInstance().reset();
SkinThemeUtils.SkinResources.getInstance().reset();
SkinResources.getInstance().reset();
} else {
try {
Resources appResource = application.getResources();
......@@ -72,7 +73,7 @@ public class SkinManager extends Observable {
Resources skinResources = new Resources(assetManager, appResource.getDisplayMetrics(), appResource.getConfiguration());
PackageManager packageManager = application.getPackageManager();
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);
} catch (Exception e) {
......@@ -88,7 +89,7 @@ public class SkinManager extends Observable {
*/
public void reset() {
SkinPreference.getInstance().reset();
SkinThemeUtils.SkinResources.getInstance().reset();
SkinResources.getInstance().reset();
setChanged();
notifyObservers();
}
......
......@@ -2,14 +2,8 @@ package com.miya.skin_lib.utils;
import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import androidx.core.content.res.ResourcesCompat;
/**
* 修改主题信息
......@@ -75,120 +69,4 @@ public class SkinThemeUtils {
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