android 用代碼實(shí)現(xiàn)文本選擇器,改變字體顏色,背景色,漸變等

一下代碼可以一定程度上代替我們編寫xml來設(shè)置背景。
有報(bào)錯(cuò)自己修改一下就好。

使用方法

//實(shí)現(xiàn)紅色圓形背景,注意控件的layout_width和layout_height要相等
MyDrawable.instance().back(Color.RED).backRadius(100).into(binding.myButton1);

實(shí)現(xiàn)

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.graphics.drawable.StateListDrawable;
import android.view.View;
import android.widget.TextView;

/**
 * 動(dòng)態(tài)樣式構(gòu)造器
 */
public class MyDrawable {
    /**
     * 前景色,字體
     */
    private final ForeStyle foreStyle = new ForeStyle();
    /**
     * 背景色
     */
    private final BackStyle backStyle = new BackStyle();

    /**
     * 入口
     *
     * @return MyDrawable
     */
    public static MyDrawable instance() {
        return new MyDrawable();
    }

    /**
     * 私有構(gòu)造方法---空
     */
    private MyDrawable() {
    }

    /**
     * 設(shè)定字體顏色
     *
     * @param color 顏色值
     * @return MyDrawable
     */
    public MyDrawable fore(int color) {
        this.foreStyle.setColor(color);
        return this;
    }

    /**
     * 設(shè)定字體顏色
     *
     * @param color 顏色值,如#e5e0e5
     * @return MyDrawable
     */
    public MyDrawable fore(String color) {
        this.foreStyle.setColor(color);
        return this;
    }

//    /**
//     * 設(shè)定字體顏色
//     *
//     * @param resId 資源id
//     * @return MyDrawable
//     */
//    public MyDrawable foreRes(int resId) {
//        this.foreStyle.setColorRes(resId);
//        return this;
//    }

    /**
     * 設(shè)定點(diǎn)擊字體顏色樣式
     *
     * @param color    常規(guī)顏色
     * @param colorSel 選中顏色
     * @return MyDrawable
     */
    public MyDrawable fore(int color, int colorSel) {
        this.foreStyle.setColor(color, colorSel);
        return this;
    }

    /**
     * 設(shè)定點(diǎn)擊字體顏色樣式
     *
     * @param color    常規(guī)顏色
     * @param colorSel 選中顏色
     * @return MyDrawable
     */
    public MyDrawable fore(String color, String colorSel) {
        this.foreStyle.setColor(color, colorSel);
        return this;
    }

//    /**
//     * 設(shè)定點(diǎn)擊字體顏色樣式
//     *
//     * @param resId    常規(guī)顏色
//     * @param resIdSel 選中顏色
//     * @return MyDrawable
//     */
//    public MyDrawable foreRes(int resId, int resIdSel) {
//        this.foreStyle.setColorRes(resId, resIdSel);
//        return this;
//    }

    /**
     * 設(shè)定字體顏色樣式,當(dāng)顏色數(shù)量超為1,則只有常規(guī)顏色;當(dāng)顏色數(shù)量為2,則表現(xiàn)為點(diǎn)擊效果;
     * 當(dāng)顏色數(shù)控為3以上,顏色從上到下漸變
     *
     * @param colors 顏色值,列表
     * @return MyDrawable
     */
    public MyDrawable fore(int... colors) {
        this.foreStyle.setColors(colors);
        return this;
    }

    /**
     * 背景圓角值,使用該方法請(qǐng)注意要設(shè)置背景顏色,否則可能沒有圓角值效果
     *
     * @param radius 圓角值0~100有效
     * @return MyDrawable
     */
    public MyDrawable backRadius(int radius) {
        this.backStyle.setRadius(radius);
        return this;
    }

    /**
     * 設(shè)定背景圓角值,使用要求同{@link #backRadius(int)}
     *
     * @param topLeft     左上圓角值
     * @param topRight    右上圓角值
     * @param bottomLeft  左下圓角值
     * @param bottomRight 右下圓角值
     * @return MyDrawable
     */
    public MyDrawable backRadius(int topLeft, int topRight, int bottomLeft, int bottomRight) {
        this.backStyle.setRadius(topLeft, topRight, bottomLeft, bottomRight);
        return this;
    }

