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

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