替代Shape文件姻报,相對(duì)圓角布局

平時(shí)看見(jiàn) UI 設(shè)計(jì)的圖里面有許多的圓角和漸變色,相信做Android的同志對(duì)這個(gè)很熟悉了间螟。
可以用Shape 文件實(shí)現(xiàn)上面的效果吴旋,但是當(dāng)Shape文件變多了呢,咋辦厢破,這東西又不好管理荣瑟。所以我自己自定義了一個(gè)相對(duì)布局,直接可以在布局中聲明圓角漸變色摩泪。

首先來(lái)看效果吧

image.png

貼代碼


/**
 * @description 實(shí)現(xiàn)自定義圓角相對(duì)布局
 * 支持
 * 1.四邊圓角
 * 2.指定邊圓角
 * 3.支持填充色以及邊框色,邊框虛線
 */

public class ShapeRelativeLayout extends RelativeLayout {

    //自定背景邊框Drawable
    private GradientDrawable gradientDrawable;
    //填充色
    private int solidColor = 0;
    //漸變色
    private int startColor = 0;
    private int endColor = 0;
    //邊框色
    private int strokeColor = 0;
    //邊框?qū)挾?    private int strokeWidth = 0;
    //漸變方向 0 是從左到右 1是從上到下
    private int orientation = 0;
    //四個(gè)角的弧度
    private float radius;
    private float topLeftRadius;
    private float topRightRadius;
    private float bottomLeftRadius;
    private float bottomRightRadius;
    //邊框虛線的寬度
    float dashWidth = 0;
    //邊框虛線的間隙
    float dashGap = 0;

    public ShapeRelativeLayout(Context context) {
        this(context,null);
    }

    public ShapeRelativeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShapeRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
        if (startColor != 0 && endColor != 0) {
            //漸變背景
            gradientDrawable = getLineDrawable(new float[]{topLeftRadius, topLeftRadius, topRightRadius, topRightRadius,
                            bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius},
                    startColor, endColor, strokeWidth, strokeColor, dashWidth, dashGap, orientation);
        } else {
            //默認(rèn)背景
            gradientDrawable = getNeedDrawable(new float[]{topLeftRadius, topLeftRadius, topRightRadius, topRightRadius,
                            bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius},
                    solidColor, strokeWidth, strokeColor, dashWidth, dashGap);
        }


        setBackground(gradientDrawable);
    }


    /**
     * 初始化參數(shù)
     *
     * @param context
     * @param attrs
     */
    private void init(Context context, AttributeSet attrs) {
        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ShapeLayout, 0, 0);
        //填充顏色
        solidColor = ta.getInteger(R.styleable.ShapeLayout_solidColor, 0x00000000);
        //邊框顏色
        strokeColor = ta.getInteger(R.styleable.ShapeLayout_strokeColor, 0x00000000);
        startColor = ta.getInteger(R.styleable.ShapeLayout_startColor, 0x00000000);
        endColor = ta.getInteger(R.styleable.ShapeLayout_endColor, 0x00000000);
        //邊框?qū)挾?        strokeWidth = (int) ta.getDimension(R.styleable.ShapeLayout_strokeWidth, 0);
        //四個(gè)角單獨(dú)設(shè)置會(huì)覆蓋radius設(shè)置
        radius = ta.getDimension(R.styleable.ShapeLayout_radius, 0);
        topLeftRadius = ta.getDimension(R.styleable.ShapeLayout_topLeftRadius, radius);
        topRightRadius = ta.getDimension(R.styleable.ShapeLayout_topRightRadius, radius);
        bottomLeftRadius = ta.getDimension(R.styleable.ShapeLayout_bottomLeftRadius, radius);
        bottomRightRadius = ta.getDimension(R.styleable.ShapeLayout_bottomRightRadius, radius);

        dashGap = ta.getDimension(R.styleable.ShapeLayout_dashGap, 0);
        dashWidth = ta.getDimension(R.styleable.ShapeLayout_dashWidth, 0);

        orientation = ta.getInt(R.styleable.ShapeLayout_orientation, 0);
        ta.recycle();
    }


    /**
     * @param radius      四個(gè)角的半徑
     * @param bgColor     背景顏色
     * @param strokeWidth 邊框?qū)挾?     * @param strokeColor 邊框顏色
     * @param dashWidth   虛線邊框?qū)挾?     * @param dashGap     虛線邊框間隙
     * @return
     */
    public static GradientDrawable getNeedDrawable(float[] radius, int bgColor, int strokeWidth, int strokeColor, float dashWidth, float dashGap) {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setCornerRadii(radius);
        drawable.setStroke(strokeWidth, strokeColor, dashWidth, dashGap);
        drawable.setColor(bgColor);
        return drawable;
    }


    public static GradientDrawable getLineDrawable(float[] radius, int startColor, int endColor, int strokeWidth, int strokeColor, float dashWidth, float dashGap, int orientation) {
        GradientDrawable drawable = new GradientDrawable();
        if (orientation == 0) {
            drawable.setOrientation(GradientDrawable.Orientation.TOP_BOTTOM);
        } else if (orientation == 1) {
            drawable.setOrientation(GradientDrawable.Orientation.LEFT_RIGHT);
        }
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setCornerRadii(radius);
        drawable.setStroke(strokeWidth, strokeColor, dashWidth, dashGap);
        drawable.setColors(new int[]{startColor, endColor});

        return drawable;
    }


}