    /**
     * 設(shè)定背景顏色
     *
     * @param color 顏色值
     * @return MyDrawable
     */
    public MyDrawable back(int color) {
        this.backStyle.setColor(color);
        return this;
    }

    /**
     * 設(shè)備背景顏色
     *
     * @param color 顏色值
     * @return MyDrawable
     */
    public MyDrawable back(String color) {
        this.backStyle.setColor(color);
        return this;
    }

//    /**
//     * 設(shè)定背景顏色
//     *
//     * @param resId 顏色值-資源id
//     * @return MyDrawable
//     */
//    public MyDrawable backRes(int resId) {
//        this.backStyle.setColorRes(resId);
//        return this;
//    }

//    /**
//     * 設(shè)定點(diǎn)擊背景顏色樣式
//     *
//     * @param resId       默認(rèn)背景顏色資源id
//     * @param colorResSel 選中背景顏色資源id
//     * @return MyDrawable
//     */
//    public MyDrawable backRes(int resId, int colorResSel) {
//        this.backStyle.setColorRes(resId, colorResSel);
//        return this;
//    }

    /**
     * 設(shè)定點(diǎn)擊背景顏色樣式
     *
     * @param color    默認(rèn)背景顏色
     * @param colorSel 選中背景顏色樣式
     * @return MyDrawable
     */
    public MyDrawable back(int color, int colorSel) {
        this.backStyle.setColor(color, colorSel);
        return this;
    }

    /**
     * 設(shè)定背景樣式
     *
     * @param colors 參考{@link #fore(int...)}
     * @return MyDrawable
     */
    public MyDrawable back(int... colors) {
        this.backStyle.setColors(colors);
        return this;
    }

    /**
     * 設(shè)定邊框樣式
     *
     * @param color 邊框顏色
     * @param width 邊框?qū)挾?     * @return MyDrawable
     */
    public MyDrawable backBorder(int color, int width) {
        this.backStyle.setBorderColor(color, width);
        return this;
    }

//    /**
//     * 設(shè)定邊框樣式
//     *
//     * @param resId 邊框顏色資源id
//     * @param width 邊框?qū)挾?//     * @return MyDrawable
//     */
//    public MyDrawable backBorderRes(int resId, int width) {
//        this.backStyle.setBorderColorRes(resId, width);
//        return this;
//    }

    /**
     * 設(shè)定邊框樣式
     *
     * @param color 邊框顏色
     * @param width 邊框?qū)挾?     * @return MyDrawable
     */
    public MyDrawable backBorderRes(String color, int width) {
        this.backStyle.setBorderColor(color, width);
        return this;
    }

