「Update」

1、换肤SDK兼容 targetSdkVersion >= 29
2、优化代码结构
This commit is contained in:
donghongyu
2024-10-23 11:42:35 +08:00
parent 37ebe37a25
commit 80578feeca
9 changed files with 230 additions and 207 deletions

View File

@@ -1,27 +1,18 @@
package com.mogo.skin;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.core.view.ViewCompat;
import com.mogo.skin.utils.SkinResources;
import com.mogo.skin.utils.SkinThemeUtils;
import java.util.ArrayList;
import java.util.List;
/**
* donghongyu
* @author donghongyu
* @description : 属性集合 需要针对哪些属性进行替换
*/
public class SkinAttribute {
@@ -44,16 +35,19 @@ public class SkinAttribute {
mAttributes.add("hint");
}
private Typeface typeface;
private static final String TAG = "SkinAttribute";
/**
* 记录换肤需要操作的View
*/
List<SkinView> mSkinViews = new ArrayList<>();
public SkinAttribute(Typeface typeface) {
this.typeface = typeface;
public SkinAttribute() {
}
/**
* 记录View中哪几个属性需要换肤,找到当前页面的所有属性
*/
public void load(View view, AttributeSet attrs) {
List<SkinPair> skinPairs = new ArrayList<>();
for (int i = 0; i < attrs.getAttributeCount(); i++) {
@@ -61,13 +55,18 @@ public class SkinAttribute {
String attributeName = attrs.getAttributeName(i);
//是否符合 需要筛选的属性名
if (mAttributes.contains(attributeName)) {
//属性名写法
//#ffff
//?attr/xxx
//@string/xxx
String attributeValue = attrs.getAttributeValue(i);
//写死了,不管了
//如果是写死的字符串 #fffff
if (attributeValue.startsWith("#")) {
continue;
}
//资源id
int resId = 0;
//以?开头的表示用属性
if (attributeValue.startsWith("?")) {
//attr Id
int attrId = Integer.parseInt(attributeValue.substring(1));
@@ -75,6 +74,7 @@ public class SkinAttribute {
resId = SkinThemeUtils.getResId(view.getContext(), new int[]{attrId})[0];
} else {
try {
// 以@开头的
// @12343455332
String resIdStr = attributeValue.substring(1);
if (isNumeric(resIdStr)) {
@@ -95,8 +95,9 @@ public class SkinAttribute {
//将View与之对应的可以动态替换的属性集合 放入 集合中
if (!skinPairs.isEmpty() || view instanceof TextView || view instanceof SkinViewSupport) {
//选择皮肤 更新
SkinView skinView = new SkinView(view, skinPairs);
skinView.applySkin(typeface);
skinView.applySkin();
mSkinViews.add(skinView);
}
}
@@ -111,169 +112,9 @@ public class SkinAttribute {
/**
* 换皮肤
*/
public void applySkin(Typeface typeface) {
public void applySkin() {
for (SkinView mSkinView : mSkinViews) {
mSkinView.applySkin(typeface);
mSkinView.applySkin();
}
}
static class SkinView {
View view;
List<SkinPair> skinPairs;
public SkinView(View view, List<SkinPair> skinPairs) {
this.view = view;
this.skinPairs = skinPairs;
}
/**
* @param typeface 字体
*/
public void applySkin(Typeface typeface) {
//Log.d(TAG, "applySkin() called with: typeface = [" + typeface + "] view = [" + view + "] Parent = " + view.getParent());
//applySkinTypeface(typeface);
applySkinViewSupport();
try {
for (SkinPair skinPair : skinPairs) {
Drawable left = null, top = null, right = null, bottom = null;
String textStr;
switch (skinPair.attributeName) {
case "background":
Object background = SkinResources.getInstance().getBackground(skinPair.resId);
//Color
if (background instanceof Integer) {
view.setBackgroundColor((Integer) background);
} else {
ViewCompat.setBackground(view, (Drawable) background);
}
break;
case "src":
try {
if (view instanceof ImageView) {
background = SkinResources.getInstance().getBackground(skinPair.resId);
if (background instanceof Integer) {
((ImageView) view).setImageDrawable(new ColorDrawable((Integer) background));
} else {
((ImageView) view).setImageDrawable((Drawable) background);
}
}
} catch (Exception e) {
e.printStackTrace();
}
break;
case "textColor":
try {
((TextView) view).setTextColor(SkinResources.getInstance().getColorStateList(skinPair.resId));
} catch (Exception e) {
e.printStackTrace();
}
break;
case "textColorHint":
try {
((EditText) view).setHintTextColor(SkinResources.getInstance().getColorStateList(skinPair.resId));
} catch (Exception e) {
e.printStackTrace();
}
break;
case "drawableLeft":
left = SkinResources.getInstance().getDrawable(skinPair.resId);
break;
case "drawableTop":
top = SkinResources.getInstance().getDrawable(skinPair.resId);
break;
case "drawableRight":
right = SkinResources.getInstance().getDrawable(skinPair.resId);
break;
case "drawableBottom":
bottom = SkinResources.getInstance().getDrawable(skinPair.resId);
break;
case "skinTypeface":
Typeface typeface1 = SkinResources.getInstance().getTypeface(skinPair.resId);
applySkinTypeface(typeface1);
break;
case "text":
if (skinPair.resId > 0) {
textStr = SkinResources.getInstance().getString(skinPair.resId);
applyText(textStr);
}
break;
case "hint":
if (skinPair.resId > 0) {
textStr = SkinResources.getInstance().getString(skinPair.resId);
applyHintText(textStr);
}
break;
default:
break;
}
if (null != left || null != right || null != top || null != bottom) {
((TextView) view).setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static Handler mHandler = new Handler(Looper.getMainLooper());
private void applySkinViewSupport() {
if (view instanceof SkinViewSupport) {
((SkinViewSupport) view).applySkin();
}
}
private void applySkinTypeface(final Typeface typeface) {
if (view instanceof TextView) {
//Log.d(TAG, "applySkinTypeface() called with: view = [" + ((TextView) view).getText() + "] Parent = " + view.getParent());
//post 防止某些控件的属性设置是在构造函数调用完成之后进行的。
mHandler.post(new Runnable() {
@Override
public void run() {
((TextView) view).setTypeface(typeface);
}
});
}
}
private void applyText(final String textStr) {
if (view instanceof TextView) {
//Log.d(TAG, "applyText() called with: view = [" + ((TextView) view).getText() + "] Parent = " + view.getParent());
//post 防止某些控件的属性设置是在构造函数调用完成之后进行的。
mHandler.post(new Runnable() {
@Override
public void run() {
((TextView) view).setText(textStr);
}
});
}
}
private void applyHintText(final String textStr) {
if (view instanceof EditText) {
//Log.d(TAG, "applyHintText() called with: view = [" + ((TextView) view).getText() + "] Parent = " + view.getParent());
//post 防止某些控件的属性设置是在构造函数调用完成之后进行的。
mHandler.post(new Runnable() {
@Override
public void run() {
((EditText) view).setHint(textStr);
}
});
}
}
}
static class SkinPair {
String attributeName;
int resId;
public SkinPair(String attributeName, int resId) {
this.attributeName = attributeName;
this.resId = resId;
}
}
}