Android shape相关

来源:互联网 发布:安庆市网络问政 编辑:程序博客网 时间:2024/06/18 08:39

官方的一些说明是如下,列出了所有的属性:

<?xml version="1.0" encoding="utf-8"?><shape    xmlns:android="http://schemas.android.com/apk/res/android"    android:shape=["rectangle" | "oval" | "line" | "ring"] >   //从这里可以看出,shape可以画四种图案:矩形,椭圆,线,环    <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文件要在Drawable中,定义完成 了shape之后,布局在view的background中就行了。如下
 <Button        android:layout_marginTop="12dp"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="圆角矩形"        android:background="@drawable/shape_rectangle1"/>

具体说一下吧。

直角矩形:直角矩形很简单,改个属性值,填充下颜色就行了

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <solid android:color="@color/colorPrimary"></solid></shape>


圆角矩形:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <corners android:radius="10dp"></corners>    <solid android:color="@color/colorPrimary"></solid>    <padding android:bottom="12dp"        android:left="12dp"        android:right="12dp"        android:top="12dp"></padding></shape>


无填充带边框:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <corners android:radius="10dp"></corners>    <padding android:bottom="12dp"        android:left="12dp"        android:right="12dp"        android:top="12dp"></padding>    <stroke android:width="5dp"         android:color="@color/colorAccent"></stroke>//也可以设置一个solid  填充点颜色</shape>


渐变:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <solid android:color="@color/colorPrimary"></solid>    <padding android:bottom="12dp"        android:left="12dp"        android:right="12dp"        android:top="12dp"></padding>    <!--angle 渐变角度,0:左到右;90:下到上;180:右到左;270:上到下-->    <gradient android:startColor="@android:color/white"        android:endColor="@android:color/black"        android:angle="0"></gradient></shape>

oval其实一般是用来画圆的,设置给一个imageview就行了 。只需要将size属性的宽和高设置成一样的就行了,纯色的圆,渐变的圆:

纯色的原:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="oval">    <solid android:color="@color/colorPrimary"></solid>    <size android:height="100dp"        android:width="100dp"></size></shape>

将一个圆设置成颜色渐变的:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="oval">    <size android:height="100dp"        android:width="100dp"></size>    <gradient android:centerX="0.5"    //渐变的x轴起始位置,选择0-1,0.5代表的是圆心位置        android:centerY="0.5"          //渐变的y轴起始位置,选择0-1,0.5代表的是圆心的位置        android:type="sweep"        android:startColor="@color/colorPrimary"        android:endColor="@color/colorAccent"></gradient></shape>
在渐变中的type属性中有三个值:
linear 线性渐变,默认的渐变类型
radial 放射渐变,设置该项时,android:gradientRadius也必须设置
sweep 扫描性渐变


线:一般所讲的线就是直线或者虚线,在布局中,线的view你可以直接用一个linearlayout就行了,在background中设置shape为一个线就可以。

虚线:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="line">    <stroke        android:width="1dp"            android:color="@color/colorAccent"         android:dashGap="3dp"   //虚线的间距宽度         android:dashWidth="4dp"> //虚线的宽度
   </stroke>    <size android:height="3dp"></size>  //注意:这里的这个height要大于stroke中的width</shape>
 
虚线在设置的时候一定要加上layertype属性为software,否则看不见虚线<LinearLayout        android:layout_marginTop="12dp"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layerType="software"        android:background="@drawable/shape_line"></LinearLayout>


直线:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="line">    <stroke        android:width="1dp"         android:color="@color/colorAccent"></stroke>    <size android:height="3dp"></size></shape>


环:同样的shape设置给一个imageview就行了,环的话也是可以做成纯色的和颜色渐变的。

纯色的环:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="ring"    android:useLevel="false"    android:thickness="10dp">    <!--useLevel需要设置为false-->    <solid android:color="@color/colorAccent"></solid></shape>
颜色渐变的环:


<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="ring"    android:useLevel="false"    android:thickness="10dp">    <!--useLevel需要设置为false-->    <solid android:color="@color/colorAccent"></solid>    <gradient android:startColor="@color/colorAccent"        android:endColor="@color/colorPrimary"        android:type="sweep"></gradient></shape>

比较粗糙,以后用到的时候根据需求再仔细研究吧。