Android學習之插件化換膚

做好計劃,定期復盤,感知責任,提高執(zhí)行力
換膚有好幾種方式,今天主要來看看插件化換膚的相關(guān)知識,本文主要涉及到的知識點:
setContentView實現(xiàn)源碼分析、LayoutInflater中的inflate解析、
Observer與Observable、 Application的onCreate的妙用

1.setContentView實現(xiàn)源碼分析

setContentView源碼分析流程.png
上圖為setContentView的一個調(diào)用流程,具體可以參考源碼做詳細分析
布局層級結(jié)構(gòu).png
2.LayoutInflater中的inflate解析

2.1 對于inflate其主要做以下說明:

 public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
      //此處代碼省略
    }

主要是一般在使用是會正常傳入root參數(shù),并且attachToRoot為false,如果attachToRoot為ture,有可能會出現(xiàn)設(shè)置layout_width參數(shù)生效問題(此部分可作為后面的一個補充點做說明)。
另外,會調(diào)用createViewFromTag對XML布局進行解析,下面會介紹個這個接口;

2.2 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;
        }
    }

從代碼看首先會判斷mFactory2 ,mFactory 來進行createView即創(chuàng)建view,這兩個可謂是作用很大會用來做攔截view的創(chuàng)建過程,從而可以做本文將的重點換膚。心靜1秒。。。
3. Observer與Observable
Observer作為被觀察者,在換膚中可以用來檢測每個Activity的換膚需求,而用來接管系統(tǒng)的View的生產(chǎn)過程的類(SkinLayoutInflaterFactory)即可作為觀察者,當當前Activity需要換膚時,通過setChanged()和notifyObservers接口即可通知觀察類通過update() 接口進行更新?lián)Q膚;
4. Application的onCreate的妙用
Application的onCreate()方法調(diào)用是早于Anctivity的onCreate(),此點在換膚中的應用是Factory的設(shè)置,由自定義Factory來接管,以及會走后面的onCreatView進行新皮膚的加載;另外,比如Gride在第一次調(diào)用時會出現(xiàn)加載圖片很慢的情況,因為在第一次是Gride會做一些初始化的動作,所以如果出現(xiàn)這種問題,可以考慮在Application的onCreate()中簡單做一個Gride的一個初始化init,可以優(yōu)化此問題。。。。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容