插件式换肤框架搭建 - 插件式换肤框架的完善

1. 概述


基于插件式换肤框架搭建 - 资源加载源码分析和插件式换肤框架搭建 - setContentView源码阅读前两篇文章,那么目前我们不仅可以从另外一个插件皮肤包中获取资源了而且还可以去拦截系统View的创建,那么现在我们只要写点代码就可以达到无缝换肤的效果了。

GIF.gif

所有分享大纲:2017Android进阶之路与你同行

视频讲解地址:http://pan.baidu.com/s/1nvv2Nln

2. Hook拦截View的创建


前面讲解模板设计模式构建BaseActivity的时候,因为想到了框架的可扩展性所以就特意留了一层BaseSkinActivity,我们就在这里面去拦截和创建View就好,前提是要兼容tint等等的一些功能,下面的这些代码我们都是看源码得到的,google工程师写的基本不会有问题,看他们的源码考虑得很周到,还是可以借鉴一下的:

public class BaseSkinActivity extends AppCompatActivity implements LayoutInflaterFactory {
    // 创建View的Inflater
    private SkinCompatViewInflater mSkinCompatViewInflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 给系统的LayoutInflater 设置Factory可以仿照系统的源码去写
        LayoutInflater layoutInflater = LayoutInflater.from(this);
        LayoutInflaterCompat.setFactory(layoutInflater, this);
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(View parent, final String name, @NonNull Context context,
                             @NonNull AttributeSet attrs) {
        final boolean isPre21 = Build.VERSION.SDK_INT < 21;

        if (mSkinCompatViewInflater == null) {
            mSkinCompatViewInflater = new SkinCompatViewInflater();
        }

        // We only want the View to inherit its context if we're running pre-v21
        final boolean inheritContext = isPre21 && shouldInheritContext((ViewParent) parent);

        View view = mSkinCompatViewInflater.createView(parent, name, context, attrs, inheritContext,
                isPre21, /* 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 */
        );

        if (view != null) {
            List skinAttrs = SkinSupport.getSkinView(context, attrs);
            if (skinAttrs.size() != 0) {
                addSkinManager(view, skinAttrs);
            }
        }
        return view;
    }

    // 添加到皮肤管理
    private void addSkinManager(View view, List skinAttrs) {
        SkinView skinView = new SkinView(view, skinAttrs);

        List skinViews = SkinManager.getInstance().getSkinViews(this);

        if (skinViews == null) {
            skinViews = new ArrayList<>();
            SkinManager.getInstance().registerSkinView(skinViews, this);
        }
        skinViews.add(skinView);
        // 如果需要换肤
        if(SkinManager.getInstance().needChangeSkin()){
            SkinManager.getInstance().changeSkin(skinView);
        }
    }

    // 从系统源码贴的,google工程师写的基本不会有问题
    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();
        }
    }
}

3. 完善SkinManager这个管理类


在网上也看了很多第三方的换肤框架都还蛮好的,但是总感觉达不到自己想要的效果。其实之所以自己写是因为当时我真的想把电脑一脚踢了,就那么一个Bug自己搞了好几天没搞定,从来没有那么痛苦过。自己写也就没那么大的局限性,也不需要关注配置什么的,直接一行解决问题。

 /**
 * Created by Darren on 2017/3/20.
 * Email: [email protected]
 * Description: 皮肤的管理类
 */
public class SkinManager {
    private static SkinManager mInstance;
    private SkinResources mSkinResources;
    private Context mContext;

    private Map> mSkinViews = new HashMap<>();
    // 把View交给它管理

    private SkinManager() {
    }

    /**
     * 初始化 一般都在Application中配置
     * @param context
     */
    public void init(Context context) {
        this.mContext = context.getApplicationContext();
        String skinPath = SkinPreUtils.getInstance(mContext).getSkinPath();

        if (TextUtils.isEmpty(skinPath)) {
            return;
        }

        File skinFile = new File(skinPath);
        if (!skinFile.exists()) {
            clearSkinInfo();
            return;
        }

        initSkinResource(skinPath);
    }

    static {
        mInstance = new SkinManager();
    }

    public static SkinManager getInstance() {
        return mInstance;
    }


