静态换肤:BiuSkin2DN

2023年9月4日 pm5:51发布3个月前更新 3XCN.COM站长
1 0 0
广告也精彩
目录

前言

之前介绍Android的动态换肤(BiuSkin1PNA )和静态换肤(BiuSkin1DN)都是同一套方式,采集View属性,然后进行切换。比较完美,但缺点也是有的,采集的View属性越多,越占内存!

因此参考网上Android高级课程中的换肤方式,在其基础上,演变出第二种换肤方式:BiuSkin2。

BiuSkin2也分动态换肤和静态换肤,今天介绍的是第二种静态换肤库:BiuSkin2DN。

正文

BiuSkin2DN跟《静态换肤库:BiuSkin1DN》换肤机制不一样,但使用方法我这里都统一了,都差不多,唯一就是自定义View换肤需要自己按照要求修改。

如果对日夜模式不熟悉,可以看《再谈Android日夜模式》,这篇算是我比较全的记录。

下载

已经失效。。

隐藏内容!
评论后才能查看!

优点和缺点

优点
  1. 不需要采集View属性

  2. SkinManager接口简单

缺点
  1. 部分View不支持,已知的ProgressBar和Spinner

  2. 自定义View需要重写换肤View

接口

SkinManager

皮肤管理中心,核心类。

SkinManager.initContext(this, false);
SkinManager.getInstance().setISkinRefreshListener(this);
SkinManager.getInstance().changeSkin();
ISkinRefreshListener
public interface ISkinRefreshListener {
    View onSkinRefresh(Context context, String name, AttributeSet attrs);
}
IBiuSkin

自定义View需要实现的接口,换肤是回调onSwitchView()

public interface IBiuSkin {
    void onSwitchView();
}
SkinResource

自定义View需要

public void addResourceIdArray(int[] styleAble, TypedArray typedArray);
public int getResourceId(int styleAble);
public void clear();
自定义View换肤

参考TextView的换肤替代View:BiuTextView

res/valuse/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="BiuTextView">
        <attr name="android:background" />
        <attr name="android:textColor" />
        <attr name="android:textSize" />
    </declare-styleable>
</resources>
BiuTextView.java
public class BiuTextView extends AppCompatTextView implements IBiuSkin {
    private final SkinResource mSkinResource;
    public BiuTextView(Context context) {
        this(context, null);
    }
    public BiuTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public BiuTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mSkinResource = new SkinResource();
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BiuTextView, defStyleAttr, 0);
        mSkinResource.addResourceIdArray(R.styleable.BiuTextView, typedArray);
        typedArray.recycle();
    }

    @Override
    public void onSwitchView() {
        int backgroundResourceId = mSkinResource.getResourceId(R.styleable.BiuTextView[
        R.styleable.BiuTextView_android_background]);
        if (backgroundResourceId > 0) {
            Drawable drawable = ContextCompat.getDrawable(getContext(), backgroundResourceId);
            setBackgroundDrawable(drawable);
        }
        int textColorResourceId = mSkinResource.getResourceId(R.styleable.BiuTextView[
        R.styleable.BiuTextView_android_textColor]);
        if (textColorResourceId > 0) {
            setTextColor(ContextCompat.getColorStateList(getContext(), textColorResourceId));
        }
        int textSizeId = mSkinResource.getResourceId(R.styleable.BiuTextView[
        R.styleable.BiuTextView_android_textSize]);
        if (textSizeId > 0) {
            setTextSize(getResources().getDimensionPixelSize(textSizeId));
        }
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (null != mSkinResource) {
            mSkinResource.clear();
        }
    }
}

参考文章

  1. 参考网易云课堂的Android课程(课程下架了)

版权声明 1、 本站名称: 91易搜
2、 本站网址: 91es.com3xcn.com
3、 本站文章: 部分来源于网络,仅供站长学习和参考,若侵权请留言
广告也精彩

暂无评论

评论审核已启用。您的评论可能需要一段时间后才能被显示。

暂无评论...