android外掛式換膚核心實現
阿新 • • 發佈:2019-02-15
本文思路來源於騰訊課堂,在此記錄與大家分享並記錄後用
- 在setContentView之前對view進行攔截
@Override
protected void onCreate(Bundle savedInstanceState) {
beforeContentView();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void beforeContentView() {
LayoutInflater layoutInflater = LayoutInflater.from(this );
LayoutInflaterCompat.setFactory2(layoutInflater, this);
//這個方法已過時(API23以下使用)
// LayoutInflaterCompat.setFactory(layoutInflater, new LayoutInflaterFactory() {
// @Override
// public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
// //攔截到View的建立
// //參照AppCompatViewInflater中
// if (TextUtils.equals(name, "TextView")) {
// Button button = new Button(MainActivity.this);
// button.setText("攔截了");
// return button;
// }
// return null;
// }
// });
}
- 重新onCreateView方法(copy原始碼)
private SkinAppCompatViewInflater mAppCompatViewInflater;
private static final boolean IS_PRE_LOLLIPOP = Build.VERSION.SDK_INT < 21;
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
//攔截到View的建立 獲取View之後要去解析
//1.建立View
View view = createView(parent, name, context, attrs);
return view;
}
public View createView(View parent, final String name, @NonNull Context context,
@NonNull AttributeSet attrs) {
if (mAppCompatViewInflater == null) {
mAppCompatViewInflater = new SkinAppCompatViewInflater();
}
boolean inheritContext = false;
if (IS_PRE_LOLLIPOP) {
inheritContext = (attrs instanceof XmlPullParser)
// If we have a XmlPullParser, we can detect where we are in the layout
? ((XmlPullParser) attrs).getDepth() > 1
// Otherwise we have to use the old heuristic
: shouldInheritContext((ViewParent) parent);
}
return mAppCompatViewInflater.createView(parent, name, context, attrs, inheritContext,
IS_PRE_LOLLIPOP, /* Only read android:theme pre-L (L+ handles this anyway) */
true, /* Read read app:theme as a fallback at all times for legacy reasons */
VectorEnabledTintResources.shouldBeUsed() /* Only tint wrap the context if enabled */
);
}
private boolean shouldInheritContext(ViewParent parent) {
if (parent == null) {
// The initial parent is null so just return false
return false;
}
final View windowDecor = getWindow().getDecorView();
while (true) {
if (parent == null) {
// Bingo. We've hit a view which has a null parent before being terminated from
// the loop. This is (most probably) because it's the root view in an inflation
// call, therefore we should inherit. This works as the inflated layout is only
// added to the hierarchy at the end of the inflate() call.
return true;
} else if (parent == windowDecor || !(parent instanceof View)
|| ViewCompat.isAttachedToWindow((View) parent)) {
// We have either hit the window's decor view, a parent which isn't a View
// (i.e. ViewRootImpl), or an attached view, so we know that the original parent
// is currently added to the view hierarchy. This means that it has not be
// inflated in the current inflate() call and we should not inherit the context.
return false;
}
parent = parent.getParent();
}
}
- 參照系統實現SkinAppCompatViewInflater類
package tsou.cn.hookinterceptview.skin.support;
/**
* Created by Administrator on 2018/8/3 0003.
*/
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.TypedArray;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.util.ArrayMap;
import android.support.v4.view.ViewCompat;
import android.support.v7.appcompat.R;
import android.support.v7.view.ContextThemeWrapper;
import android.support.v7.widget.AppCompatAutoCompleteTextView;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.AppCompatCheckedTextView;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatImageButton;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatMultiAutoCompleteTextView;
import android.support.v7.widget.AppCompatRadioButton;
import android.support.v7.widget.AppCompatRatingBar;
import android.support.v7.widget.AppCompatSeekBar;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.TintContextWrapper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.InflateException;
import android.view.View;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
/**
* This class is responsible for manually inflating our tinted widgets which are used on devices
* running {@link android.os.Build.VERSION_CODES#KITKAT KITKAT} or below. As such, this class
* should only be used when running on those devices.
* <p>This class two main responsibilities: the first is to 'inject' our tinted views in place of
* the framework versions in layout inflation; the second is backport the {@code android:theme}
* functionality for any inflated widgets. This include theme inheritance from its parent.
*/
public class SkinAppCompatViewInflater {
private static final Class<?>[] sConstructorSignature = new Class[]{
Context.class, AttributeSet.class};
private static final int[] sOnClickAttrs = new int[]{android.R.attr.onClick};
private static final String[] sClassPrefixList = {
"android.widget.",
"android.view.",
"android.webkit."
};
private static final String LOG_TAG = "AppCompatViewInflater";
private static final Map<String, Constructor<? extends View>> sConstructorMap
= new ArrayMap<>();
private final Object[] mConstructorArgs = new Object[2];
public final View createView(View parent, final String name, @NonNull Context context,
@NonNull AttributeSet attrs, boolean inheritContext,
boolean readAndroidTheme, boolean readAppTheme, boolean wrapContext) {
final Context originalContext = context;
// We can emulate Lollipop's android:theme attribute propagating down the view hierarchy
// by using the parent's context
if (inheritContext && parent != null) {
context = parent.getContext();
}
if (readAndroidTheme || readAppTheme) {
// We then apply the theme on the context, if specified
context = themifyContext(context, attrs, readAndroidTheme, readAppTheme);
}
if (wrapContext) {
context = TintContextWrapper.wrap(context);
}
View view = null;
// We need to 'inject' our tint aware Views in place of the standard framework versions
switch (name) {
case "TextView":
view = new AppCompatTextView(context, attrs);
break;
case "ImageView":
view = new AppCompatImageView(context, attrs);
break;
case "Button":
view = new AppCompatButton(context, attrs);
break;
case "EditText":
view = new AppCompatEditText(context, attrs);
break;
case "Spinner":
view = new AppCompatSpinner(context, attrs);
break;
case "ImageButton":
view = new AppCompatImageButton(context, attrs);
break;
case "CheckBox":
view = new AppCompatCheckBox(context, attrs);
break;
case "RadioButton":
view = new AppCompatRadioButton(context, attrs);
break;
case "CheckedTextView":
view = new AppCompatCheckedTextView(context, attrs);
break;
case "AutoCompleteTextView":
view = new AppCompatAutoCompleteTextView(context, attrs);
break;
case "MultiAutoCompleteTextView":
view = new AppCompatMultiAutoCompleteTextView(context, attrs);
break;
case "RatingBar":
view = new AppCompatRatingBar(context, attrs);
break;
case "SeekBar":
view = new AppCompatSeekBar(context, attrs);
break;
}
if (view == null && originalContext != context) {
// If the original context does not equal our themed context, then we need to manually
// inflate it using the name so that android:theme takes effect.
view = createViewFromTag(context, name, attrs);
}
if (view != null) {
// If we have created a view, check its android:onClick
checkOnClickListener(view, attrs);
}
return view;
}
private View createViewFromTag(Context context, String name, AttributeSet attrs) {
if (name.equals("view")) {
name = attrs.getAttributeValue(null, "class");
}
try {
mConstructorArgs[0] = context;
mConstructorArgs[1] = attrs;
if (-1 == name.indexOf('.')) {
for (int i = 0; i < sClassPrefixList.length; i++) {
final View view = createView(context, name, sClassPrefixList[i]);
if (view != null) {
return view;
}
}
return null;
} else {
return createView(context, name, null);
}
} catch (Exception e) {
// We do not want to catch these, lets return null and let the actual LayoutInflater
// try
return null;
} finally {
// Don't retain references on context.
mConstructorArgs[0] = null;
mConstructorArgs[1] = null;
}
}
/**
* android:onClick doesn't handle views with a ContextWrapper context. This method
* backports new framework functionality to traverse the Context wrappers to find a
* suitable target.
*/
private void checkOnClickListener(View view, AttributeSet attrs) {
final Context context = view.getContext();
if (!(context instanceof ContextWrapper) ||
(Build.VERSION.SDK_INT >= 15 && !ViewCompat.hasOnClickListeners(view))) {
// Skip our compat functionality if: the Context isn't a ContextWrapper, or
// the view doesn't have an OnClickListener (we can only rely on this on API 15+ so
// always use our compat code on older devices)
return;
}
final TypedArray a = context.obtainStyledAttributes(attrs, sOnClickAttrs);
final String handlerName = a.getString(0);
if (handlerName != null) {
view.setOnClickListener(new SkinAppCompatViewInflater.DeclaredOnClickListener(view, handlerName));
}
a.recycle();
}
private View createView(Context context, String name, String prefix)
throws ClassNotFoundException, InflateException {
Constructor<? extends View> constructor = sConstructorMap.get(name);
try {
if (constructor == null) {
// Class not found in the cache, see if it's real, and try to add it
Class<? extends View> clazz = context.getClassLoader().loadClass(
prefix != null ? (prefix + name) : name).asSubclass(View.class);
constructor = clazz.getConstructor(sConstructorSignature);
sConstructorMap.put(name, constructor);
}
constructor.setAccessible(true);
return constructor.newInstance(mConstructorArgs);
} catch (Exception e) {
// We do not want to catch these, lets return null and let the actual LayoutInflater
// try
return null;
}
}
/**
* Allows us to emulate the {@code android:theme} attribute for devices before L.
*/
private static Context themifyContext(Context context, AttributeSet attrs,
boolean useAndroidTheme, boolean useAppTheme) {
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.View, 0, 0);
int themeId = 0;
if (useAndroidTheme) {
// First try reading android:theme if enabled
themeId = a.getResourceId(R.styleable.View_android_theme, 0);
}
if (useAppTheme && themeId == 0) {
// ...if that didn't work, try reading app:theme (for legacy reasons) if enabled
themeId = a.getResourceId(R.styleable.View_theme, 0);
if (themeId != 0) {
Log.i(LOG_TAG, "app:theme is now deprecated. "
+ "Please move to using android:theme instead.");
}
}
a.recycle();
if (themeId != 0 && (!(context instanceof ContextThemeWrapper)
|| ((ContextThemeWrapper) context).getThemeResId() != themeId)) {
// If the context isn't a ContextThemeWrapper, or it is but does not have
// the same theme as we need, wrap it in a new wrapper
context = new ContextThemeWrapper(context, themeId);
}
return context;
}
/**
* An implementation of OnClickListener that attempts to lazily load a
* named click handling method from a parent or ancestor context.
*/
private static class DeclaredOnClickListener implements View.OnClickListener {
private final View mHostView;
private final String mMethodName;
private Method mResolvedMethod;
private Context mResolvedContext;
public DeclaredOnClickListener(@NonNull View hostView, @NonNull String methodName) {
mHostView = hostView;
mMethodName = methodName;
}
@Override
public void onClick(@NonNull View v) {
if (mResolvedMethod == null) {
resolveMethod(mHostView.getContext(), mMethodName);
}
try {
mResolvedMethod.invoke(mResolvedContext, v);
} catch (IllegalAccessException e) {
throw new IllegalStateException(
"Could not execute non-public method for android:onClick", e);
} catch (InvocationTargetException e) {
throw new IllegalStateException(
"Could not execute method for android:onClick", e);
}
}
@NonNull
private void resolveMethod(@Nullable Context context, @NonNull String name) {
while (context != null) {
try {
if (!context.isRestricted()) {
final Method method = context.getClass().getMethod(mMethodName, View.class);
if (method != null) {
mResolvedMethod = method;
mResolvedContext = context;
return;
}
}
} catch (NoSuchMethodException e) {
// Failed to find method, keep searching up the hierarchy.
}
if (context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
} else {
// Can't search up the hierarchy, null out and fail.
context = null;
}
}
final int id = mHostView.getId();
final String idText = id == View.NO_ID ? "" : " with id '"
+ mHostView.getContext().getResources().getResourceEntryName(id) + "'";
throw new IllegalStateException("Could not find method " + mMethodName
+ "(View) in a parent or ancestor Context for android:onClick "
+ "attribute defined on view " + mHostView.getClass() + idText);
}
}
}
- 解析屬性
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
//攔截到View的建立 獲取View之後要去解析
//1.建立View
View view = createView(parent, name, context, attrs);
//2.解析屬性 src textColor background 自定義屬性
Log.d("huangxiaoguo", view + "----------");
if (view != null) {
List<SkinAttr> skinAttrs = SkinAttrSupport.getSkinAttrs(context, attrs);
}
return view;
}
- 面板屬性解析的支援類
/**
* Created by Administrator on 2018/8/3 0003.
* 面板屬性解析的支援類
*/
public class SkinAttrSupport {
/**
* 獲取SkinAttr的屬性
*
* @param context
* @param attrs
* @return
*/
public static List<SkinAttr> getSkinAttrs(Context context, AttributeSet attrs) {
// background src textColor
ArrayList<SkinAttr> skinAttrs = new ArrayList<>();
int attributeCount = attrs.getAttributeCount();
for (int i = 0; i < attributeCount; i++) {
//獲取名稱和值
String attrName = attrs.getAttributeName(i);
String attrValue = attrs.getAttributeValue(i);
Log.d("huangxiaoguo", "attrName--->" + attrName);
Log.d("huangxiaoguo", "attriValue--->" + attrValue);
//獲取需要的資源
SkinType skinType = getSkinType(attrName);
if (skinType != null) {
//資源名稱
String resName = geteResName(context, attrValue);
if (TextUtils.isEmpty(resName)) {
continue;
}
SkinAttr skinAttr = new SkinAttr(resName, skinType);
skinAttrs.add(skinAttr);
}
}
return skinAttrs;
}
/**
* 獲取資源名稱
*
* @param context
* @param attrValue
* @return
*/
private static String geteResName(Context context, String attrValue) {
if (attrValue.startsWith("@")) {
attrValue = attrValue.substring(1);
int resId = Integer.parseInt(attrValue);
return context.getResources().getResourceEntryName(resId);
}
return null;
}
/**
* 通過名稱獲取SkinType
*
* @param attrName
* @return
*/
private static SkinType getSkinType(String attrName) {
SkinType[] skinTypes = SkinType.values();
for (SkinType skinType : skinTypes) {
if (skinType.getResName().equals(attrName)) {
return skinType;
}
}
return null;
}
}
- SkinManager管理
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
//攔截到View的建立 獲取View之後要去解析
//1.建立View
View view = createView(parent, name, context, attrs);
//2.解析屬性 src textColor background 自定義屬性
Log.d("huangxiaoguo", view + "----------");
if (view != null) {
List<SkinAttr> skinAttrs = SkinAttrSupport.getSkinAttrs(context, attrs);
SkinView skinView = new SkinView(view, skinAttrs);
//3.統一交給SkinManager管理
managerSkinView(skinView);
}
return view;
}
/**
* 統一管理SkinView
*
* @param skinView
*/
protected void managerSkinView(SkinView skinView) {
List<SkinView> skinViews = SkinManager.getInstance().getSkinViews(this);
if (skinViews == null) {
skinViews = new ArrayList<>();
SkinManager.getInstance().register(this, skinViews);
}
skinViews.add(skinView);
}
- 面板管理類
/**
* Created by Administrator on 2018/8/3 0003.
* 面板管理類
*/
public class SkinManager {
private static SkinManager mInstance;
private Context mContext;
private Map<Activity, List<SkinView>> mSkinViews = new HashMap<>();
private SkinResource mSkinResource;
static {
mInstance = new SkinManager();
}
public static SkinManager getInstance() {
return mInstance;
}
public void init(Context context) {
this.mContext = context.getApplicationContext();
}
/**
* 載入面板
*
* @param skinPath
* @return
*/
public int loadSkin(String skinPath) {
//初始化資源管理
mSkinResource = new SkinResource(mContext, skinPath);
//改變面板
Set<Activity> keys = mSkinViews.keySet();
for (Activity key : keys) {
List<SkinView> skinViews = mSkinViews.get(key);
for (SkinView skinView : skinViews) {
skinView.skin();
}
}
return 0;
}
/**
* 恢復預設
*
* @return
*/
public int restoreDefault() {
return 0;
}
/**
* 獲取SkinView
*
* @param activity
* @return
*/
public List<SkinView> getSkinViews(Activity activity) {
return mSkinViews.get(activity);
}
/**
* 註冊
*
* @param activity
* @param skinViews
*/
public void register(Activity activity, List<SkinView> skinViews) {
mSkinViews.put(activity, skinViews);
}
/**
* 獲取當前面板資源
* @return
*/
public SkinResource getSkinResource() {
return mSkinResource;
}
}
- 面板資源管理
/**
* Created by Administrator on 2018/8/3 0003.
* 面板資源管理
*/
public class SkinResource {
//資源獲取
private Resources mSkinResource;
private String mPackageName;
public SkinResource(Context context, String skinPath) {
try {
//讀取本地資源
Resources superRes = context.getResources();
//建立AssetManager
AssetManager asset = AssetManager.class.newInstance();
//新增本地下載好的資源面板 Native層
Method method = AssetManager.class.getDeclaredMethod("addAssetPath", String.class);
//允許方法私有
method.setAccessible(true);
//反射方法
method.invoke(asset, Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + "red.skin");
mSkinResource = new Resources(asset, superRes.getDisplayMetrics(), superRes.getConfiguration());
//獲取報名
mPackageName = context.getPackageManager().getPackageArchiveInfo(skinPath, PackageManager.GET_ACTIVITIES)
.packageName;
Log.d("huangxiaoguo", "mPackageName===>" + mPackageName);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 通過名字獲取Drawable
*
* @param resName
* @return
*/
public Drawable getDrawableByName(String resName) {
try {
int resId = mSkinResource.getIdentifier(resName, "drawable", mPackageName);
Log.d("huangxiaoguo", "drawable===>" + resId);
Drawable drawable = mSkinResource.getDrawable(resId);
return drawable;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 通過名字獲取顏色
*
* @param resName
* @return
*/
public ColorStateList getColorByName(String resName) {
try {
int resId = mSkinResource.getIdentifier(resName, "color", mPackageName);
Log.d("huangxiaoguo", "color===>" + resId);
ColorStateList color = mSkinResource.getColorStateList(resId);
return color;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
- 封裝View和屬性bean
public class SkinView {
private View mView;
private List<SkinAttr> mAttrs;
public SkinView(View view, List<SkinAttr> attrs) {
this.mView = view;
this.mAttrs = attrs;
}
public void skin() {
for (SkinAttr attr : mAttrs) {
attr.skin(mView);
}
}
}
- 封裝包名和要替換的內容
public class SkinAttr {
private String mResName;
private SkinType mType;
public SkinAttr(String resName, SkinType skinType) {
this.mResName=resName;
this.mType=skinType;
}
public void skin(View view) {
mType.skin(view, mResName);
}
}
- 獲取想要的資源
/**
* Created by Administrator on 2018/8/3 0003.
* 獲取想要的資源
*/
public enum SkinType {
TEXT_COLOR("textColor") {
@Override
public void skin(View view, String resName) {
SkinResource skinResource = getSkinResource();
ColorStateList color = skinResource.getColorByName(resName);
if (color == null) {
return;
}
TextView textView = (TextView) view;
textView.setTextColor(color);
}
},
BACKGROUND("background") {
@SuppressLint("NewApi")
@Override
public void skin(View view, String resName) {
//背景可能是圖片也可能是顏色
SkinResource skinResource = getSkinResource();
Drawable drawable = skinResource.getDrawableByName(resName);
if (drawable != null) {
view.setBackground(drawable);
return;
}
//顏色
ColorStateList color = skinResource.getColorByName(resName);
if (color != null) {
view.setBackgroundTintList(color);
}
}
},
SRC("src") {
@Override
public void skin(View view, String resName) {
//獲取資源
SkinResource skinResource = getSkinResource();
Log.d("huangxiaoguo", "srcName--->" + resName);
Drawable drawable = skinResource.getDrawableByName(resName);
if (drawable != null) {
ImageView imageView = (ImageView) view;
imageView.setImageDrawable(drawable);
return;
}
}
};
//會根據名字調對應的方法
private String mResName;
SkinType(String resName) {
this.mResName = resName;
}
public abstract void skin(View view, String resName);
public String getResName() {
return mResName;
}
public SkinResource getSkinResource() {
return SkinManager.getInstance().getSkinResource();
}
}
- 點選換膚
/**
* 點選換膚
*/
public void skin(View view) {
String skinPath = Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator + "red.skin";
int result = SkinManager.getInstance().loadSkin(skinPath);
}
重新建立專案,編寫一套資原始檔,替換color,drawable等等資源,打包更改apk名稱為red.skin放進相應的目錄下即可
補充
如何攔截佈局(以LinearLayout為例)
在SkinAppCompatViewInflater中 createView新增
case "LinearLayout":
view = new LinearLayout(context, attrs);
break;
- 如果資源包放在SD卡中請申請許可權
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>