自定義圓角線性布局


public class ShapeLinearLayout extends LinearLayout {

    //自定背景邊框Drawable
    private GradientDrawable gradientDrawable;
    //填充色
    private int solidColor = 0;
    //漸變色
    private int startColor = 0;
    private int endColor = 0;
    //邊框色
    private int strokeColor = 0;
    //邊框?qū)挾?    private int strokeWidth = 0;
    //漸變方向 0 是從左到右 1是從上到下
    private int orientation = 0;
    //四個(gè)角的弧度
    private float radius;
    private float topLeftRadius;
    private float topRightRadius;
    private float bottomLeftRadius;
    private float bottomRightRadius;
    //邊框虛線的寬度
    float dashWidth = 0;
    //邊框虛線的間隙
    float dashGap = 0;

    public ShapeLinearLayout(Context context) {
        this(context,null);
    }

    public ShapeLinearLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ShapeLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
        if (startColor != 0 && endColor != 0) {
            //漸變背景
            gradientDrawable = getLineDrawable(new float[]{topLeftRadius, topLeftRadius, topRightRadius, topRightRadius,
                            bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius},
                    startColor, endColor, strokeWidth, strokeColor, dashWidth, dashGap, orientation);
        } else {
            //默認(rèn)背景
            gradientDrawable = getNeedDrawable(new float[]{topLeftRadius, topLeftRadius, topRightRadius, topRightRadius,
                            bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius},
                    solidColor, strokeWidth, strokeColor, dashWidth, dashGap);
        }


        setBackground(gradientDrawable);
    }


    /**
     * 初始化參數(shù)
     *
     * @param context
     * @param attrs
     */
    private void init(Context context, AttributeSet attrs) {
        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ShapeLayout, 0, 0);
        //填充顏色
        solidColor = ta.getInteger(R.styleable.ShapeLayout_solidColor, 0x00000000);
        //邊框顏色
        strokeColor = ta.getInteger(R.styleable.ShapeLayout_strokeColor, 0x00000000);
        startColor = ta.getInteger(R.styleable.ShapeLayout_startColor, 0x00000000);
        endColor = ta.getInteger(R.styleable.ShapeLayout_endColor, 0x00000000);
        //邊框?qū)挾?        strokeWidth = (int) ta.getDimension(R.styleable.ShapeLayout_strokeWidth, 0);
        //四個(gè)角單獨(dú)設(shè)置會(huì)覆蓋radius設(shè)置
        radius = ta.getDimension(R.styleable.ShapeLayout_radius, 0);
        topLeftRadius = ta.getDimension(R.styleable.ShapeLayout_topLeftRadius, radius);
        topRightRadius = ta.getDimension(R.styleable.ShapeLayout_topRightRadius, radius);
        bottomLeftRadius = ta.getDimension(R.styleable.ShapeLayout_bottomLeftRadius, radius);
        bottomRightRadius = ta.getDimension(R.styleable.ShapeLayout_bottomRightRadius, radius);

        dashGap = ta.getDimension(R.styleable.ShapeLayout_dashGap, 0);
        dashWidth = ta.getDimension(R.styleable.ShapeLayout_dashWidth, 0);

        orientation = ta.getInt(R.styleable.ShapeLayout_orientation, 0);
        ta.recycle();
    }


    /**
     * @param radius      四個(gè)角的半徑
     * @param bgColor     背景顏色
     * @param strokeWidth 邊框?qū)挾?     * @param strokeColor 邊框顏色
     * @param dashWidth   虛線邊框?qū)挾?     * @param dashGap     虛線邊框間隙
     * @return
     */
    public static GradientDrawable getNeedDrawable(float[] radius, int bgColor, int strokeWidth, int strokeColor, float dashWidth, float dashGap) {
        GradientDrawable drawable = new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setCornerRadii(radius);
        drawable.setStroke(strokeWidth, strokeColor, dashWidth, dashGap);
        drawable.setColor(bgColor);
        return drawable;
    }


    public static GradientDrawable getLineDrawable(float[] radius, int startColor, int endColor, int strokeWidth, int strokeColor, float dashWidth, float dashGap, int orientation) {
        GradientDrawable drawable = new GradientDrawable();
        if (orientation == 0) {
            drawable.setOrientation(GradientDrawable.Orientation.TOP_BOTTOM);
        } else if (orientation == 1) {
            drawable.setOrientation(GradientDrawable.Orientation.LEFT_RIGHT);
        }
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setCornerRadii(radius);
        drawable.setStroke(strokeWidth, strokeColor, dashWidth, dashGap);
        drawable.setColors(new int[]{startColor, endColor});

        return drawable;
    }


}

