美文网首页Android初学者
drawable xml绘图简单用法

drawable xml绘图简单用法

作者: YueDev | 来源:发表于2018-10-14 10:03 被阅读1次

drawable里的xml文件做绘图资源非常方便,不需要适配屏幕dpi,几个比较简单的用法:

1.sharp

sharp是比较常用的drawable,可以绘制line、oval、rectangle和 ring。以sharp为例绘制一个红色椭圆和蓝色圆环。

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid
        android:color="@android:color/holo_red_light"/>
</shape>

预览下:


sharp_oval_solid_red .png

接着画一个外环宽度为8dp的圆环

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke
        android:width="8dp"
        android:color="@android:color/holo_blue_light"/>
</shape>
sharp_oval_stroke_blue .png

2.layer-list

故名思议,layer-list就是图层,把几个可绘制的drawable排列起来,layer-list最下边的item会放置在最上层,我们把之前的红色圆形和蓝色环形重叠起来,绘制一个带蓝边的红圆。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:shape="oval">
            <solid
                android:color="@android:color/holo_red_light"/>
        </shape>
    </item>


    <item>
        <shape
            android:shape="oval">
            <stroke
                android:width="8dp"
                android:color="@android:color/holo_blue_light"/>
        </shape>
    </item>
</layer-list>

预览:


layer_list.png

3.state-list

state-list是根据对象的状态分别绘制不同的图形,比如的是绘制一个圆形按钮,平时是红色,按下时是蓝色。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true">
        <shape android:shape="oval">
            <solid android:color="@android:color/holo_blue_light"/>
        </shape>
    </item>

    <item>
        <shape android:shape="oval">
            <solid android:color="@android:color/holo_red_light"/>
        </shape>
    </item>
</selector>

需要注意的是,按下状态的item要写在通常状态之前。把这个xml文件设置一个button的background,就可以使用了。

推荐阅读:

https://developer.android.google.cn/guide/topics/resources/drawable-resource

相关文章

网友评论

    本文标题:drawable xml绘图简单用法

    本文链接:https://www.haomeiwen.com/subject/uakzaftx.html