android.graphics.drawable.GradientDrawable Shap可以是矩形(矩形可以变成圆角矩形、圆形),椭圆、线、或者圆环(控制内圆半径可以达到圆形的效果)
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape="rectangle|oval|line|ring"
- android:visible=""
- android:dither=""
- android:innerRadiusRatio=""
- android:thicknessRatio=""
- android:innerRadius=""
- android:thickness=""
- android:useLevel=""
- android:tint=""
- android:tintMode=""
- >
-
- </shape>
shap :指定形状 visible:是否可见 dither:是否抗抖动 thickness:环的厚度 thicknessRatio:以环的宽度比率来表示环的厚度,例如,如果android:thicknessRatio="2", 那么环的厚度就等于环的宽度除以2。这个值是可以被android:thickness覆盖的,默认值是3. innerRadius :内环的半径。 innerRadiusRatio :以环的宽度比率来表示内环的半径, 例如,如果android:innerRadiusRatio,表示内环半径等于环的宽度除以5,这个值是可以被覆盖的,默认为9. useLevel :如果当做是LevelListDrawable使用时值为true,否则为false. 1.corners 圆角
- <!--
-
- android:radius 整型 半径
- android:topLeftRadius 整型 左上角半径
- android:topRightRadius 整型 右上角半径
- android:bottomLeftRadius 整型 左下角半径
- android:bottomRightRadius 整型 右下角半径
- -->
- <corners
- android:radius="8dp"
- android:topLeftRadius="5dp"
- android:topRightRadius="15dp"
- android:bottomLeftRadius="20dp"
- android:bottomRightRadius="25dp"
- />
2.Gradient 渐变
- <!--
- 代表渐变颜色的角度, 0 is left to right, 90 is bottom to top.
- 必须是45的整数倍.
- 默认是 0.该属性只有在type=linear情况下起作用,默认的type为linear。
- android:angle="integer"
-
- 渐变开始色,渐变中间色,渐变结束色
- android:startColor="color"
- android:centerColor="color"
- android:endColor="color"
-
- 这两个属性只有在type不为linear情况下起作用。
- 相对X、Y的渐变位置
- android:centerX="float"
- android:centerY="float"
-
- 渐变颜色的半径,单位应该是像素点. 需要 android:type="radial".
- 如果android:type="radial",没有设置android:gradientRadius,将会报 错, error inflating class.
- android:gradientRadius="float"
-
- linear 线性渐变,这是默认设置
- radial 放射性渐变,以开始色为中心。
- sweep 扫描线式的渐变。
- android:type=["linear" | "radial" | "sweep"]
-
- 如果要使用LevelListDrawable对象,就要设置为true。设置为true无渐变。
- false有渐变色
- android:useLevel=["true" | "false"]
-
- -->
-
- <gradient
- android:angle="integer"
- android:centerX="float"
- android:centerY="float"
- android:startColor="color"
- android:centerColor="color"
- android:endColor="color"
- android:gradientRadius="float"
- android:type=["linear" | "radial" | "sweep"]
- android:useLevel=["true" | "false"] />
3.solid 填充色
- <solid android:color="" />
4.padding 内边距
- <padding
- android:left=""
- android:top=""
- android:right=""
- android:bottom=""
- />
5.size 形状大小
- <size android:width=""
- android:height=""
- />
6.stroke 边框
- <!-- android:width=”2dp” 描边的宽度,android:color 描边的颜色。
- 我们还可以把描边弄成虚线的形式,设置方式为:
- android:dashWidth=”5dp”
- android:dashGap=”3dp”
- 其中android:dashWidth表示"-‘这样一个横线的宽度,android:dashGap表示之间隔开的距离 -->
- <stroke
- android:width=""
- android:dashWidth=""
- android:color=""
- android:dashGap=""
- />
sweep 渐变
- <shape xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape="rectangle"
- >
-
- <gradient
- android:centerColor="#00ff00"
- android:endColor="#0000ff"
- android:startColor="#ff0000"
- android:type="sweep"
- android:useLevel="false" />
-
- </shape>
- <shape xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape="rectangle"
- >
-
- <gradient
- android:centerColor="#00ff00"
- android:endColor="#0000ff"
- android:startColor="#ff0000"
- android:type="sweep"
- android:centerX="0.2"
- android:centerY="0.2"
- android:useLevel="false" />
-
- </shape>
linear 渐变 该渐变下有个特有属性 android:angle=""
- <shape xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape="rectangle"
- >
-
- <gradient
- android:centerColor="#00ff00"
- android:endColor="#0000ff"
- android:startColor="#ff0000"
- android:type="linear"
- android:useLevel="false" />
-
- </shape>
radial 渐变 该渐变下有个特有属性 android:gradientRadius="" <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<gradient
android:startColor="#ff0000"
android:centerColor="#00ff00"
android:endColor="#0000ff"
android:type="radial"
android:gradientRadius="400"
android:useLevel="false" />
</shape>
android:centerX="0.2"
android:centerY="0.2"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadiusRatio="8"
android:thicknessRatio="3"
android:useLevel="false"
>
<gradient
android:startColor="#ff0000"
android:centerColor="#00ff00"
android:endColor="#0000ff"
android:type="sweep"
android:useLevel="false" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line"
android:useLevel="false">
<padding android:bottom="30dp" />
<stroke
android:width="2dp"
android:color="#645454"
android:dashWidth="5dp"
android:dashGap="10dp"
/>
</shape>
你很可能看到的是一条实线,Android 4.0 默认打开了硬件加速,我们把硬件加速关掉就行, yourView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:useLevel="false">
<corners android:radius="5dp" />
<solid android:color="@android:color/white" />
<stroke
android:width="2dp"
android:color="#a3d458"
/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:useLevel="false">
<corners android:radius="5dp"/>
<solid android:color="@android:color/white" />
<stroke
android:width="2dp"
android:color="#a3d458"
android:dashWidth="5dp"
android:dashGap="10dp"
/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:useLevel="false">
<corners
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
/>
<solid android:color="@android:color/white" />
<stroke
android:width="2dp"
android:color="#a3d458"
/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:useLevel="false">
<corners android:radius="10dp"/>
<gradient
android:startColor="#ff115a"
android:endColor="#ff9083"
android:angle="270"
/>
<stroke
android:width="2dp"
android:color="#a3d458"
/>
</shape>
|