    /**
     * 將參數(shù)設(shè)定到view中
     *
     * @param target 指定的View,不為空
     */
    public void into(View target) {
        if (target instanceof TextView) {
//            target.setClickable(true);
            if (this.foreStyle.colors != null && this.foreStyle.colors.length == 2) {
                LinearGradient mLinearGradient = new LinearGradient(0.0F, 0.0F, 0.0F, ((TextView) target).getPaint().getTextSize(), this.foreStyle.colors[0], this.foreStyle.colors[1], TileMode.CLAMP);
                ((TextView) target).getPaint().setShader(mLinearGradient);
                target.invalidate();
            } else {
                if (this.foreStyle.normalColor == 2147483647) {
                    this.foreStyle.setColor(((TextView) target).getCurrentTextColor());
                }

                if (this.foreStyle.selectColor == 2147483647) {
                    ((TextView) target).setTextColor(this.foreStyle.normalColor);
                } else {
                    ColorStateList colorStateList = new ColorStateList(new int[][]{{-16842919}, {16842919}}, new int[]{this.foreStyle.normalColor, this.foreStyle.selectColor});
                    ((TextView) target).setTextColor(colorStateList);
                }
            }
        }

        float tl = toFloat(1.6D, (double) this.backStyle.topLeft);
        float tr = toFloat(1.6D, (double) this.backStyle.topRight);
        float bl = toFloat(1.6D, (double) this.backStyle.bottomLeft);
        float br = toFloat(1.6D, (double) this.backStyle.bottomRight);
        float[] radius = new float[]{tl, tl, tr, tr, br, br, bl, bl};
        GradientDrawable normal;
        if (this.backStyle.colors != null && this.backStyle.colors.length > 0) {
            normal = new GradientDrawable(Orientation.TOP_BOTTOM, this.backStyle.colors);
            normal.setCornerRadii(radius);
            target.setBackground(normal);
        } else if (this.backStyle.normalColor != 2147483647) {
            normal = new GradientDrawable();
            normal.setColor(this.backStyle.normalColor);
            normal.setCornerRadii(radius);
            normal.setStroke(this.backStyle.borderWidth, this.backStyle.borderColor);
            if (this.backStyle.selectColor == 2147483647) {
                target.setBackground(normal);
            } else {
//                target.setClickable(true);
                GradientDrawable select = new GradientDrawable();
                select.setColor(this.backStyle.selectColor);
                select.setCornerRadii(radius);
                select.setStroke(this.backStyle.borderWidth, this.backStyle.borderColor);
                StateListDrawable drawable = new StateListDrawable();
                drawable.addState(new int[]{16842913}, select);
                drawable.addState(new int[]{16842919}, select);
                drawable.addState(new int[]{16842910}, normal);
                drawable.addState(new int[0], normal);
                target.setBackground(drawable);
            }
        }

    }

    /**
     * 將所有已設(shè)定的數(shù)據(jù)打包返回
     *
     * @return StateListDrawable
     */
    public StateListDrawable get() {
        float tl = toFloat(1.6D, (double) this.backStyle.topLeft);
        float tr = toFloat(1.6D, (double) this.backStyle.topRight);
        float bl = toFloat(1.6D, (double) this.backStyle.bottomLeft);
        float br = toFloat(1.6D, (double) this.backStyle.bottomRight);
        float[] radius = new float[]{tl, tl, tr, tr, br, br, bl, bl};
        GradientDrawable normal;
        StateListDrawable drawable;
        if (this.backStyle.colors != null && this.backStyle.colors.length > 0) {
            normal = new GradientDrawable(Orientation.TOP_BOTTOM, this.backStyle.colors);
            normal.setCornerRadii(radius);
            drawable = new StateListDrawable();
            drawable.addState(new int[]{16842913}, normal);
            drawable.addState(new int[]{16842919}, normal);
            drawable.addState(new int[]{16842910}, normal);
            drawable.addState(new int[0], normal);
            return drawable;
        } else if (this.backStyle.normalColor != 2147483647) {
            normal = new GradientDrawable();
            normal.setColor(this.backStyle.normalColor);
            normal.setCornerRadii(radius);
            normal.setStroke(this.backStyle.borderWidth, this.backStyle.borderColor);
            if (this.backStyle.selectColor == 2147483647) {
                drawable = new StateListDrawable();
                drawable.addState(new int[]{16842913}, normal);
                drawable.addState(new int[]{16842919}, normal);
                drawable.addState(new int[]{16842910}, normal);
                drawable.addState(new int[0], normal);
                return drawable;
            } else {
                GradientDrawable select = new GradientDrawable();
                select.setColor(this.backStyle.selectColor);
                select.setCornerRadii(radius);
                select.setStroke(this.backStyle.borderWidth, this.backStyle.borderColor);
                drawable = new StateListDrawable();
                drawable.addState(new int[]{16842913}, select);
                drawable.addState(new int[]{16842919}, select);
                drawable.addState(new int[]{16842910}, normal);
                drawable.addState(new int[0], normal);
                return drawable;
            }
        } else {
            return null;
        }
    }

    private static Float toFloat(double value, double per) {
        return Double.valueOf(value * per).floatValue();
    }

