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

shape在Android中的使用

2016-01-05 14:42 337 查看
shape用于设定形状,可以在selector,layout,layer-list等里面使用,如果有需求是实现一些简单的几何图形可以用shape来实现。

根据交互设计的需要,可以考虑用shape绘制按钮,好处有:

矢量绘制,易于缩放;

字节数更少(一般而言);

基于XML文本,属性值易于调整;

Drawable组件间可嵌套,可重用;

XML与项目其他源代码在一起,便于版本控制。

当然也有缺点:

没有可视化的编辑器,编辑不够直观;

受限于基本的图形和填充方式;

美工人员很难上手。

shape有6个子标签,各属性如下:

<?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>


shape:形状

android:shape="rectangle" //设置形状


共有四种rectangle(矩形),oval(椭圆形),line(线),ring(环),不设置该属性则默认为矩形。

corners:圆角

android:radius="20dp"
为角的弧度,值越大角越圆。

我们还可以把四个角设定成不同的角度

android:topRightRadius="20dp"     //右上角
android:bottomLeftRadius="20dp"   //右下角
android:topLeftRadius="1dp"       //左上角
android:bottomRightRadius="0dp"   //左下角


以上四个属性都已设置时,android:radius属性就会失效。

gradient:渐变

android:startColor="@android:color/white"  //起始颜色
android:centerColor="@android:color/black" //中心颜色
android:endColor="@android:color/black"    //结束颜色
android:angle="45"                         //渐变角度(必须为45的倍数)
android:type="radial"                      //渐变类型
android:centerX="0"                        //中心点的X坐标(0-1.0)
android:centerY="0"                        //中心点的Y坐标(0-1.0)
android:gradientRadius="90"                //径向渐变指定半径


渐变的时候,最原始的,即android:angle=“0”时,是从左到右,按照开始颜色到结束颜色来渲染的,android:angle=“90”是从上到下来渲染的,android:angle=“180”是从右到左来渲染的,android:angle=“360”和android:angle=“0”是一样的,所以这里应该是这样的,渲染时按照最原始的渲染色板(把控件内部看作一块可以绕中心旋转的板子)围绕控件中心来旋转相应的度数,即android:angle里面的值就是所需要旋转的角度,只是这个旋转角度必须是45的整数倍。

默认的渐变模式为android:type=”linear”,即线性渐变,可以指定渐变为径向渐变,android:type=”radial”,径向渐变需要指定半径android:gradientRadius=”50”。

padding:间隔

设置实际内容与控件之间的边距

android:bottom="2dp"   //下边距
android:left="2dp"     //左边距
android:right="2dp"    //右边距
android:top="2dp"      //上边距


size:大小

设置图像的大小

android:height="50dp"  //高
android:width="50dp"   //宽


solid:填充

填充底色

android:color="@android:color/white"    //底色


stroke:描边

android:width="20dp"                    //设置边边的宽度
android:color="@android:color/black"    //设置边边的颜色
android:dashWidth="2dp"                 //设置虚线的宽度
android:dashGap="20dp"                  //设置虚线的间隔宽度


当dashWidth属性或者dashGap属性,任意一个设置为0dp时则显示的是实线。

参考自:

/article/1598592.html

/article/8519063.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: