2024年安卓最全说说Android动态换肤实现原理吧,2024年最新零基础学习android

写在最后

本次我的分享也接近尾声了,感谢你们在百忙中花上一下午来这里聆听我的宣讲,希望在接下来的日子,我们共同成长,一起进步!!!

最后放上一个大概的Android学习方向及思路(详细的内容太多了~),提供给大家:

对于程序员来说,要学习的知识内容、技术有太多太多,这里就先放上一部分,其他的内容有机会在后面的文章向大家呈现出来,不过我自己所有的学习资料都整理成了一个文档,一直在不断学习,希望能帮助到大家,也节省大家在网上搜索资料的时间来学习,也可以分享动态给身边好友一起学习!

为什么某些人会一直比你优秀,是因为他本身就很优秀还一直在持续努力变得更优秀,而你是不是还在满足于现状内心在窃喜!希望读到这的您能点个小赞和关注下我,以后还会更新技术干货,谢谢您的支持!

Android架构师之路很漫长,一起共勉吧!

如果你觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言,一定会认真查询,修正不足,谢谢。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

适用于大量皮肤,用户选择下载,像QQ、网易云音乐这种。它是将皮肤包下载到本地,皮肤包其实是个APK。

换肤包括替换图片资源、布局颜色、字体、文字颜色、状态栏和导航栏颜色。

动态换肤步骤包括:

  • 采集需要换肤的控件

  • 加载皮肤包

  • 替换资源

实现原理

首先Activity的onCreate()方法里面我们都要去调用setContentView(int id) 来指定当前Activity的布局文件:

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

再往里看:

@Override

public void setContentView(int resId) {

ensureSubDecor();

ViewGroup contentParent = (ViewGroup) mSubDecor.findViewById(android.R.id.content);

contentParent.removeAllViews();

LayoutInflater.from(mContext).inflate(resId, contentParent);//这里实现view布局的加载

mOriginalWindowCallback.onContentChanged();

}

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {

return inflate(resource, root, root != null);

}

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {

final Resources res = getContext().getResources();

if (DEBUG) {

Log.d(TAG, “INFLATING from resource: “” + res.getResourceName(resource) + “” (”

  • Integer.toHexString(resource) + “)”);

}

final XmlResourceParser parser = res.getLayout(resource);

try {

return inflate(parser, root, attachToRoot);

} finally {

parser.close();

}

}

public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {

final String name = parser.getName();

final View temp = createViewFromTag(root, name, inflaterContext, attrs);

return temp;

}

可以看到inflate会返回具体的View对象出去,那么我们的关注焦点就放在createViewFromTag中了

/**

  • Creates a view from a tag name using the supplied attribute set.

  • Note: Default visibility so the BridgeInflater can

  • override it.

  • @param parent the parent view, used to inflate layout params

  • @param name the name of the XML tag used to define the view

  • @param context the inflation context for the view, typically the

  •            {@code parent} or base layout inflater context
    
  • @param attrs the attribute set for the XML tag used to define the view

  • @param ignoreThemeAttr {@code true} to ignore the {@code android:theme}

  •                    attribute (if set) for the view being inflated,
    
  •                    {@code false} otherwise
    

*/

View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,

boolean ignoreThemeAttr) {

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;

}

return view;

} catch (Exception e) {

}

}

inflate最终调用了createViewFromTag方法来创建View,在这之中用到了factory,如果factory存在就用factory创建对象,如果不存在就由系统自己去创建。我们只需要实现我们的Factory然后设置给mFactory2就可以采集到所有的View了,这里是一个Hook点。

当我们采集完了需要换肤的view,下一步就是加载皮肤包资源。当我们拿到当前View的资源名称时就会先去皮肤插件中的资源文件里找

Android加载资源的流程图:

1.采集换肤控件

android解析xml创建view的步骤:

  • setContentView -> window.setContentView()(实现类是PhoneWindow)->mLayoutInflater.inflate() -> inflate … ->createViewFromTag().

所以我们复写了Factory的onCreateView之后,就可以不通过系统层而是自己截获从xml映射的View进行相关View创建的操作,包括对View的属性进行设置(比如背景色,字体大小,颜色等)以实现换肤的效果。如果onCreateView返回null的话,会将创建View的操作交给Activity默认实现的Factory的onCreateView处理。

1.使用ActivityLifecycleCallbacks,尽可能少的去侵入代码,在onActivityCreated中监听每个activity的创建。

@Override

public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

LayoutInflater layoutInflater = LayoutInflater.from(activity);

try {

//系统默认 LayoutInflater只能设置一次factory,所以利用反射解除限制

Field mFactorySet = LayoutInflater.class.getDeclaredField(“mFactorySet”);

mFactorySet.setAccessible(true);

mFactorySet.setBoolean(layoutInflater, false);

} catch (Exception e) {

e.printStackTrace();

}

//添加自定义创建View 工厂

SkinLayoutFactory factory = new SkinLayoutFactory(activity, skinTypeface);

layoutInflater.setFactory2(factory);

}