自定義屬性

  <!--RelativeLayout-->
    <declare-styleable name="ShapeLayout">
        <!--填充色-->
        <attr name="solidColor" format="color" />
        <!--邊框色-->
        <attr name="strokeColor" format="color" />
        <!--邊框?qū)挾?->
        <attr name="strokeWidth" format="dimension" />
        <!--設(shè)置漸變色-->
        <attr name="startColor" format="color" />
        <attr name="endColor" format="color" />
        <!--圓角弧度-->
        <attr name="radius" format="dimension" />
        <!--四個(gè)角的圓角弧度-->
        <attr name="topLeftRadius" format="dimension" />
        <attr name="topRightRadius" format="dimension" />
        <attr name="bottomLeftRadius" format="dimension" />
        <attr name="bottomRightRadius" format="dimension" />
        <!--虛線邊框?qū)挾?->
        <attr name="dashWidth" format="dimension" />
        <!--虛線邊框間隙-->
        <attr name="dashGap" format="dimension" />
        <!-- Standard orientation constant. -->
        <attr name="orientation">
            <!-- 漸變從左至右 -->
            <enum name="horizontal" value="0" />
            <!-- 漸變從上至下 -->
            <enum name="vertical" value="1" />
        </attr>
    </declare-styleable>

使用方式

<com.example.ShapeRelativeLayout
        app:startColor="#FF9326"
        app:endColor="#FFC54E"
        android:layout_width="200dp"
        app:radius="5dp"
        app:orientation="vertical"
        android:layout_height="100dp"
        android:layout_marginTop="220dp"
        >

    </com.example.ShapeRelativeLayout>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末笆焰,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子见坑,更是在濱河造成了極大的恐慌嚷掠,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,406評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件荞驴,死亡現(xiàn)場(chǎng)離奇詭異不皆,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)熊楼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門霹娄,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人鲫骗,你說(shuō)我怎么就攤上這事犬耻。” “怎么了执泰?”我有些...
    開(kāi)封第一講書人閱讀 163,711評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵枕磁,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我术吝,道長(zhǎng)计济,這世上最難降的妖魔是什么晴楔? 我笑而不...
    開(kāi)封第一講書人閱讀 58,380評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮峭咒,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘纪岁。我一直安慰自己凑队,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布幔翰。 她就那樣靜靜地躺著漩氨,像睡著了一般。 火紅的嫁衣襯著肌膚如雪遗增。 梳的紋絲不亂的頭發(fā)上叫惊,一...
    開(kāi)封第一講書人閱讀 51,301評(píng)論 1 301
  • 那天,我揣著相機(jī)與錄音做修,去河邊找鬼霍狰。 笑死,一個(gè)胖子當(dāng)著我的面吹牛饰及,可吹牛的內(nèi)容都是我干的蔗坯。 我是一名探鬼主播,決...
    沈念sama閱讀 40,145評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼燎含,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼宾濒!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起屏箍,我...
    開(kāi)封第一講書人閱讀 39,008評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤绘梦,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后赴魁,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體卸奉,經(jīng)...
    沈念sama閱讀 45,443評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評(píng)論 3 334
  • 正文 我和宋清朗相戀三年尚粘,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了择卦。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,795評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡郎嫁,死狀恐怖秉继,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情泽铛,我是刑警寧澤尚辑,帶...
    沈念sama閱讀 35,501評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站盔腔,受9級(jí)特大地震影響杠茬,放射性物質(zhì)發(fā)生泄漏月褥。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評(píng)論 3 328
  • 文/蒙蒙 一瓢喉、第九天 我趴在偏房一處隱蔽的房頂上張望宁赤。 院中可真熱鬧,春花似錦栓票、人聲如沸决左。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,731評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)佛猛。三九已至,卻和暖如春坠狡,著一層夾襖步出監(jiān)牢的瞬間继找,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 32,865評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工逃沿, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留婴渡,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,899評(píng)論 2 370
  • 正文 我出身青樓凯亮,卻偏偏與公主長(zhǎng)得像缩搅,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子触幼,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容