一、要实现换肤首先要理清换肤的处理流程
1、当我们的主app需要自动换肤,就需要下载一个皮肤包,其实就是一个包含了各种资源的apk文件,这个apk文件里面定义了一套资源,这些资源的名字跟我们的主app里面的资源名字一样,例如主app的一个activity的xml的背景色是图片名字为android:background="@drawable/home_bg",那么我们在资源apk里面也会有相应的一个文件名字叫home_bg。
2、将资源文件里面的resId都替换给主app相同的resId,达到换肤的目的。
二、如何实现
1、创建资源apk很简单,就是build出来一个apk文件,将apk文件放入我们想放的路径就ok了。
2、开始替换,这时我们需要了解Android源码的执行过程以及资源的加载时机。
看下源码的加载流程:
在ActivityThread.java中的performLaunchActivity方法:从ActivityClientRecord中获取待启动的Activity的组件信息并通过类加载器生成Activity对象
ActivityInfo aInfo = r.activityInfo;
if (r.packageInfo == null) {
r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
Context.CONTEXT_INCLUDE_CODE);
}
ComponentName component = r.intent.getComponent();
if (component == null) {
component = r.intent.resolveActivity(
mInitialApplication.getPackageManager());
r.intent.setComponent(component);
}
if (r.activityInfo.targetActivity != null) {
component = new ComponentName(r.activityInfo.packageName,
r.activityInfo.targetActivity);
}
ContextImpl appContext = createBaseContextForActivity(r);
Activity activity = null;
try {
java.lang.ClassLoader cl = appContext.getClassLoader();
activity = mInstrumentation.newActivity(
cl, component.getClassName(), r.intent);
....
接着开始创建Application、拿到windowd对象并且通过activity的attach的方法跟Activity绑定
try {
Application app = r.packageInfo.makeApplication(false, mInstrumentation);
if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
if (localLOGV) Slog.v(
TAG, r + ": app=" + app
+ ", appName=" + app.getPackageName()
+ ", pkg=" + r.packageInfo.getPackageName()
+ ", comp=" + r.intent.getComponent().toShortString()
+ ", dir=" + r.packageInfo.getAppDir());
if (activity != null) {
CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Configuration config = new Configuration(mCompatConfiguration);
if (r.overrideConfig != null) {
config.updateFrom(r.overrideConfig);
}
if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
+ r.activityInfo.name + " with config " + config);
Window window = null;
if (r.mPendingRemoveWindow != null && r.mPreserveWindow) {
window = r.mPendingRemoveWindow;
r.mPendingRemoveWindow = null;
r.mPendingRemoveWindowManager = null;
}
appContext.setOuterContext(activity);
activity.attach(appContext, this, getInstrumentation(), r.token,
r.ident, app, r.intent, r.activityInfo, title, r.parent,
r.embeddedID, r.lastNonConfigurationInstances, config,
r.referrer, r.voiceInteractor, window, r.configCallback);
查看attach的方法发现在此方法里面生成了一个PhoneWindow对象
final void attach(Context context, ActivityThread aThread,
Instrumentation instr, IBinder token, int ident,
Application application, Intent intent, ActivityInfo info,
CharSequence title, Activity parent, String id,
NonConfigurationInstances lastNonConfigurationInstances,
Configuration config, String referrer, IVoiceInteractor voiceInteractor,
Window window, ActivityConfigCallback activityConfigCallback) {
attachBaseContext(context);
mFragments.attachHost(null /*parent*/);
mWindow = new PhoneWindow(this, window, activityConfigCallback);
mWindow.setWindowControllerCallback(this);
mWindow.setCallback(this);
mWindow.setOnWindowDismissedCallback(this);
mWindow.getLayoutInflater().setPrivateFactory(this);
PhoneWindow里面有我们熟悉的setContentView方法
@Override
public void setContentView(int layoutResID) {
// Note: FEATURE_CONTENT_TRANSITIONS may be set in the process of installing the window
// decor, when theme attributes and the like are crystalized. Do not check the feature
// before this happens.
if (mContentParent == null) {
installDecor();
} else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
mContentParent.removeAllViews();
}
if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,
getContext());
transitionTo(newScene);
} else {
mLayoutInflater.inflate(layoutResID, mContentParent);
}
mContentParent.requestApplyInsets();
final Callback cb = getCallback();
if (cb != null && !isDestroyed()) {
cb.onContentChanged();
}
mContentParentExplicitlySet = true;
}
setContentView中主要的代码是LayoutInflater的inflate方法
mLayoutInflater.inflate(layoutResID, mContentParent);
我们看看inflate方法方法干了啥
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
...
final View temp = createViewFromTag(root, name, inflaterContext, attrs);
ViewGroup.LayoutParams params = null;
if (root != null) {
if (DEBUG) {
System.out.println("Creating params from root: " +
root);
}
// Create layout params that match root, if supplied
params = root.generateLayoutParams(attrs);
if (!attachToRoot) {
// Set the layout params for temp if we are not
// attaching. (If we are, we use addView, below)
temp.setLayoutParams(params);
}
}
if (DEBUG) {
System.out.println("-----> start inflating children");
}
// Inflate all children under temp against its context.
rInflateChildren(parser, temp, attrs, true);
...
}
该方法中的一个重要方法时createViewFromTag,此方法也在rInflateChildren中递归调用。createViewFromTag方法的作用就是通过xml的布局创建生成view的对象,例如我们的一个xml文件是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@drawable/test_skin_bg"
android:orientation="vertical">
<Button
android:id="@+id/btn_change_skin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="点击换肤" />
<Button
android:id="@+id/btn_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="还原" />
<Button
android:id="@+id/btn_newactivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="去换肤页面" />
</LinearLayout>
那么在rInflateChildren中就会执行四次createViewFromTag生成一个LinearLayout 和三个Button的view对象,再看下createViewFromTag具体干了啥:
View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,
boolean ignoreThemeAttr) {
if (name.equals("view")) {
name = attrs.getAttributeValue(null, "class");
}
// Apply a theme wrapper, if allowed and one is specified.
if (!ignoreThemeAttr) {
final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME);
final int themeResId = ta.getResourceId(0, 0);
if (themeResId != 0) {
context = new ContextThemeWrapper(context, themeResId);
}
ta.recycle();
}
if (name.equals(TAG_1995)) {
// Let's party like it's 1995!
return new BlinkLayout(context, attrs);
}
try {
View view;
if (mFactory2 != null) {
view = mFactory2.onCreateView(parent, name, context, attrs);
} else if (mFactory != null) {
view = mFactory.onCreateView(name, context, attrs);
} else {
view = null;
}
if (view == null && mPrivateFactory != null) {
view = mPrivateFactory.onCreateView(parent, name, context, attrs);
}
if (view == null) {
final Object lastContext = mConstructorArgs[0];
mConstructorArgs[0] = context;
try {
if (-1 == name.indexOf('.')) {
view = onCreateView(parent, name, attrs);
} else {
view = createView(name, null, attrs);
}
} finally {
mConstructorArgs[0] = lastContext;
}
}
return view;
} catch (InflateException e) {
throw e;
} catch (ClassNotFoundException e) {
final InflateException ie = new InflateException(attrs.getPositionDescription()
+ ": Error inflating class " + name, e);
ie.setStackTrace(EMPTY_STACK_TRACE);
throw ie;
} catch (Exception e) {
final InflateException ie = new InflateException(attrs.getPositionDescription()
+ ": Error inflating class " + name, e);
ie.setStackTrace(EMPTY_STACK_TRACE);
throw ie;
}
}
我们看到view的创建过程了在try catch里面,看到这里我们就能想到了, 如果我们重写Factory2并且赋值给LayoutInflater,那么就会执行我们自己的view创建过程,在这里面我们可以记录下来每一个view的attribute
并且在需要的时候改变view的属性值。
我们可以这样来实现:
public class SkinFactoryLayoutInflaterFactory implements LayoutInflater.Factory2, Observer {
//如果我们的控件是sdk的,那么在view通过反射生成对象的时候 我们需要拼接上前缀 例如android.widget.Button, 在xml中我们只会写Button
private static final String[] mViewClassPrefix = new String[]{
"android.widget.",
"android.webkit.",
"android.app.",
"android.view."
};
private static final Class<?>[] mConstructorSignature = new Class[]{
Context.class, AttributeSet.class};
private static final HashMap<String, Constructor<? extends View>> sConstructorMap = new HashMap<>();
private SkinAttribute skinAttribute;
private Activity activity;
public SkinFactoryLayoutInflaterFactory(Activity activity) {
this.activity = activity;
skinAttribute = new SkinAttribute();
}
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
View view = createSDKDefaultView(context, name, attrs);
if (view == null) {
view = createView(context, name, attrs);
}
if (view != null) {
skinAttribute.findAttribute(view, attrs);
}
return view;
}
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
return null;
}
private View createSDKDefaultView(Context context, String name, AttributeSet attrs) {
if (name.contains(".")) {
return null;
}
for (int i = 0; i < mViewClassPrefix.length; i++) {
View view = createView(context, mViewClassPrefix[i] + name, attrs);
if (view != null) {
return view;
}
}
return null;
}
private View createView(Context context, String name, AttributeSet attrs) {
Constructor<? extends View> constructor = findViewConstructor(context, name);
try {
View view = constructor.newInstance(context, attrs);
return view;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
private Constructor<? extends View> findViewConstructor(Context context, String name) {
Constructor<? extends View> constructor = sConstructorMap.get(name);
Class<? extends View> clazz;
try {
if (constructor == null) {
clazz = context.getClassLoader().loadClass(name).asSubclass(View.class);
constructor = clazz.getConstructor(mConstructorSignature);
constructor.setAccessible(true);
sConstructorMap.put(name, constructor);
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
return constructor;
}
@Override
public void update(Observable o, Object arg) {
//当有一个activity界面触发换肤的时候,所有再栈内的activity都将会收到信息,并执行换肤
Log.e("Observable----->",activity.toString());
skinAttribute.applyAllSkin();
}
}
具体的文件代码可以参考:https://download.csdn.net/download/xuyanhu_jiayou/19891683