Commit 7c739eaf authored by gaodapeng's avatar gaodapeng

提交标准版

parent 1698d4bc
...@@ -2,7 +2,7 @@ package com.study.skindemo ...@@ -2,7 +2,7 @@ package com.study.skindemo
import android.os.Bundle import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import com.study.libs.SkinManager import com.miya.skin_lib.SkinManager
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import java.io.File import java.io.File
......
...@@ -2,7 +2,7 @@ package com.study.skindemo; ...@@ -2,7 +2,7 @@ package com.study.skindemo;
import android.app.Application; import android.app.Application;
import com.study.libs.SkinManager; import com.miya.skin_lib.SkinManager;
public class OneApplication extends Application { public class OneApplication extends Application {
......
apply plugin: 'com.android.library' apply plugin: 'com.android.library'
apply plugin: 'kotlin-android' apply plugin: "maven-publish"
apply plugin: 'kotlin-android-extensions' apply plugin: 'maven' //发布到maven 库中的插件
android { android {
compileSdkVersion 28 compileSdkVersion 28
...@@ -12,7 +12,6 @@ android { ...@@ -12,7 +12,6 @@ android {
versionCode 1 versionCode 1
versionName "1.0" versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro" consumerProguardFiles "consumer-rules.pro"
} }
...@@ -22,15 +21,44 @@ android { ...@@ -22,15 +21,44 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
} }
} }
android.libraryVariants.all { variant ->
variant.outputs.all {
outputFileName = 'skin-lib' + "-${defaultConfig.versionName}" + '.aar'
}
}
} }
dependencies { dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"]) 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' 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" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.study.libs"> package="com.miya.skin_lib">
/
</manifest> </manifest>
\ No newline at end of file
package com.study.libs; package com.miya.skin_lib;
import android.app.Activity; import android.app.Activity;
import android.app.Application; import android.app.Application;
...@@ -10,7 +10,7 @@ import androidx.annotation.NonNull; ...@@ -10,7 +10,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import androidx.core.view.LayoutInflaterCompat; 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.lang.reflect.Field;
import java.util.HashMap; import java.util.HashMap;
......
package com.study.libs; package com.miya.skin_lib;
import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
...@@ -9,8 +9,7 @@ import android.widget.TextView; ...@@ -9,8 +9,7 @@ import android.widget.TextView;
import androidx.core.view.ViewCompat; import androidx.core.view.ViewCompat;
import com.study.libs.utils.SkinResources; import com.miya.skin_lib.utils.SkinThemeUtils;
import com.study.libs.utils.SkinThemeUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -111,7 +110,7 @@ public class SkinAttribute { ...@@ -111,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 = SkinResources.getInstance().getBackground(skinPair Object background = SkinThemeUtils.SkinResources.getInstance().getBackground(skinPair
.resId); .resId);
//背景可能是 @color 也可能是 @drawable //背景可能是 @color 也可能是 @drawable
if (background instanceof Integer) { if (background instanceof Integer) {
...@@ -121,7 +120,7 @@ public class SkinAttribute { ...@@ -121,7 +120,7 @@ public class SkinAttribute {
} }
break; break;
case "src": case "src":
background = SkinResources.getInstance().getBackground(skinPair.resId); background = SkinThemeUtils.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));
...@@ -130,20 +129,20 @@ public class SkinAttribute { ...@@ -130,20 +129,20 @@ public class SkinAttribute {
} }
break; break;
case "textColor": case "textColor":
((TextView) view).setTextColor(SkinResources.getInstance().getColorStateList ((TextView) view).setTextColor(SkinThemeUtils.SkinResources.getInstance().getColorStateList
(skinPair.resId)); (skinPair.resId));
break; break;
case "drawableLeft": case "drawableLeft":
left = SkinResources.getInstance().getDrawable(skinPair.resId); left = SkinThemeUtils.SkinResources.getInstance().getDrawable(skinPair.resId);
break; break;
case "drawableTop": case "drawableTop":
top = SkinResources.getInstance().getDrawable(skinPair.resId); top = SkinThemeUtils.SkinResources.getInstance().getDrawable(skinPair.resId);
break; break;
case "drawableRight": case "drawableRight":
right = SkinResources.getInstance().getDrawable(skinPair.resId); right = SkinThemeUtils.SkinResources.getInstance().getDrawable(skinPair.resId);
break; break;
case "drawableBottom": case "drawableBottom":
bottom = SkinResources.getInstance().getDrawable(skinPair.resId); bottom = SkinThemeUtils.SkinResources.getInstance().getDrawable(skinPair.resId);
break; break;
default: default:
break; break;
......
package com.study.libs; package com.miya.skin_lib;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
...@@ -11,7 +11,7 @@ import androidx.annotation.NonNull; ...@@ -11,7 +11,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import com.study.libs.utils.SkinThemeUtils; import com.miya.skin_lib.utils.SkinThemeUtils;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.util.HashMap; import java.util.HashMap;
......
package com.study.libs; package com.miya.skin_lib;
import android.app.Application; import android.app.Application;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
...@@ -10,7 +10,7 @@ import android.text.TextUtils; ...@@ -10,7 +10,7 @@ import android.text.TextUtils;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import com.study.libs.utils.SkinResources; import com.miya.skin_lib.utils.SkinThemeUtils;
import java.io.File; import java.io.File;
import java.lang.reflect.Method; import java.lang.reflect.Method;
...@@ -30,7 +30,7 @@ public class SkinManager extends Observable { ...@@ -30,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());
SkinResources.init(application); SkinThemeUtils.SkinResources.init(application);
application.registerActivityLifecycleCallbacks(new ActivityLifeCycleCallback(this)); application.registerActivityLifecycleCallbacks(new ActivityLifeCycleCallback(this));
loadSkin(SkinPreference.getInstance().getSkin()); loadSkin(SkinPreference.getInstance().getSkin());
} }
...@@ -61,7 +61,7 @@ public class SkinManager extends Observable { ...@@ -61,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();
SkinResources.getInstance().reset(); SkinThemeUtils.SkinResources.getInstance().reset();
} else { } else {
try { try {
Resources appResource = application.getResources(); Resources appResource = application.getResources();
...@@ -72,7 +72,7 @@ public class SkinManager extends Observable { ...@@ -72,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);
SkinResources.getInstance().applySkin(skinResources, info.packageName); SkinThemeUtils.SkinResources.getInstance().applySkin(skinResources, info.packageName);
SkinPreference.getInstance().setSkin(skinPath); SkinPreference.getInstance().setSkin(skinPath);
} catch (Exception e) { } catch (Exception e) {
...@@ -88,7 +88,7 @@ public class SkinManager extends Observable { ...@@ -88,7 +88,7 @@ public class SkinManager extends Observable {
*/ */
public void reset() { public void reset() {
SkinPreference.getInstance().reset(); SkinPreference.getInstance().reset();
SkinResources.getInstance().reset(); SkinThemeUtils.SkinResources.getInstance().reset();
setChanged(); setChanged();
notifyObservers(); notifyObservers();
} }
......
package com.study.libs; package com.miya.skin_lib;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
......
package com.study.libs; package com.miya.skin_lib;
public interface SkinViewSupport { public interface SkinViewSupport {
......
package com.study.libs.utils; package com.miya.skin_lib.utils;
import android.content.Context; import android.content.Context;
import android.content.res.ColorStateList; import android.content.res.ColorStateList;
...@@ -10,7 +10,6 @@ import androidx.core.content.res.ResourcesCompat; ...@@ -10,7 +10,6 @@ import androidx.core.content.res.ResourcesCompat;
public class SkinResources { public class SkinResources {
//皮肤包的包名 //皮肤包的包名
private String skinPkgName; private String skinPkgName;
//是否使用默认的皮肤,用原始的内容 //是否使用默认的皮肤,用原始的内容
......
package com.study.libs.utils; 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;
/** /**
* 修改主题信息 * 修改主题信息
...@@ -70,4 +76,119 @@ public class SkinThemeUtils { ...@@ -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 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