Android中shape常用属性

1.基本属性(corners、gradient、padding、size、solid、stroke)

Shape的属性(rectangle、oval、line、ring)

android:shape=["rectangle" | "oval" | "line" | "ring"]  
shape的形状,默认为矩形,可以设置为矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)  
下面的属性只有在android:shape="ring时可用:  
android:innerRadius         尺寸,内环的半径。  
android:innerRadiusRatio    浮点型,以环的宽度比率来表示内环的半径,  
android:thickness           尺寸,环的厚度  
android:thicknessRatio      浮点型,以环的宽度比率来表示环的厚度,例如,如果android:thicknessRatio="2",  
android:useLevel            boolean值,如果当做是LevelListDrawable使用时值为true,否则为false. 
可见,只有第一个shape是可用的,其它五个都是shape等于ring时所特有的。

注意,无论这里shape取什么形状,他的子标签都是可用的,但有时并不会有效果,比如在shape为椭圆时,那corners标签就不会有效果,很显然的道理。下面一个个看看各个形状都是怎么样的

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

2、oval(椭圆)

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

3.shape定义:(这里一定要要加上useLevel属性并定义为false,不然没有效果)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="ring" 
    android:innerRadius="20dp" 
    android:thickness="50dp"  
    android:useLevel="false">
    
    <solid android:color="#ff00ff"/>
    
</shape>

(1).Corners( 标签是用来字义圆角的,其中radius与其它四个并不能共同使用。)

<corners 
    android:radius="dimension"      //全部的圆角半径  
    android:topLeftRadius="dimension"   //左上角的圆角半径  
    android:topRightRadius="dimension"  //右上角的圆角半径  
    android:bottomLeftRadius="dimension"    //左下角的圆角半径  
    android:bottomRightRadius="dimension" />    //右下角的圆角半径  

(2)solid(填充颜色)

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

(3)gradient用以定义渐变色,可以定义两色渐变和三色渐变,及渐变样式,它的属性有下面几个:

<gradient 
    android:type=["linear" | "radial" | "sweep"]    //共有3中渐变类型,线性渐变(默认)/放射渐变/扫描式渐变  
    android:angle="integer"     //渐变角度,必须为45的倍数,0为从左到右,90为从上到下  
    android:centerX="float"     //渐变中心X的相当位置,范围为0~1  
    android:centerY="float"     //渐变中心Y的相当位置,范围为0~1  
    android:startColor="color"   //渐变开始点的颜色  
    android:centerColor="color"  //渐变中间点的颜色,在开始与结束点之间  
    android:endColor="color"    //渐变结束点的颜色  
    android:gradientRadius="float"  //渐变的半径,只有当渐变类型为radial时才能使用  
    android:useLevel=["true" | "false"] />  //使用LevelListDrawable时就要设置为true。设为false时才有渐变效果  
首先有三种渐变类型,分别是:linear(线性渐变)、radial(放射性渐变)、sweep(扫描式渐变)

下面我们使用三色渐变来看看这三种渐变方式都是怎么显示的:(如果不使用centerColor属性就是双色渐变,这个属性是可选的)


image.png

需要注意的一点是,在构造放射性渐变时,要加上android:gradientRadius属性(渐变半径),即必须指定渐变半径的大小才会起作用,下面列出这三个渐变方式的shape代码,供大家参考:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        android:type="linear"
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"/>
</shape>

a.放射性渐变:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        android:type="radial"
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"
        android:gradientRadius="100"/>
</shape>

b.扫描式渐变

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        android:type="sweep"
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"/>
</shape>

c、android:angle属性(仅对线性渐变有效)

android:angle="integer"     //渐变角度,必须为45的倍数,0为从左到右,90为从上到下  
我们在上面的三种渐变上都加上angle属性,看看效果如何:
image.png

能过跟上一个图对比可以发现,angle属性确实只对线性渐变有效,其它两种渐变方式都没有任何动静,下面是此时的线性渐变shape代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        android:type="linear"
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"
        android:angle="45"/>
</shape>

d.android:centerX与android:centerY

centerX、centerY两个属性用于设置渐变的中心点位置,仅当渐变类型为放射渐变时有效,类型为分数或小数,不接受Dimension。默认值是0.5,有效值是0.0~1.0,超出该范围后会看不出渐变效果。centerX、centerY的取值其实是宽和高的百分比;不难理解,下面看代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        android:type="sweep"
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"
        android:centerX="0.2"
        android:centerY="0.8"/>
</shape>

(4)stroke(这是描边属性,可以定义描边的宽度,颜色,虚实线等)

<stroke       
    android:width="dimension"   //描边的宽度  
    android:color="color"   //描边的颜色  
    // 以下两个属性设置虚线  
    android:dashWidth="dimension"   //虚线的宽度,值为0时是实线  
    android:dashGap="dimension" />      //虚线的间隔 

上面各个属性的意义如下:


image.png

我们使用绿色虚线描边,虚线高度是20dp,虚线宽度为10dp虚线间距为1dp:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke 
        android:width="20dp" 
        android:color="#00ff00"
        android:dashWidth="10dp"
        android:dashGap="1dp" />
</shape>
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。