Android shape
2016-05-13 本文已影响165人
许宏川
android的shape就是自己画各种形状,就是一些xml文件,放在drawable目录下。官方文档
使用方法
在drawable目录新建shape资源文件。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="xxx">
</shape>
然后指定shape种类,有四个分类:
android:shape=["rectangle" | "oval" | "line" | "ring"]
分别代表矩形,椭圆,线条和环形。其中rectangle是默认的。
每种shape都有以下6个属性:
size、padding、solid、stroke、corners、gradient。
size
size是最简单的,就是宽和高。
<size
android:width="integer"
android:height="integer" />
padding
内边距。
<padding
android:left="integer"
android:top="integer"
android:right="integer"
android:bottom="integer" />
solid
填充颜色。
<solid
android:color="color" />
stroke
边框,width是边框宽度,color是边框颜色。dashWidth是虚线的线条长度,dashGap是虚线的线条间隔。这里有个问题,怎么设置边框是实线还是虚线?答案是dashGap为0就是实线呗。
<stroke
android:width="integer"
android:color="color"
android:dashWidth="integer"
android:dashGap="integer" />
corners
圆角半径,只有rectangle需要设置这个值。
radius是圆角半径,topLeftRadius、topRightRadius、bottomLeftRadius、bottomRightRadius分别是左上、右上、左下、右下的圆角半径。
<corners
android:radius="integer"
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer" />
gradient
渐变颜色,有三种类型:linear线性,radial放射性,sweep扫描性。
startColor、centerColor和endColor分别代表开始,结束和中间颜色。
angle是linear的属性,值为45的倍数,表示渐变的角度。
gradientRadius是radial的属性,表示发射的半径,如果type为radial这个值必须设置。
centerX和centerY是radial的属性,表示圆心的位置,默认都为0.5表示圆心在正中央。
useLevel为true时能在LevelListDrawable使用。
<gradient
android:type=["linear" | "radial" | "sweep"]
android:startColor="color"
android:centerColor="integer"
android:endColor="color"
android:angle="integer"
android:gradientRadius="integer"
android:centerX="float"
android:centerY="float"
android:useLevel=["true" | "false"] />