Commit 7c739eaf authored by gaodapeng's avatar gaodapeng

提交标准版

parent 1698d4bc
......@@ -2,7 +2,7 @@ package com.study.skindemo
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.study.libs.SkinManager
import com.miya.skin_lib.SkinManager
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File
......
......@@ -2,7 +2,7 @@ package com.study.skindemo;
import android.app.Application;
import com.study.libs.SkinManager;
import com.miya.skin_lib.SkinManager;
public class OneApplication extends Application {
......
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "maven-publish"
apply plugin: 'maven' //发布到maven 库中的插件
android {
compileSdkVersion 28
......@@ -12,7 +12,6 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}
......@@ -22,15 +21,44 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
android.libraryVariants.all { variant ->
variant.outputs.all {
outputFileName = 'skin-lib' + "-${defaultConfig.versionName}" + '.aar'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
//设置动态属性
ext {
//发布到仓库用户名
publishUserName = "face-dev"
//发布到仓库地址
publishUserPassword = "Face-dev@2019"
//仓库地址
publishURL = "https://nexus.infra.miyatech.com/repository/shualianzhifu/"
publishVersion = 1.0
publishArtifactId = "skin-lib"
publishGroupId = 'com.miya.iot'
}
uploadArchives {
repositories.mavenDeployer {
repository(url: publishURL) {//RELEASE
authentication(userName: publishUserName, password: publishUserPassword)
}
pom.project {
version publishVersion
artifactId publishArtifactId
groupId publishGroupId
packaging 'aar'
description 'skin_lib'
}
}
}
\ No newline at end of file
package com.study.libs
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Assert.*
/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.study.libs.test", appContext.packageName)
}
}
\ No newline at end of file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.study.libs">
package="com.miya.skin_lib">
/
</manifest>
\ No newline at end of file
package com.study.libs;
package com.miya.skin_lib;
import android.app.Activity;
import android.app.Application;
......@@ -10,7 +10,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.view.LayoutInflaterCompat;
import com.study.libs.utils.SkinThemeUtils;
import com.miya.skin_lib.utils.SkinThemeUtils;
import java.lang.reflect.Field;
import java.util.HashMap;
......
package com.study.libs;
package com.miya.skin_lib;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
......@@ -9,8 +9,7 @@ import android.widget.TextView;
import androidx.core.view.ViewCompat;
import com.study.libs.utils.SkinResources;
import com.study.libs.utils.SkinThemeUtils;
import com.miya.skin_lib.utils.SkinThemeUtils;
import java.util.ArrayList;
import java.util.List;
......@@ -111,7 +110,7 @@ public class SkinAttribute {
Drawable left = null, top = null, right = null, bottom = null;
switch (skinPair.attributeName) {
case "background":
Object background = SkinResources.getInstance().getBackground(skinPair
Object background = SkinThemeUtils.SkinResources.getInstance().getBackground(skinPair
.resId);
//背景可能是 @color 也可能是 @drawable
if (background instanceof Integer) {
......@@ -121,7 +120,7 @@ public class SkinAttribute {
}
break;
case "src":
background = SkinResources.getInstance().getBackground(skinPair.resId);
background = SkinThemeUtils.SkinResources.getInstance().getBackground(skinPair.resId);
if (background instanceof Integer) {
((ImageView) view).setImageDrawable(new ColorDrawable((Integer)
background));
......@@ -130,20 +129,20 @@ public class SkinAttribute {
}
break;
case "textColor":
((TextView) view).setTextColor(SkinResources.getInstance().getColorStateList
((TextView) view).setTextColor(SkinThemeUtils.SkinResources.getInstance().getColorStateList
(skinPair.resId));
break;
case "drawableLeft":
left = SkinResources.getInstance().getDrawable(skinPair.resId);
left = SkinThemeUtils.SkinResources.getInstance().getDrawable(skinPair.resId);
break;
case "drawableTop":
top = SkinResources.getInstance().getDrawable(skinPair.resId);
top = SkinThemeUtils.SkinResources.getInstance().getDrawable(skinPair.resId);
break;
case "drawableRight":
right = SkinResources.getInstance().getDrawable(skinPair.resId);
right = SkinThemeUtils.SkinResources.getInstance().getDrawable(skinPair.resId);
break;
case "drawableBottom":
bottom = SkinResources.getInstance().getDrawable(skinPair.resId);
bottom = SkinThemeUtils.SkinResources.getInstance().getDrawable(skinPair.resId);
break;
default:
break;
......
package com.study.libs;
package com.miya.skin_lib;
import android.app.Activity;
import android.content.Context;
......@@ -11,7 +11,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import com.study.libs.utils.SkinThemeUtils;
import com.miya.skin_lib.utils.SkinThemeUtils;
import java.lang.reflect.Constructor;
import java.util.HashMap;
......
package com.study.libs;
package com.miya.skin_lib;
import android.app.Application;
import android.content.pm.PackageInfo;
......@@ -10,7 +10,7 @@ import android.text.TextUtils;
import androidx.annotation.RequiresApi;
import com.study.libs.utils.SkinResources;
import com.miya.skin_lib.utils.SkinThemeUtils;
import java.io.File;
import java.lang.reflect.Method;
......@@ -30,7 +30,7 @@ public class SkinManager extends Observable {
private SkinManager(Application application) {
this.application = application;
SkinPreference.init(application.getApplicationContext());
SkinResources.init(application);
SkinThemeUtils.SkinResources.init(application);
application.registerActivityLifecycleCallbacks(new ActivityLifeCycleCallback(this));
loadSkin(SkinPreference.getInstance().getSkin());
}
......@@ -61,7 +61,7 @@ public class SkinManager extends Observable {
public void loadSkin(String skinPath) {
if (TextUtils.isEmpty(skinPath) && !new File(skinPath).exists()) {//如果为空或者不存在该文件,则不用管
SkinPreference.getInstance().reset();
SkinResources.getInstance().reset();
SkinThemeUtils.SkinResources.getInstance().reset();
} else {
try {
Resources appResource = application.getResources();
......@@ -72,7 +72,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);
SkinResources.getInstance().applySkin(skinResources, info.packageName);
SkinThemeUtils.SkinResources.getInstance().applySkin(skinResources, info.packageName);
SkinPreference.getInstance().setSkin(skinPath);
} catch (Exception e) {
......@@ -88,7 +88,7 @@ public class SkinManager extends Observable {
*/
public void reset() {
SkinPreference.getInstance().reset();
SkinResources.getInstance().reset();
SkinThemeUtils.SkinResources.getInstance().reset();
setChanged();
notifyObservers();
}
......
package com.study.libs;
package com.miya.skin_lib;
import android.content.Context;
import android.content.SharedPreferences;
......
package com.study.libs.utils;
package com.miya.skin_lib.utils;
import android.content.Context;
import android.content.res.ColorStateList;
......@@ -10,7 +10,6 @@ import androidx.core.content.res.ResourcesCompat;
public class SkinResources {
//皮肤包的包名
private String skinPkgName;
//是否使用默认的皮肤,用原始的内容
......
package com.study.libs.utils;
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;
/**
* 修改主题信息
......@@ -70,4 +76,119 @@ 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);
}
}
}
}
package com.study.libs
package com.miya.skin_lib
import org.junit.Test
......
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