Android中XML绘图

在开发当中一直秉承:能用xml绘制的图片效果,就一定用xml来绘制。

因为xml绘制的所占文件大小比图片小很多,一般都小于1K。

从shape,layer-list 2种方式来做整理。
shape:
shape有4种属性,分别描述4种不同的图形:ovale(圆) line:(线) rectangle:(矩形) ring: (环形)
默认为rectangle。

子标签中有gradient、corners、padding、size、solid、stroke;来对图形进行样式描述。

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle / ovale / ring/ line">
    <corners  //角半径 shape = "rectangle" 有用
        android:radius="integer"
        android:topRightRadius="integer"
        android:topLeftRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer"
    />
    <gradient //颜色渐变
        android:type="linear / radial / sweep" //渐变类型
        android:startColor="color"     //渐变开始点的颜色    
        android:centerColor="color"    //渐变中间点的颜色,在开始与结束点之间    
        android:endColor="color"       //渐变结束点的颜色 
        android:angle="integer"        //渐变角度,必须为45的倍数,0为从左到右,90为从上到下 
        android:gradientRadius="float" //渐变的半径,只有当渐变类型为radial时才能使用
        android:centerX="float"      //渐变中心X的相当位置,范围为0~1,从左到右,默认0.5
        android:centerY="float"      //渐变中心Y的相当位置,范围为0~1,从上到下,默认0.5
        android:useLevel="boolean"  //使用LevelListDrawable时就要设置为true。设为false时才有渐变效
    />
    //type = linear时,angle属性才有效;type=radial时,gradientRadius属性必须设置;type = sweep时,       centerX和centerY属性才有效
    <solid   //填充颜色
        android:color = "color"/>
    <stroke  //边框描述
        android:color = "color"
        android:width  = "integer"
        //边框虚线
        android:dashWidth="integer"    //每节虚线的长度
        android:dashGap="integer"          //每节虚线直接的间隙宽度
    />
    <padding  //内边距
        android:left = "integer"
        android:top = "integer"
        android:right = "integer"
        android:bottom = "integer"/>
    <size     //图片大小
        android:width= "integer"
        android:height= "integer"/>
</shape>

其中,< corners />子标签,用来描述图形4个角的半径,只有shape = “rectangle”即矩形是才有用。当shape = “ring” 环形时,设置下面属性

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:innerRadius="integer"  //内环半径
    android:thickness="integer"    //环的宽度
    android:useLevel="false" //boolean值,false值时,才能看见效果。
    android:innerRadiusRatio="float"   //浮点型,以环的宽度比例来表示内环的半径。默认是9,就是环的宽度乘以9再除以2就是内环半径的长度
    android:thicknessRatio = "float"      浮点型,以环的宽度比率来表示环的厚度。>
    </shape>

< gradient/>渐变时
type = linear时,angle属性才有效;当类型type为radial时,gradientRadius属性必须设置;当类型type为sweep时,centerX和centerY属性才有效
layer-list:
图层列表,多个item,先后顺序,依次层叠,实现图层效果。

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

不足之处,多多包涵。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值