Commit 47c1673e authored by gaodapeng's avatar gaodapeng

修改非androidx版本,增加maven脚本

parent ccfe73a2
apply plugin: 'com.android.application' apply plugin: 'com.android.application'
android { android {
compileSdkVersion 29 compileSdkVersion 28
buildToolsVersion "29.0.2" buildToolsVersion "28.0.3"
defaultConfig { defaultConfig {
applicationId "com.study.skindemo" applicationId "com.study.skindemo"
minSdkVersion 18 minSdkVersion 18
targetSdkVersion 29 targetSdkVersion 28
versionCode 1 versionCode 1
versionName "1.0" versionName "1.0"
...@@ -29,7 +29,7 @@ dependencies { ...@@ -29,7 +29,7 @@ dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"]) implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.android.support:support-v13:28.0.0" implementation "com.android.support:support-v13:28.0.0"
implementation 'com.google.android.material:material:1.2.1' implementation 'com.google.android.material:material:1.2.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2' implementation 'com.android.support.constraint:constraint-layout:2.0.4'
implementation project(':skin-lib') implementation project(':skin-lib')
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
......
package com.study.skindemo; package com.study.skindemo;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View; import android.view.View;
import androidx.annotation.RequiresApi;
import com.miya.skin_lib.SkinManager;
import com.study.libs.SkinManager;
import java.io.File; import java.io.File;
...@@ -18,6 +20,7 @@ public class MainActivity extends AppCompatActivity { ...@@ -18,6 +20,7 @@ public class MainActivity extends AppCompatActivity {
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
findViewById(R.id.btnCtl).setOnClickListener(new View.OnClickListener() { findViewById(R.id.btnCtl).setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override @Override
public void onClick(View v) { public void onClick(View v) {
SkinManager.getInstance() SkinManager.getInstance()
......
...@@ -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: "maven-publish"
apply plugin: 'maven' //发布到maven 库中的插件
def supportLibVersion = "28.0.0" def supportLibVersion = "28.0.0"
android { android {
...@@ -26,6 +28,34 @@ android { ...@@ -26,6 +28,34 @@ android {
dependencies { dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"]) implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.android.support:support-v13:${supportLibVersion}" implementation "com.android.support:support-v13:${supportLibVersion}"
testImplementation 'junit:junit:4.12' }
//设置动态属性
ext {
//发布到仓库用户名
publishUserName = "face-dev"
//发布到仓库地址
publishUserPassword = "Face-dev@2019"
//仓库地址
publishURL = "https://nexus.infra.miyatech.com/repository/shualianzhifu/"
publishVersion = '1.0'
//skin-lib-compat表示兼容之前版本,skin-lib是androidx版本的
publishArtifactId = "skin-lib-compat"
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
<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;
...@@ -7,7 +7,7 @@ import android.support.annotation.NonNull; ...@@ -7,7 +7,7 @@ import android.support.annotation.NonNull;
import android.support.v4.view.LayoutInflaterCompat; import android.support.v4.view.LayoutInflaterCompat;
import android.view.LayoutInflater; import android.view.LayoutInflater;
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;
...@@ -8,8 +8,7 @@ import android.view.View; ...@@ -8,8 +8,7 @@ import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
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;
...@@ -110,7 +109,7 @@ public class SkinAttribute { ...@@ -110,7 +109,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) {
...@@ -120,7 +119,7 @@ public class SkinAttribute { ...@@ -120,7 +119,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));
...@@ -129,20 +128,20 @@ public class SkinAttribute { ...@@ -129,20 +128,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;
...@@ -10,7 +10,7 @@ import android.util.AttributeSet; ...@@ -10,7 +10,7 @@ import android.util.AttributeSet;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
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;
...@@ -9,7 +9,7 @@ import android.os.Build; ...@@ -9,7 +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.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;
...@@ -29,7 +29,7 @@ public class SkinManager extends Observable { ...@@ -29,7 +29,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());
} }
...@@ -60,7 +60,7 @@ public class SkinManager extends Observable { ...@@ -60,7 +60,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();
...@@ -71,7 +71,7 @@ public class SkinManager extends Observable { ...@@ -71,7 +71,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) {
...@@ -87,7 +87,7 @@ public class SkinManager extends Observable { ...@@ -87,7 +87,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 android.text.TextUtils; ...@@ -10,7 +10,6 @@ import android.text.TextUtils;
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.support.v4.content.res.ResourcesCompat;
import android.text.TextUtils;
/** /**
* 修改主题信息 * 修改主题信息
...@@ -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