Drawable子类之—— ShapeDrawable (图形定义)

本文出自 “阿敏其人” 简书博客,转载或引用请注明出处。

ShapeDrawable 一种创建的Drawable,可以理解为通过颜色来狗仔的图形,它既可以是纯色的图形,也可以是具有渐变效果的图形。

ShapeDrawable的xml的根元素是 shape

一、语法

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

.
.

二、子节点

分别这么几个:corners(角度)、gradient(渐变)、padding(距离)、size(大小)、solid(纯色填充)、stroke(描边)。

.
.

根元素:shape

  • android:shape
    表示图形的形状,有四种选项

rectabgle 矩形
  oval 椭圆
  line 横线
  ring 圆环

针对line和ring
四个当中, line 和 ring 一定需要通过 <stroke> 标签来指定 线的宽和和颜色信息 等。

针对ring有5个特殊的属性

作用
android:innerRadius 圆内的内半径,当和innerRadiusRatio同时存在时,以innerRadius为准
android:thickness 厚度, 圆环的厚度 = 外半径 - 内半径 ,当和thicknessRatio一起存在时,以thickness为准
innerRadiusRatio 内半径在占整个Drawable宽度的比例,默认值是9。如果为n,那么 内半径 = 宽度 / n
android:thicknessRatio 厚度占整个Drawable的宽度的比例,默认值是3。如果为n,那么 厚度 = 宽度 / n 。
android:useLevel 一般都应该使用false,否则可能无法达到预期的效果,除非他被用来作为 LevelListDrawable 来使用

.
.

<corners> 角度 ( 只适用于 shape )

表示shape图形四个交的角度,即四个角的圆角程度。单位是px,他有5个属性

| corners的5个属性 | 作用 |
| android:radius | 为四个角同时设定相同的角度,优先级低,会被下面几个覆盖(因为人家是专门指定的)|
|android:topLeftRadius | 左上角的角度 |
| android:topRightRadius | 右上角的角度 |
| android:bottomLeftRadius | 左下角的角度 |
| android:bottomRightRadius | 右下角的角度 |

注意:每个圆角半径值都必须大于1,否侧就没有圆角。

.
.

<solid> 纯色填充

表示纯色填充,利用 android:color 就可以指定shape的颜色。

<gradient> 渐变效果 (与solid互斥,纯色或者渐变只能要一个)

  • android:angle —— 渐变的角度,默认是0,其值必须是45的整数倍。
    0表示从左边到右
    90表示从上到下。具体效果随着角度的调整而产生变化,角度影响渐变方向。
  • android:centerX —— 渐变中心的横坐标点
  • android:centerY —— 渐变中心的纵坐标点
  • android:startColor —— 渐变色的起始色
  • android:centerColor —— 渐变色的中间色
  • android:endColor —— 渐变色的结束色
  • android:type —— 渐变的类型,分3种,linear(线性渐变),radio(径向渐变),sweep(扫描线渐变),默认值是 线性渐变 。
  • android:gradientRadius —— 渐变的半径(仅当 android:type 为radio时有效)
  • android:useLevel —— 一般为false,当Drawable作为StateListDrawable时有效。

.
.

<stroke> 描边

stroke的属性 作用
android:width 描边的宽度,越大则shape的边缘线越粗
android:color 描边的颜色
android:dashWidth 虚线的宽度
android:dashGap 虚线的空隙的间隔

注:如果 android:dashWidth 和 android:dashGap 两者有任意一个为0,那么虚线效果就无法显示。

.
.

<padding>

这个没什么好讲的,就是padding,分上下左右
.
.

<size> shape大小 (只是大小并不是指定了固定了大小)

android:width 指定shape宽
android:height 指定shape高

严格来说shape没有宽高,但是我们指定size就有了所谓的宽高。
当时当shape作为View 的背景的时候,shape还是会被拉伸的,所以这个宽高并不是指定多少就一定多少(对于Drawable都是没有绝对的宽高的)

三、Demo

首先先来四个最简单的图像,矩形、椭圆、线,圆形。

先上xml代码,后上展示图。

�就是这四个.png

线 simple_line.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line"
    >
    <!--line一定需要描边stroke-->
    <stroke
        android:width="10dp"
        android:color="#0000ff"
         />

</shape>

.
.
椭圆 simple_oval.xml

<?xml version="1.0" encoding="utf-8"?>

<!--椭圆-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    >
    <solid android:color="#00ff00"/>
    <corners android:radius="5px"/>

</shape>

.
.
矩形 simple_rectagle.xml

<?xml version="1.0" encoding="utf-8"?>

<!--椭圆-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    >
    <solid android:color="#00ff00"/>
    <corners android:radius="5px"/>

</shape>

.
.
圆形 simple_ring.xml

<?xml version="1.0" encoding="utf-8"?>

<!--  安卓的圆一般来说 外半径 = 内半径 + 厚度-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:useLevel="false"

    android:innerRadius="20dp"
    android:thickness="3dp"
    >

    <stroke
        android:color="#ff0000" />
    <solid android:color="#ff0000" />
</shape>

图形展示.png

以上就是基本图形最基本的使用。

下面结合几个节点做一些demo

圆形的点击变换颜色

<?xml version="1.0" encoding="utf-8"?>
<!--别看这里我们使用的是ovrl(椭圆) ,但是我们得到可是 圆形 的点击效果-->
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape android:shape="oval">
            <solid
                android:color="#ff0000" />
            <stroke
                android:width="4dp"
                android:color="#294736" />
        </shape>
    </item>
    <item >
        <shape android:shape="oval">
            <solid
                android:color="#848374" />
            <stroke
                android:width="4dp"
                android:color="#745863" />
        </shape>
    </item>
</selector>

圆形的点击颜色变化.gif

.
.
Edittext的背景框和焦点变化

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_window_focused="false">
        <shape android:shape="rectangle">
            <solid
                android:color="#FFFFFFFF"/>
            <corners
                android:radius="3dp"/>
            <padding
                android:left="10dp"
                android:right="10dp"/>
            <stroke
                android:width="1dp"
                android:color="#BDC7D8"/>
        </shape>
    </item>

    <item android:state_focused="true" >
        <shape android:shape="rectangle" >
            <solid
                android:color="#FFFFFFFF"/>
            <corners
                android:radius="3dp"/>
            <padding
                android:left="10dp"
                android:right="10dp"/>
            <stroke
                android:width="1dp"
                android:color="#728ea3"/>
        </shape>
    </item>
</selector>

Edittext的焦点变化.gif

如果我想画一个矩形,只有底边怎么办?
就好像在TextView里面,我们需要底线,“我们是有底线的TextView”,但是呢,我又不想弄复制的布局,弄一个背景岂不是方便多了?
嗯,你可以这么做,但是单纯的shape是实现不了的,需要用到LayerDrawable。原理就是shape组合排列。
有兴趣可以跳转一下——Drawable子类之——LayerDrawable (图层叠加)

了解更多的Drawable分类 Drawable图像资源抽象类
本篇完。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266

推荐阅读更多精彩内容