ShapeDrawable

作者: 难得糊涂与君勉 | 来源:发表于2018-01-04 14:18 被阅读525次

android.graphics.drawable.ShapeDrawable

父类

android.graphics.drawable.Drawable

其他Draw文章参考:
Android中Drawable整体介绍

介绍

语法用起来比较复杂

理解为通过颜色来构造的图形,既可以是纯色的图形,也可以是渐变的图形,理解为图形

绘制原始形状的可绘制对象。 一个ShapeDrawable需要一个Shape对象并管理它在屏幕上的存在。 如果没有给出Shape,那么ShapeDrawable将默认为RectShape。

这个对象可以使用<shape>元素在XML文件中定义。

在XML中定义形状

<?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="float"
        android:centerY="float"
        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>

(1)xmlns:android:

字符串。必备。定义 XML 命名空间,其必须是 "http://schemas.android.com/apk/res/android"。

(2)android:shape

关键字。定义形状的类型。有效值为:
"rectangle" 填充包含视图的矩形。这是默认形状。
"oval"  适应包含视图尺寸的椭圆形状。
"line"  跨越包含视图宽度的水平线。此形状需要 <stroke> 元素定义线宽。
"ring"  环形。

仅当 android:shape="ring" 如下时才使用以下属性:

android:innerRadius
尺寸。环内部(中间的孔)的半径,以尺寸值或尺寸资源表示。如何和android:innnerRadiusRatio同时有,以这个为准

android:innerRadiusRatio
浮点型。环内部的半径,以环宽度的比率表示。例如,如果 android:innerRadiusRatio="5",
则内半径等于环宽度除以 5。此值被 android:innerRadius 覆盖。默认值为 9。

android:thickness
尺寸。环的厚度,以尺寸值或尺寸资源表示。即外半径减去内半径的大小,如果android:thicknessRatio都有,以这个为准

android:thicknessRatio
浮点型。环的厚度,表示为环宽度的比率。例如,如果 android:thicknessRatio="2",
则厚度等于环宽度除以 2。此值被 android:innerRadius 覆盖。默认值为 3。

android:useLevel
布尔值。如果这用作 LevelListDrawable,则此值为“true”。这通常应为“false”,否则形状不会显示。

(3)corners

<corners>
为形状产生圆角。仅当形状为矩形时适用。
属性:

android:radius
尺寸。所有角的半径,以尺寸值或尺寸资源表示。对于每个角,这会被以下属性覆盖。其优先级比较低
android:topLeftRadius
尺寸。左上角的半径,以尺寸值或尺寸资源表示。
android:topRightRadius
尺寸。右上角的半径,以尺寸值或尺寸资源表示。
android:bottomLeftRadius
尺寸。左下角的半径,以尺寸值或尺寸资源表示。
android:bottomRightRadius
尺寸。右下角的半径,以尺寸值或尺寸资源表示。
注:(最初)必须为每个角提供大于 1 的角半径,否则无法产生圆角。如果希望特定角不要倒圆角
,解决方法是使用 android:radius 设置大于 1 的默认角半径,然后使用实际所需的值替换每个角,为不希望倒圆角的角提供零(“0dp”)。

(3)gradient 梯度

<gradient>
指定形状的渐变颜色。他与<solid>标签是互斥的,
属性:

android:angle
整型。渐变的角度(度)。0 为从左到右,90 为从上到上。必须是 45 的倍数。默认值为 0。
android:centerX
浮点型。渐变中心的相对 X 轴位置 (0 - 1.0)。
android:centerY
浮点型。渐变中心的相对 Y 轴位置 (0 - 1.0)。
android:centerColor
颜色。起始颜色与结束颜色之间的可选颜色,以十六进制值或颜色资源表示。
android:endColor
颜色。结束颜色,表示为十六进制值或颜色资源。
android:gradientRadius
浮点型。渐变的半径。仅在 android:type="radial" 时适用。
android:startColor
颜色。起始颜色,表示为十六进制值或颜色资源。
android:type
关键字。要应用的渐变图案的类型。
有效值为:
         "linear"   线性渐变。这是默认值。
         "radial"   径向渐变。起始颜色为中心颜色。
         "sweep"    流线型渐变。
android:useLevel
布尔值。如果这用作 LevelListDrawable,则此值为“true”。

(4)padding

<padding>
要应用到包含视图元素的内边距(这会填充视图内容的位置,而非形状)。
属性:

android:left
尺寸。左内边距,表示为尺寸值或尺寸资源
android:top
尺寸。上内边距,表示为尺寸值或尺寸资源
android:right
尺寸。右内边距,表示为尺寸值或尺寸资源
android:bottom
尺寸。下内边距,表示为尺寸值或尺寸资源

(5)size

<size>
形状的大小。
属性:

android:height
尺寸。形状的高度,表示为尺寸值或尺寸资源
android:width
尺寸。形状的宽度,表示为尺寸值或尺寸资源
注:默认情况下,形状按照此处定义的尺寸按比例缩放至容器视图的大小。在 ImageView 中使用形状时,
可通过将 android:scaleType 设置为 "center" 来限制缩放。

(6)solid

<solid>
用于填充形状的纯色。
属性:

android:color
颜色。应用于形状的颜色,以十六进制值或颜色资源表示。

(7)stroke

<stroke>
形状的笔划中线。
属性:

android:width
尺寸。线宽,以尺寸值或尺寸资源表示。描边的宽度,越大则shape的边缘就会越粗
android:color
颜色。线的颜色,表示为十六进制值或颜色资源。描边的颜色
android:dashGap
尺寸。短划线的间距,以尺寸值或尺寸资源表示。仅在设置了 android:dashWidth 时有效。这里的短划线就是指虚线的宽度
android:dashWidth
尺寸。每个短划线的大小,以尺寸值或尺寸资源表示。仅在设置了 android:dashGap 时有效。
组成虚线的线段之间的间隔,间隔越大,则虚线看起来间隙越大
dashWidth与 dashGap两个有任何一个为0那么虚线就不生效

XML的使用

(1)

直接给View设置背景

(2)

Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.gradient_box);

TextView tv = (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);

通过构造函数创建ShapeDrawable

一种是空的构造:
ShapeDrawable();
一种是带有参数构造:
ShapeDrawable(Shape s);

由上述可知,可以通过Shape参数动态构建一个ShapeDrawable.
那么这个Shape:


关系图

发现有几个子类可以去完成,并且这个Shape是放在android.graphics.drawable.shapes.Shape包下

具体参考另一篇文章:
Shape介绍

相关文章

网友评论

    本文标题:ShapeDrawable

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