    /**
     * 加载皮肤
     *
     * @param path 当前皮肤的路径
     */
    public int loadSkin(String path) {
        String currentSkinPath = SkinPreUtils.getInstance(mContext).getSkinPath();
        if (currentSkinPath.equals(path)) {
            return SkinConfig.SKIN_LOADED;
        }

        File skinFile = new File(path);
        if(!skinFile.exists()){
            return SkinConfig.SKIN_PATH_ERROR;
        }

        // 判断签名是否正确,后面增量更新再说
        initSkinResource(path);
        changeSkin(path);
        saveSkinInfo(path);
        // 加载成功
        return SkinConfig.SKIN_LOAD_SUCCESS;
    }

    /**
     * 切换皮肤
     *
     * @param path 当前皮肤的路径
     */
    private void changeSkin(String path) {
        for (ISkinChangeListener skinChangeListener : mSkinViews.keySet()) {
            List skinViews = mSkinViews.get(skinChangeListener);
            for (SkinView skinView : skinViews) {
                skinView.skin();
            }
            skinChangeListener.changeSkin(path);
        }
    }

    /**
     * 初始化皮肤的Resource
     *
     * @param path
     */
    private void initSkinResource(String path) {
        mSkinResources = new SkinResources(mContext, path);
    }

    /**
     * 保存当前皮肤的信息
     *
     * @param path 当前皮肤的路径
     */
    private void saveSkinInfo(String path) {
        SkinPreUtils.getInstance(mContext).saveSkinPath(path);
    }

    /**
     * 恢复默认皮肤
     */
    public void restoreDefault() {
        String currentSkinPath = SkinPreUtils.getInstance(mContext).getSkinPath();
        if (TextUtils.isEmpty(currentSkinPath)) {
            return;
        }

        String path = mContext.getPackageResourcePath();
        initSkinResource(path);
        changeSkin(path);
        clearSkinInfo();
    }

    /**
     * 清空皮肤信息
     */
    private void clearSkinInfo() {
        SkinPreUtils.getInstance(mContext).clearSkinInfo();
    }

    public SkinResources getSkinResources() {
        return mSkinResources;
    }

    /**
     * 注册监听回调
     */
    public void register(List skinViews, ISkinChangeListener skinChangeListener) {
        mSkinViews.put(skinChangeListener, skinViews);
    }

    public List getSkinViews(Activity activity) {
        return mSkinViews.get(activity);
    }

    public boolean isChangeSkin() {
        return SkinPreUtils.getInstance(mContext).needChangeSkin();
    }

    public void changeSkin(SkinView skinView) {
        skinView.skin();
    }

    /**
     * 移除回调,怕引起内存泄露
     */
    public void unregister(ISkinChangeListener skinChangeListener) {
        mSkinViews.remove(skinChangeListener);
    }
}

4. 最后的换肤使用和测试


前提是我们所有的Activity必须继承自BaseSkinActivity,然后在任何一个地方都可以用SkinManager的loadSkin()方法就可以达到无缝切换不需要重启app,目前虽然整个换肤框架只有十几个类但是有事没事就可以优化优化或者增加点功能什么的。奇葩的自定义View那就只能自己在Activity做一丁点手脚,这是目前还没有办法解决的问题。

public class MainActivity extends BaseSkinActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void skin(View view) {
        // 1. 首先去网上下载资源皮肤
        // 2. 下载后保存在本地
        String skinPath = Environment.getExternalStorageDirectory().getAbsolutePath()
                + File.separator + "skin1.skin";

        // 3. 调用该方法去加载皮肤 不需要重启
        int result = SkinManager.getInstance().loadSkin(skinPath);
        // 可以判断result是否换肤成功,不同的状态对应不同的原因
    }

    public void skin1(View view) {
        // 恢复默认皮肤
        SkinManager.getInstance().restoreDefault();
    }

    // 跳转activity
    public void skin2(View view) {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }
}

所有分享大纲:2017Android进阶之路与你同行

视频讲解地址:http://pan.baidu.com/s/1nvv2Nln

你可能感兴趣的:(插件式换肤框架搭建 - 插件式换肤框架的完善)