您的位置:首页 > 移动开发 > Android开发

Android shape drawable详解

2016-03-23 14:41 495 查看
在Android开发中经常需要自定义一些形状,这个时候就要用到shape drawable。

Android Developer : http://developer.android.com/intl/zh-cn/guide/topics/resources/drawable-resource.html#Shape

创建及使用shape drawable

文件位置:res/drawable/filename.xml

编译资源类型:GradientDrawable

资源引用:

- In Java: R.drawable.filename

- In XML: @drawable/filename

具体结构:

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle" | "oval" | "line" | "ring"] >
<corners
android:radius="integer"
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer" />
<gradient
android:angle="integer"
android:centerX="integer"
android:centerY="integer"
android:centerColor="integer"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type=["linear" | "radial" | "sweep"]
android:useLevel=["true" | "false"] />
<padding
android:left="integer"
android:top="integer"
android:right="integer"
android:bottom="integer" />
<size
android:width="integer"
android:height="integer" />
<solid
android:color="color" />
<stroke
android:width="integer"
android:color="color"
android:dashWidth="integer"
android:dashGap="integer" />
</shape>


Elements说明

shape:根节点

<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle" | "oval" | "line" | "ring"] //矩形|椭圆|线|圆环

//当android:shape="ring"时,还需要指定以下属性:
android:innerRadius:指定内环的半径
android:thickness:指定环的厚度
android:innerRadiusRatio:宽度比,如果该值为2,则内环半径等于环的宽度除以5
android:thicknessRatio:宽度比,如果该值为2,则环的厚度等于环的宽度除以2
android:useLevel:当为ring时,该属性必须指定为false
>


stroke:边框

<stroke
android:width="dimension"   //边框宽度
android:color="color"       //边框颜色
android:dashWidth="dimension"   //虚线宽度
android:dashGap="dimension" />  //虚线间隔


solid:填充色

<solid  android:color="color" />


gradient:渐变

<gradient
android:type=["linear" | "radial" | "sweep"]    //共有3中渐变类型,线性渐变(默认)/放射渐变/扫描式渐变
android:angle="integer"     //渐变角度,必须为45的倍数,0为从左到右,90为从上到下
android:centerX="float"     //渐变中心X的相当位置,范围为0~1
android:centerY="float"     //渐变中心Y的相当位置,范围为0~1
android:startColor="color"   //渐变开始点的颜色
android:centerColor="color"  //渐变中间点的颜色,在开始与结束点之间
android:endColor="color"    //渐变结束点的颜色
android:gradientRadius="float"  //渐变的半径,只有当渐变类型为radial时才能使用
android:useLevel=["true" | "false"] />  //使用LevelListDrawable时就要设置为true。设为false时才有渐变效果




corners:圆角半径

这个element仅仅在rectangle时使用

<corners
android:radius="integer"   //该属性指定四个圆角的半径,不能和其他属性重用
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer" />


size:shape大小

<size
android:width="integer"
android:height="integer" />


padding:内边距

<padding
android:left="integer"
android:top="integer"
android:right="integer"
android:bottom="integer" />


size和padding在具体控件也可以指定。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: