静态换肤:BiuSkin2DN

2023年9月4日 pm5:51发布1年前 (2023)更新 91es.com站长
198 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、 本站内容: 部分来源于网络,仅供学习和参考,若侵权请留言
3、 本站申明: 个人流水账日记,内容并不保证有效

暂无评论

暂无评论...

随机推荐

Android 修改ListView快速滚动条的bar

前言最近需要使用修改ListView快速滚动条的bar,ListView是可以默认支持的,但就是太丑了,需要定制一下。下面就记录一下自己使用的方法。好记性不如烂笔头正文本文并非原创,感谢网友分享。隐藏内容!付费阅读后才能查看!¥2 ¥3多个隐藏块只需支付一次付费阅读

[摘]js中对函数设置默认参数值的3种方法

在javascript中如何为函数设置默认参数值,下面提供几种方法供大家参考。第一种方法:function example(a,b){ var a = arguments[0] ? arguments[0] : 1;//设置参数a的默认值为1 var b = argume...

Android String根据指定长度进行截取文本

前言在做自定义TextImageView时,画的文本长度存在过长,因此需要跟进ImageView的宽度进行限制Text的宽度,也就引出需要新需求:根据Text文本长度进行裁剪PS: Button 本来是支持图片+Text的组合显示的,但是Glide需要传入ImageView,因此才有TextI...

简单记录AIDL添加回调

前言之前也记录过AIDL的使用,今天简单写AIDL添加回调代码。记录一下,方便自己查阅。流水账,勿喷!正文为了简单一点,放在同一个应用中,然后把服务设置到新的进程中。AIDL文件设置回调,这里会创建两个AIDL文件。ICar.aidl// ICar.aidlpackage ...

音频播放音频播放之SoundPool 详解

SoundPool —— 适合短促且对反应速度比较高的情况(游戏音效或按键声等)下面介绍SoundPool的创建过程:1. 创建一个SoundPool (构造函数)public SoundPool(int maxStream, int streamType, int srcQuality)m...

Android高版本getDrawable(int id)废弃后的替代方法

前言在Android 高版本上使用getDrawable(int id)时,如下,有提示使用的这个方法废弃了 mIvAlbum.setBackground(getResources().getDrawable(R.drawable.music_album_unknown));点进入源...