美文网首页Android 技术收集
【android】Android shape常用属性理解

【android】Android shape常用属性理解

作者: 当时不是寻常 | 来源:发表于2017-11-15 10:51 被阅读373次

首先我们看一个布局代码,然后再看效果就比较好理解了

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- 圆角 -->
    <corners
        android:topLeftRadius="5dp"
        android:topRightRadius="10dp"
        android:bottomLeftRadius="15dp"
        android:bottomRightRadius="20dp"/><!-- 设置圆角半径 -->
    <!-- 渐变 -->
    <gradient/>
    <!-- 间隔 很少使用-->
    <padding
        android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp"/><!-- 各方向的间隔 -->
    <!-- 大小 很少使用 -->
    <size
        android:width="100dp"
        android:height="50dp"/><!-- 宽度和高度 -->
    <!-- 填充 -->
    <solid
        android:color="#FF44F83E"/><!-- 填充的颜色 -->
    <!-- 描边 -->
    <stroke
        android:width="5dp"
        android:color="#FFF83E5E"
        android:dashWidth="1dp"
        android:dashGap="1dp"/>
</shape>

效果图如下:


image.png

图中两个比较显眼的颜色效果:


image.png

相信大家平常都知道怎么去用这些属性,这里我着重提一下几个小点吧:
1、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时才有渐变效果   
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> 

我们再看一下线性渐变设置angle的效果:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient
        android:angle="315"
        android:type="linear"
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"/>
</shape> 
image.png
android:angle网 上有各种说法,这里,我说说自己的实验结果,渐变的时候,最原始的,即android:angle=“0”时,是从左到右,按照开始颜色到结束颜色来渲染 的,android:angle=“90”是从下到上来渲染的,android:angle=“180”是从右到左来渲染 的,android:angle=“360”和android:angle=“0”是一样的,所以这里应该是这样的,渲染时按照最原始的渲染色板(把控件内部看作一块可以绕中心旋转的板子)围绕控件中心来旋转相应的度数,即android:angle里面的值就是所需要旋转的角度,只是这个旋转角度必须是45的整数倍
  • 放射性渐变:
<?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> 
  • 扫描式渐变:
<?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> 

2、android:angle属性(仅对线性渐变有效),android:angle="integer" //渐变角度,必须为45的倍数,0为从左到右,90为从上到下

3、android:useLevel
useLevel属性通常不使用。该属性用于指定是否将该shape当成一个LevelListDrawable来使用,默认值为false。

4、stroke的属性理解如下:

image.png

5、size和padding
这两个基本上不怎么用,因为他们所具有的功能,控件本身也能实现。 size:是用来定义图形的大小的。

6、Element shape doesn't have required attribute android:layout_width,android:layout_height
如果你在写shape布局文件的时候一直报这个错误,那就请重点看一下你的这个布局文件是否是放在drawable当中的,你很可能错误的放在了layout文件夹下面。

如果文章当中有任何不正确的地方,还请广大读者纠正,非常感谢!

相关文章

网友评论

    本文标题:【android】Android shape常用属性理解

    本文链接:https://www.haomeiwen.com/subject/mvlqvxtx.html