Shape

来源:互联网 发布:大数据入门经典书籍 编辑:程序博客网 时间:2024/06/16 01:34

Shape

有时候如果自己缺一张或两张的圆形图片,什么颜色边框的时候。因为事情量太少了,麻烦多美工又不好。这时候就需要安卓的shape出场了。

先来了解一下Shape可以干什么

当然是画图,Shape有椭圆形 圆形 线性和伞形,如果都会这些美工再也不担心你我天天烦着他

该如何做

shape有四种模式 默认模式为正方形

<!--图像形状 参数有 rectangle(正方形)oval(椭圆) line(线)ring(环形)-->
下面就是一个最简单的正方形
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"   android:shape="rectangle"><!--表示空心--><stroke    android:width="10dp"    android:color="@color/colorPrimaryDark"/><!--表示实心--><solid android:color="@color/colorAccent"/><!--设置大小--><size android:height="100dp"      android:width="100dp"/></shape>

效果如下

![](http://i.imgur.com/vzTDZpD.png)

这样的程度肯定难不倒聪明的你

然后就是椭圆形
    <?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"       android:dither="false"       android:shape="oval">    <stroke        android:width="10dp"        android:color="@color/colorPrimaryDark"/>    <!--表示实心-->    <solid android:color="@color/colorAccent"/>    <padding android:bottom="1dp"        android:top="1dp"        android:left="1dp"        android:right="1dp"/>    <!--设置大小-->    <size android:height="100dp"          android:width="100dp"/></shape>

你可根据size的大小设置圆形还是椭圆

然后就是线
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"       android:dither="false"       android:useLevel="true"       android:shape="line">    <stroke        android:width="1dp"        android:color="@color/colorPrimaryDark"/>    <!--设置大小-->    <size android:height="100dp"          android:width="100dp"/></shape>
然后就是
<?xml version="1.0" encoding="utf-8"?>    <!--下面的属性只有在android:shape="ring时可用:-->    <!--android:innerRadius         尺寸,内环的半径。-->    <!--android:innerRadiusRatio    浮点型,以环的宽度比率来表示内环的半径,-->    <!--例如,如果android:innerRadiusRatio,表示内环半径等于环的宽度除以5,这个值是可以被覆盖的,默认为9.-->    <!--android:thickness           尺寸,环的厚度-->    <!--android:thicknessRatio      浮点型,以环的宽度比率来表示环的厚度,例如,如果android:thicknessRatio="2",-->    <!--那么环的厚度就等于环的宽度除以2。这个值是可以被android:thickness覆盖的,默认值是3.-->    <!--android:useLevel            boolean值,如果当做是LevelListDrawable使用时值为true,否则为false.-->    <!--android:innerRadius="@dimen/activity_horizontal_margin"-->    <!--android:innerRadiusRatio=""-->    <!--android:thicknessRatio=""-->    <!--android:thickness="@dimen/activity_horizontal_margin"--><shape xmlns:android="http://schemas.android.com/apk/res/android"        android:thickness="10dp"       android:innerRadius="20dp"       android:useLevel="false"       android:shape="ring">    <!--表示空心-->    <stroke        android:width="1dp"        android:color="@color/colorPrimaryDark"/>    <!--表示实心-->    <solid android:color="@color/colorAccent"/>    <!--设置大小-->    <size android:height="100dp"          android:width="100dp"/></shape>
然后还可以画虚线
    <?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"       android:dither="false"       android:useLevel="true"       android:shape="line">    <!--表示边距的颜色-->    <!--width边距的宽度-->    <!--colcor边距的颜色-->    <!--android:dashWidth   整型  表示描边的样式是虚线的宽度, 值为0时,表示为实线。值大于0则为虚线。-->    <!--android:dashGap     整型  表示描边为虚线时,虚线之间的间隔 即“ - - - - ”-->    -->    <!--表示空心-->    <!--line状态下 只有这个生效-->    <stroke        android:width="10dp"        android:dashGap="12dp"        android:dashWidth="10dp"        android:color="@color/colorAccent"/>    <!--设置大小-->    <size android:height="100dp"          android:width="100dp"/></shape>

画虚线的时候需要注意的是
从android3.0开始,安卓关闭了硬件加速功能,所以就不能显示了,所以就是在 AndroidManifest.xml,或者是在activity中把硬件加速的功能关掉就可以了android:hardwareAccelerated=”false”或者是view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

Demo

位置

如果想了解更多属性请参照这里

0 0
原创粉丝点击