如何通過代碼創(chuàng)建shape, 創(chuàng)建layer-list

實例一

通過代碼創(chuàng)建漸變顏色的shape

GradientDrawable shape = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, bgColor);//設(shè)置漸變從左到右
shape.setShape(GradientDrawable.RECTANGLE);//設(shè)置為矩形
shape.setCornerRadius(radius);//設(shè)置圓角

一個相對復雜的demo

比如模仿ios的onOffButton,但是產(chǎn)品經(jīng)理要求改變選中時候的顏色,本來我們是用圖片做的,現(xiàn)在只好用shap自己畫了。
比如下圖:


通過xml來做

bg_switch_on.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:shape="rectangle">
            <corners android:radius="18dp"/>
            <solid android:color="@color/atom_flight_blue_common_color" />
            <!--<size-->
                <!--android:height="34dp"-->
                <!--android:width="52dp" />-->
        </shape>

    </item>
    <item android:left="18dp">
        <shape
            android:shape="oval">
            <solid android:color="@color/atom_browser_common_color_white"/>
            <size android:height="30dp" android:width="30dp"/>
        </shape>
    </item>
</layer-list>

bg_switch_off.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:shape="rectangle">
            <corners android:radius="18dp"/>
            <solid android:color="#FFAAAAAA" />
        </shape>

    </item>
    <item android:right="18dp">
        <shape
            android:shape="oval">
            <solid android:color="#ffffffff"/>
            <size android:height="30dp" android:width="30dp"/>
        </shape>
    </item>
</layer-list>

selector_switch.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/bg_switch_on" android:state_checked="true" />
    <item android:drawable="@drawable/bg_switch_off" android:state_checked="false" />
</selector>

在style中添加button的style

    <style name="MySwitch" parent="@android:style/Widget.CompoundButton">
        <item name="android:button">@drawable/selector_switch</item>
    </style>

在toogleButton中引用:

<ToggleButton
            android:id="@+id/mock_tb_total_switch"
            style="@style/MySwitch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:background="#00000000"
            android:checked="true"
            android:gravity="center"
            android:textOff="@null"
            android:textOn="@null"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

這樣就可以成功改變toogleButton按鈕的樣式了,但是過于麻煩了,也可以直接使用Android本身自帶的Switch,Switch還是很好看的。

而且這種方式有一個問題是長度和高度要成比例,不然很難看。

通過代碼實現(xiàn)這個layer-list

我們也可以通過代碼把上面的layer-list寫出來:

    public static StateListDrawable genOnoffButtonSelector(int color, Context context) {
        StateListDrawable res = new StateListDrawable();
        res.addState(new int[]{android.R.attr.state_checked}, genCheckedDrawable(color));
        res.addState(new int[]{}, context.getResources().getDrawable(R.drawable.atom_flight_toggle_btn_unchecked));
        return res;
    }

    public static Drawable genCheckedDrawable(int color) {
        GradientDrawable roundRect = new GradientDrawable();
        roundRect.setShape(GradientDrawable.RECTANGLE);
        roundRect.setSize(BitmapHelper.dip2px(52), BitmapHelper.dip2px(30));
        roundRect.setCornerRadius(BitmapHelper.dip2px(16));
        roundRect.setColor(color);
        GradientDrawable circle = new GradientDrawable();
        circle.setShape(GradientDrawable.OVAL);
        circle.setSize(BitmapHelper.dip2px(24), BitmapHelper.dip2px(24));
        circle.setColor(Color.parseColor("#ffffff"));
        InsetDrawable insetLayer2 = new InsetDrawable(circle, BitmapHelper.dip2px(23), 4, 3, 4);
        return new LayerDrawable(new Drawable[]{roundRect, insetLayer2});
    }

這里第二個,沒有點擊的狀態(tài)是一張圖片,因為設(shè)計師說那個有陰影效果我畫不出來,所以就直接用的圖片,如果想要做也是一樣的。

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

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,023評論 25 709
  • 每個UIView有一個伙伴稱為layer,一個CALayer。UIView實際上并沒有把自己畫到屏幕上;它繪制本身...
    shenzhenboy閱讀 3,255評論 0 17
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,270評論 4 61
  • 轉(zhuǎn)載:http://www.itdecent.cn/p/32fcadd12108 每個UIView有一個伙伴稱為l...
    F麥子閱讀 6,582評論 0 13
  • 人生即是如此,兜兜轉(zhuǎn)轉(zhuǎn)又回到了起點。大學剛剛過去兩年,忽然間覺得自己變得成熟了很多。以下是一些不成熟的建議。 1...
    人生只若初見kl閱讀 379評論 0 2

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