Android-Skin-Loader源码解析

源码

一:简介

Android-Skin-Loader是一个通过动态加载技术实现换肤的框架;解决换肤的两个核心问题:(1)怎么把外部的皮肤资源,加载到应用内部?(2)怎么确定哪些View需要换肤,以及把外部资源加载进来后怎么进行换肤?

二:核心代码位于load包下

该包下面的两个类就分别解决了上述问题;
SkinInflaterFactory: 搜集需要的换肤的控件,并创建相应的换肤控件,并把需要换肤的空间及其相应支持的换肤属性存储起来。
SkinManager: 其内部通过反射调用AssetManager.addAssetPath()把外部的皮肤资源加载到AssetManager中,并通过该AssetManager创建相应的Resource。当执行换肤操作的时候,就可以设置需要换肤View的相关属性为Resource中相应的资源;

load

2.1 SkinInflaterFactory.Java

简介: SkinInflaterFactory实现了LayoutInflaterFactory,

SkinInflaterFactory
主要思想就是根据View是否设置了skinEnable属性,如果设置skinEnable属性,就可以通过createView去创建相应的View;
SkinInflaterFactory
createView会根据name属性去创建相应的View,内部使用了

view = LayoutInflater.from(context).createView(name, "android.view.", attrs);

创建View;parseSkinAttr()方法主要是 搜集可更换皮肤的属性(color,background之类)并做一些存储;支持的属性如下;

       if (BACKGROUND.equals(attrName)) {
            return true;
        }
        if (TEXT_COLOR.equals(attrName)) {
            return true;
        }
        if (TAB_INDICATOR_COLOR.equals(attrName)) {
            return true;
        }
        if (CONTENT_SCRIM_COLOR.equals(attrName)) {
            return true;
        }
        if (BACKGROUND_TINTLIST.equals(attrName)) {
            return true;
        }
2.2 SkinManager.Java

执行换肤操作时,就会调用load()方法,load方法会根据皮肤资源的路径,在AsyncTask内部加载皮肤资源;主要逻辑如下
(1)先创建 AssetManager的实例,然后通过反射调用assetAddPath()将皮肤资源加载到AssetManager 中;

 AssetManager assetManager = AssetManager.class.newInstance(); 
 Method addAssetPath =  assetManager.getClass().getMethod("addAssetPath", String.class);
 addAssetPath.invoke(assetManager, skinPkgPath);

(2) 根据 AssetManager创建Resources。Resources的创建需要三个参数,一个是AssetManager,第二,三个是设备相关的信息,分别是显示设置,和Configuration(配置)设置;

  Resources superRes = context.getResources();
   Resources skinResource = new Resources(assetManager, superRes.getDisplayMetrics(), superRes.getConfiguration());

完整代码

  public void load(String skinPackagePath, final ILoaderListener callback) {

        new AsyncTask<String, Void, Resources>() {

            protected void onPreExecute() {
                if (callback != null) {
                    callback.onStart();
                }
            }

            @Override
            protected Resources doInBackground(String... params) {
                try {
                    if (params.length == 1) {
                        String skinPkgPath = params[0];
                        Log.i("loadSkin", skinPkgPath);
                        File file = new File(skinPkgPath);
                        if (file == null || !file.exists()) {
                            return null;
                        }
                        Log.i("SkinManager context", context + "");
                        PackageManager mPm = context.getPackageManager();
                        Log.i("SkinManager", mPm + "");
                        PackageInfo mInfo = mPm.getPackageArchiveInfo(skinPkgPath, PackageManager.GET_ACTIVITIES);
                        skinPackageName = mInfo.packageName;

                        AssetManager assetManager = AssetManager.class.newInstance();
                        Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);
                        addAssetPath.invoke(assetManager, skinPkgPath);


                        Resources superRes = context.getResources();
                        Resources skinResource = new Resources(assetManager, superRes.getDisplayMetrics(), superRes.getConfiguration());

                        SkinConfig.saveSkinPath(context, skinPkgPath);

                        skinPath = skinPkgPath;
                        isDefaultSkin = false;
                        return skinResource;
                    }
                    return null;
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }

            protected void onPostExecute(Resources result) {
                mResources = result;

                if (mResources != null) {
                    if (callback != null) callback.onSuccess();
                    notifySkinUpdate();
                } else {
                    isDefaultSkin = true;
                    if (callback != null) callback.onFailed();
                }
            }

        }.execute(skinPackagePath);
    }

三:SkinBaseActivity

所以需要换肤的Activity都需要继承这个Activity;内部主要设置了自定义的Factory;

  LayoutInflaterCompat.setFactory(getLayoutInflater(), mSkinInflaterFactory);
@Override
    protected void onCreate(Bundle savedInstanceState) {
        mSkinInflaterFactory = new SkinInflaterFactory();
        //getLayoutInflater().cloneInContext(this).setFactory(mSkinInflaterFactory);
        LayoutInflaterCompat.setFactory(getLayoutInflater(), mSkinInflaterFactory);
        super.onCreate(savedInstanceState);
        //changeStatusColor();

    }

四:其他

当操作时换肤,如何通知Activity来换肤呢?答案是通过观察者模式,当换肤这一动作发生后,会调用notifySkinUpdate() 通知所有的观察者来来执行换肤;

   @Override
    public void notifySkinUpdate() {
        if (mSkinObservers == null) return;
        for (ISkinUpdate observer : mSkinObservers) {
            observer.onThemeUpdate();
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,972评论 25 709
  • : ) 首先来说说应用场景 app里面的一些控件的属性(字体大小,字体颜色,背景..)需要根据皮肤包里面的资源随意...
    大批阅读 7,462评论 0 7
  • 该文章接上篇 Android实现夜间模式的方法(一) 三.夜间模式的实现方案——单纯夜间模式 1.通过切换主题...
    Corrine_Shao阅读 6,298评论 0 17
  • 很多年后,我们把这个夏天叫做“那年夏天”,但是那年夏天,我们曾笑得很美,很绚烂。 毕业和成年的字眼 格外扣人心弦各...
    行走的符号阅读 3,602评论 2 5
  • 只是想简单画画~在绘画的过程中~绘画变成我生活中重要的一部分~尽管画的很简单~却收获了一份好心情~专注的做...
    梅子吉祥如意怀德阅读 1,207评论 1 3

友情链接更多精彩内容