Android 换肤解决方案

https://www.jianshu.com/p/b0253de8ac04

这是一篇讲解了三种当下流行的换肤方案文章, 挺好的, 不过不太适合, 下面是我冥思苦想又机缘巧合下产生的方法(末尾添加了优化后的github下载地址):

首先是关于TextView的文本换色:

因为我们APP统一使用一种字体, 所以在项目初期就全部使用的自定义的TextView, 所以换色就比较简单了:

public class TTFTextView extends AppCompatTextView {

    private Context context;

    public TTFTextView(Context context) {
        super(context);
    }

    public TTFTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public TTFTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;

        judgeTheme();
    }

    @Override
    public void setTextColor(int color) {
        super.setTextColor(color);

        judgeTheme();
    }

    private void judgeTheme() {
        switch (ThemeUtil.getTheme()) {
            case ThemeUtil.BLACK:
                if (getCurrentTextColor() == context.getResources().getColor(R.color.text_color_light)) {
                    setTextColor(context.getResources().getColor(R.color.theme_black_text_color_light));
                } else if (getCurrentTextColor() == context.getResources().getColor(R.color.text_color_hard)) {
                    setTextColor(context.getResources().getColor(R.color.theme_black_text_color_hard));
                } else if (getCurrentTextColor() == context.getResources().getColor(R.color.text_color_select)) {
                    setTextColor(context.getResources().getColor(R.color.theme_black_text_color_select));
                }
                break;
        }
    }

下面我又用同样的方法解决掉了ImageView换图片的问题:

例如:

<com.x.x.customview.skinview.SkinImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon_normal" />

想法是, 重写ImageView, 获取到通过src设置的图片的资源id, 再由资源id得到资源名称, 就是xml中所写的"icon_normal",

之后我们再存入一张黑色主题的图片叫做theme_black_icon_normal.png; 这时我们将上一步得到的资源名称前面加上"theme_black_", 再通过这个拼好的名字获取到黑色主题的图片的资源id, 最后赋给ImageView解决问题, 哈哈哈~

public class SkinImageView extends AppCompatImageView {

    private Context context;

    public SkinImageView(Context context) {
        super(context);
    }

    public SkinImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public SkinImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)         {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, @Nullable AttributeSet attrs) {
        this.context = context;
        if (ThemeUtil.getTheme() == ThemeUtil.BLACK) {
            if (attrs != null) {
                int src_resource = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android", "src", 0);
                String resourceName = getResources().getResourceName(src_resource);
                //Log.d(">>>SkinImage", "resource name = " + resourceName);
                //打印结果 resource name = com.x.x:drawable/icon_copy
                if (!TextUtils.isEmpty(resourceName)) {
                    if (resourceName.contains("/")) {
                        String imageName = resourceName.split("/")[1];
                        if (!imageName.startsWith("theme_black_")) {
                            int resourceId = getResources().getIdentifier("theme_black_" + imageName, "drawable", context.getPackageName());
                            //没有找到图片返回 0
                            if (resourceId != 0) {
                                setImageResource(resourceId);
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public void setImageResource(int resId) {
        if (ThemeUtil.getTheme() == ThemeUtil.BLACK) {
            String resourceName = getResources().getResourceName(resId);
            if (!TextUtils.isEmpty(resourceName)) {
                if (resourceName.contains("/")) {
                    String imageName = resourceName.split("/")[1];
                    if (!imageName.startsWith("theme_black_")) {
                        int blackResourceId = getResources().getIdentifier("theme_black_" + imageName, "drawable", context.getPackageName());
                        if (blackResourceId != 0) {
                            resId = blackResourceId;
                        }
                    }
                }
            }
        }
        super.setImageResource(resId);
    }

项目链接:https://github.com/Dahuoji-Coder/SkinView

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章