    private static int colorParse(String str) {
        try {
            return Color.parseColor(str);
        } catch (Exception var2) {
            return -3355444;
        }
    }

//    private static int colorByRes(Context context,int resId) {
//        try {
//            return ContextCompat.getColor(context, resId);
//        } catch (Exception var2) {
//            return -3355444;
//        }
//    }

    /***
     * 背景樣式實(shí)體類
     */
    public static class BackStyle {
        protected int topLeft;
        protected int topRight;
        protected int bottomLeft;
        protected int bottomRight;
        protected int borderColor = 2147483647;
        protected int borderWidth;
        protected int normalColor = 2147483647;
        protected int selectColor = 2147483647;
        protected int[] colors;

        protected BackStyle() {
        }

        public BackStyle setColor(int normalColor, int selectColor) {
            this.normalColor = normalColor;
            this.selectColor = selectColor;
            return this;
        }

        public BackStyle setColor(String normalColor, String selectColor) {
            this.normalColor = MyDrawable.colorParse(normalColor);
            this.selectColor = MyDrawable.colorParse(selectColor);
            return this;
        }

//        public BackStyle setColorRes(int normalColor, int selectColor) {
//            this.normalColor = MyDrawable.colorByRes(normalColor);
//            this.selectColor = MyDrawable.colorByRes(selectColor);
//            return this;
//        }

        public BackStyle setColor(int normalColor) {
            this.normalColor = normalColor;
            return this;
        }

        public BackStyle setColor(String normalColor) {
            this.normalColor = MyDrawable.colorParse(normalColor);
            return this;
        }

//        public BackStyle setColorRes(int normalColor) {
//            this.normalColor = MyDrawable.colorByRes(normalColor);
//            return this;
//        }

        public BackStyle setColors(int... colors) {
            this.colors = colors;
            return this;
        }

        public BackStyle setRadius(int radius) {
            this.topLeft = radius;
            this.topRight = radius;
            this.bottomLeft = radius;
            this.bottomRight = radius;
            return this;
        }

        public BackStyle setRadius(int topLeft, int topRight, int bottomLeft, int bottomRight) {
            this.topLeft = topLeft;
            this.topRight = topRight;
            this.bottomLeft = bottomLeft;
            this.bottomRight = bottomRight;
            return this;
        }

        public BackStyle setBorderColor(int borderColor, int borderWidth) {
            this.borderColor = borderColor;
            this.borderWidth = borderWidth;
            return this;
        }

//        public BackStyle setBorderColorRes(int borderColor, int borderWidth) {
//            this.borderColor = MyDrawable.colorByRes(borderColor);
//            this.borderWidth = borderWidth;
//            return this;
//        }

        public BackStyle setBorderColor(String borderColor, int borderWidth) {
            this.borderColor = MyDrawable.colorParse(borderColor);
            this.borderWidth = borderWidth;
            return this;
        }
    }

    /**
     * 前景樣式實(shí)體類
     */
    public static class ForeStyle {
        protected int normalColor = 2147483647;
        protected int selectColor = 2147483647;
        protected int[] colors;

        protected ForeStyle() {
        }

        public ForeStyle setColor(int normalColor, int selectColor) {
            this.normalColor = normalColor;
            this.selectColor = selectColor;
            return this;
        }

        public ForeStyle setColor(String normalColor, String selectColor) {
            this.normalColor = MyDrawable.colorParse(normalColor);
            this.selectColor = MyDrawable.colorParse(selectColor);
            return this;
        }

//        public ForeStyle setColorRes(int normalColor, int selectColor) {
//            this.normalColor = MyDrawable.colorByRes(normalColor);
//            this.selectColor = MyDrawable.colorByRes(selectColor);
//            return this;
//        }

        public ForeStyle setColor(int normalColor) {
            this.normalColor = normalColor;
            return this;
        }

        public ForeStyle setColor(String normalColor) {
            this.normalColor = MyDrawable.colorParse(normalColor);
            return this;
        }

//        public ForeStyle setColorRes(int normalColor) {
//            this.normalColor = MyDrawable.colorByRes(normalColor);
//            return this;
//        }

        public ForeStyle setColors(int... colors) {
            this.colors = colors;
            return this;
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容