[Android开发艺术探索阅读笔记]第6章 Drawable

281 阅读3分钟

Drawable表示的是一种可以在Canvas上进行绘制的抽象的概念,它的种类有很多,最常见的颜色和图片都可以是一个Drawable。

简介

public abstract class Drawable

在Android的设计中,Drawable是一个抽象类,它是所有Drawable对象的基类,每个具体的Drawable都是它的子类,比如ShapeDrawable、BitmapDrawable等。

使用getIntrinsicWidthgetIntrinsicHeight 获得宽和高,不是所有的 Drawable 都有宽高。

分类

常见的有 BitmapDrawable, ShapeDrawable, LayerDrawable 以及 StateListDrawable。

BitmapDrawable

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    <!--资源id-->
    android:src="@mipmap/ic_launcher"
    <!--抗锯齿-->
    android:antialias="true"
    <!--抖动-->
    android:dither="true"
    <!--过滤-->
    android:filter="true"
    <!--位置-->
    android:gravity="center"
    <!--平铺模式-->
    android:tileMode="repeat"
    >
</bitmap>
<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@mipmap/ic_launcher">
    <!--属性和bitmap一样-->
</nine-patch>

ShapeDrawable

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="2dp"
    android:innerRadiusRatio="1"
    android:shape="rectangle"
    >
  <corners
      android:bottomLeftRadius="2dp"
      android:bottomRightRadius="2dp"
      android:radius="3dp"
      android:topLeftRadius="2dp"
      android:topRightRadius="2dp"
      />

  <gradient
      android:angle="2"
      android:centerColor="@color/colorAccent"
      android:centerX="23"
      android:centerY="22"
      android:endColor="@color/colorPrimaryDark"
      android:gradientRadius="23dp"
      android:startColor="@color/colorAccent"
      android:type="linear"
      android:useLevel="true"
      />

  <padding
      android:bottom="2dp"
      android:left="2dp"
      android:right="2dp"
      android:top="2dp"
      />

  <size
      android:height="100dp"
      android:width="200dp"
      />

  <solid
      android:color="@color/colorPrimary"
      />

  <stroke
      android:width="2dp"
      android:color="@color/colorPrimary"
      android:dashWidth="2dp"
      android:dashGap="2dp"
      />
</shape>
  1. android:shape

    表示形状,有四个选项:rectangle(矩形)、oval(椭圆)、line(横线)和ring(圆环)。它的默认值是矩形,另外line和ring这两个选项必须要通过标签来指定线的宽度和颜色等。

    针对ring这个形状,有5个特殊的属性:android:innerRadius、android:thickness、android:innerRadiusRatio、android:thicknessRatio和android:useLevel。

  2. corner

    表示 shape 的四个角度,只适用于矩形的 shape。

  3. gradient

    与 标签是互相排斥的。表示渐变效果。

  4. solid

    纯色填充

  5. stroke

    Shape 的描边。

  6. padding

    表示包含 Shape 的 View 的空白

  7. size

    大小。

LayerDrawable

是一种层次化的 Drawable 集合,通过将不同的 Drawable 放置在不同的层上面达到一种叠加后的效果。


<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

  <item 
      android:id="@+id/name"
      <!--相对于View向下的偏移量-->
      android:bottom="23dp"
      android:drawable="@mipmap/ic_launcher"
      <!--相对于View向左的偏移量-->
      android:left="23dp"
      <!--相对于View向右的偏移量-->
      android:right="23dp"
      <!--相对于View向上的偏移量-->
      android:top="23dp"
  />
</layer-list>

文本框输入背景

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape android:shape="rectangle">
      <solid android:color="#0ac39e"/>
    </shape>
  </item>
  <item android:bottom="6dp">
    <shape android:shape="rectangle">
      <solid android:color="#ffffff"/>
    </shape>
  </item>
  <item
      android:bottom="1dp"
      android:left="1dp"
      android:right="1dp">
    <shape android:shape="rectangle">
      <solid android:color="@android:color/black"/>
    </shape>
  </item>
</layer-list>

StateListDrawable

LevelListDrawable

对于于 标签。它同样表示一个Drawable集合,集合中的每个Drawable都有一个等级(level)的概念。根据不同的等级,LevelListDrawable会切换为对应的Drawable。

TransitionDrawable

TransitionDrawable对应于标签,它用于实现两个Drawable之间的淡入淡出效果

InsetDrawable

InsetDrawable对应于标签,它可以将其他Drawable内嵌到自己当中,并可以在四周留出一定的间距。当一个View希望自己的背景比自己的实际区域小的时候,可以采用InsetDrawable来实现,同时我们知道,通过LayerDrawable也可以实现这种效果。

ScaleDrawable

ScaleDrawable对应于标签,它可以根据自己的等级(level)将指定的Drawable缩放到一定比例。

ClipDrawable

ClipDrawable对应于标签,它可以根据自己当前的等级(level)来裁剪另一个Drawable,裁剪方向可以通过android:clipOrientation和an-droid:gravity这两个属性来共同控制

自定义 Drawable

Drawable的使用范围很单一,一个是作为Im-ageView中的图像来显示,另外一个就是作为View的背景,大多数情况下Drawable都是以View的背景这种形式出现的。Drawable的工作原理很简单,其核心就是draw方法。