2.在 SkinLayoutFactory中将每个创建的view进行筛选采集

//根据tag反射获取view

@Override

public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {

// 反射 classLoader

View view = createViewFromTag(name, context, attrs);

// 自定义View

if(null == view){

view = createView(name, context, attrs);

}

//筛选符合属性View

skinAttribute.load(view, attrs);

return view;

}

3.将view封装成对象

//view的参数对象

static class SkinPain {

String attributeName;

int resId;

public SkinPain(String attributeName, int resId) {

this.attributeName = attributeName;

this.resId = resId;

}

}

//view对象

static class SkinView {

View view;

List skinPains;

public SkinView(View view, List skinPains) {

this.view = view;

this.skinPains = skinPains;

}

}

将属性符合的view保存起来

public class SkinAttribute {

private static final List mAttributes = new ArrayList<>();

static {

mAttributes.add(“background”);

mAttributes.add(“src”);

mAttributes.add(“textColor”);

mAttributes.add(“drawableLeft”);

mAttributes.add(“drawableTop”);

mAttributes.add(“drawableRight”);

mAttributes.add(“drawableBottom”);

mAttributes.add(“skinTypeface”);

}

private List skinViews = new ArrayList<>();

public void load(View view, AttributeSet attrs) {

List skinPains = new ArrayList<>();

for (int i = 0; i < attrs.getAttributeCount(); i++) {

//获取属性名字

String attributeName = attrs.getAttributeName(i);

if (mAttributes.contains(attributeName)) {

//获取属性对应的值

String attributeValue = attrs.getAttributeValue(i);

if (attributeValue.startsWith(“#”)) {

continue;

}

int resId;

//判断前缀字符串 是否是"?"

//attributeValue = “?2130903043”

if (attributeValue.startsWith(“?”)) { //系统属性值

//字符串的子字符串 从下标 1 位置开始

int attrId = Integer.parseInt(attributeValue.substring(1));

resId = SkinThemeUtils.getResId(view.getContext(), new int[]{attrId})[0];

} else {

//@1234564

resId = Integer.parseInt(attributeValue.substring(1));

}

if (resId != 0) {

SkinPain skinPain = new SkinPain(attributeName, resId);

skinPains.add(skinPain);

}

}

}

//SkinViewSupport是自定义view实现的接口,用来区分是否需要换肤

if (!skinPains.isEmpty() || view instanceof TextView || view instanceof SkinViewSupport) {

SkinView skinView = new SkinView(view, skinPains);

skinView.applySkin(mTypeface);

skinViews.add(skinView);

}

}

}

2.加载皮肤包

加载皮肤包需要我们动态获取网络下载的皮肤包资源,问题是我们如何加载皮肤包中的资源

Android访问资源使用的是Resources这个类,但是程序里面通过getContext获取到的Resources实例实际上是对应程序本来的资源的实例,也就是说这个实例只能加载app里面的资源,想要加载皮肤包里面的就不行了

自己构造一个Resources(这个Resources指向的资源就是我们的皮肤包)

看看Resources的构造方法,可以看到主要是需要一个AssetManager

public Resources(AssetManager assets, DisplayMetrics metrics, Configuration config) {

this(null);

mResourcesImpl = new ResourcesImpl(assets, metrics, config, new DisplayAdjustments());

}

构造一个指向皮肤包的AssetManager,但是这个AssetManager是不能直接new出来的,这里就使用反射来实例化了

AssetManager assetManager = AssetManager.class.newInstance();

AssetManager有一个addAssetPath方法可以指定资源的位置,可惜这个也只能用反射来调用

Method addAssetPath = assetManager.getClass().getMethod(“addAssetPath”, String.class);

addAssetPath.invoke(assetManager, filePath);

最后,如果大伙有什么好的学习方法或建议欢迎大家在评论中积极留言哈,希望大家能够共同学习、共同努力、共同进步。

小编在这里祝小伙伴们在未来的日子里都可以 升职加薪,当上总经理,出任CEO,迎娶白富美,走上人生巅峰!!

不论遇到什么困难,都不应该成为我们放弃的理由!

很多人在刚接触这个行业的时候或者是在遇到瓶颈期的时候,总会遇到一些问题,比如学了一段时间感觉没有方向感,不知道该从那里入手去学习

如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言,一定会认真查询,修正不足,谢谢。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

).getMethod(“addAssetPath”, String.class);

addAssetPath.invoke(assetManager, filePath);

最后,如果大伙有什么好的学习方法或建议欢迎大家在评论中积极留言哈,希望大家能够共同学习、共同努力、共同进步。

小编在这里祝小伙伴们在未来的日子里都可以 升职加薪,当上总经理,出任CEO,迎娶白富美,走上人生巅峰!!

不论遇到什么困难,都不应该成为我们放弃的理由!

很多人在刚接触这个行业的时候或者是在遇到瓶颈期的时候,总会遇到一些问题,比如学了一段时间感觉没有方向感,不知道该从那里入手去学习

如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言,一定会认真查询,修正不足,谢谢。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值