Android XML绘图

1.Shape

在XML中使用Shape可以绘制各种不同的形状,,代码如下: 
注意:在创建shape文件的时候要将工程切换到project模式下。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="ring">

<!--半径-->
    <corners
        android:radius="10dp"
        android:topLeftRadius=""
        android:topRightRadius=""
        android:bottomLeftRadius=""
        android:bottomRightRadius=""/>


    <!--渐变-->
    <gradient
        android:angle=""
        android:centerColor=""
        android:centerX=""
        android:centerY=""
        android:endColor=""
        android:startColor=""
        android:type="["linear","sweep","radial"]"/>
    <!--间距-->
    <padding
        android:left=""
        android:right=""
        android:top=""
        android:bottom=""/>

    <!--大小-->
    <size
        android:width=""
        android:height=""/>

    <!--填充颜色,实心-->
    <solid
        android:color=""/>

    <!--描边-->
    <stroke
        android:width=""
        android:color=""
        android:dashGap=""      //虚线间隔宽度
        android:dashWidth=""/>//虚线宽度
</shape>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

2.layer

layer主要用来实现图层的效果:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:drawable="@drawable/ic_launcher"/>

    <item
        android:drawable="@drawable/ic_launcher1"
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp"/>

</layer-list>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

通过layer或者是layer_list可以实现ps中的图层效果,图片会依次的的叠加。

3.Selector

Selector的作用在于帮助开发者实现静态绘图中的事件反馈,通过给不同的事件设置不同的图像,从而在程序中根据用户输入,返回不同的效果。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--默认的图片-->
    <item android:drawable="" />

    <!--没有焦点时的的背景图片-->
    <item
        android:drawable=""
        android:state_window_focused="false"/>

    <!--单击图片的时候背景图片-->
    <item
        android:drawable=""
        android:state_pressed="true"/>

    <!--选中那个图片时的的背景-->
    <item
        android:drawable=""
        android:state_selected="true"/>
</selector>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

selector可以制作view的触摸反馈。通过配置不同的触发事件,selector可以选择不同的图片,特别是在自定义图片的时候,我们就可以不使用原生的点击背景了,可以设置不同的点击背景颜色,给予用户点击反馈。

通常情况下,以上的几个xml用法是可以结合使用共同作用的

posted @ 2016-11-26 16:07  天涯海角路  阅读(78)  评论(0)    收藏  举报