1.Drawable简介
可以在 Canvas 上进行绘制的抽象概念 。它的种类有很多,最常见的颜色和图片都可以是一个Drawable 。
1.一种可以在Canvas上进行绘制的抽象的概念
2.颜色、图片等都可以是一个Drawable
3.Drawable可以通过XML定义,或者通过代码创建
4.Android中Drawable是一个抽象类,每个具体的Drawable都是其子类
2.Drawable的分类
Drawable是一个抽象类,子类繁多,常见的有BitmapDrawable,ShapeDrawable,LayerDrawable,StateListDrawable等,Drawable的继承关系如下(Android Studio中查看继承关系Ctrl+H):
3.BitmapDrawable
对应标签为< bitmap >
Bitmap代表位图图像,BitmapDrawable就是一张图片,相比引用原始图像,它可以设置更多的显示效果,xml中定义:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@[package:]drawable/drawable_resource"
android:antialias=["true" | "false"]
android:dither=["true" | "false"]
android:filter=["true" | "false"]
android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
"fill_vertical" | "center_horizontal" | "fill_horizontal" |
"center" | "fill" | "clip_vertical" | "clip_horizontal"]
android:mipMap=["true" | "false"]
android:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] />
各属性的含义:
属性 | 作用 |
---|---|
android:src | 图片资源ID |
android:antialias | 布尔值。启用或停用抗锯齿(图片平滑) |
android:dither | 布尔值。当位图的像素配置与屏幕不同时(例如:ARGB 8888 位图和 RGB 565 屏幕),启用或停用位图抖动(避免失真) |
android:filter | 布尔值。启用或停用位图过滤。当位图收缩或拉伸以使其外观平滑时使用过滤 |
android:gravity | 指示当位图小于容器时,可绘制对象在其容器中放置的位置 |
android:mipMap | 布尔值。启用或停用 mipmap 提示。默认值为 false |
android:tileMode | 定义填充模式 |
其中gravity属性参考属性值如下:
值 | 说明 |
---|---|
top/bottom/left/right/center | 将对象放在其容器顶/底/左/右部/中心,不改变其大小 |
center_vertical/center_horizontal | 将对象放在其容器的垂直中心/水平中心,不改变其大小 |
fill_vertical/fill_horizontal | 按需要扩展对象的水平/垂直大小,使其完全适应其容器 |
fill | 按需要扩展对象的垂直大小,使其完全适应其容器。这是默认值 |
clip_vertical | 可设置为让子元素的上边缘和/或下边缘裁剪至其容器边界的附加选项。裁剪基于垂直重力:顶部重力裁剪上边缘,底部重力裁剪下边缘,任一重力不会同时裁剪两边 |
clip_horizontal | 可设置为让子元素的左边和/或右边裁剪至其容器边界的附加选项。裁剪基于水平重力:左边重力裁剪右边缘,右边重力裁剪左边缘,任一重力不会同时裁剪两边 |
其中titleMode参考属性值如下:
值 | 说明 |
---|---|
disabled | 不平铺位图。这是默认值 |
clamp | 当着色器绘制范围超出其原边界时复制边缘颜色 |
repeat | 水平和垂直重复着色器的图像 |
mirror | 水平和垂直重复着色器的图像,交替镜像图像以使相邻图像始终相接 |
4.基本使用
- 新建xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/test"
android:antialias="true"
android:dither="true"
android:tileMode="clamp" />
- 在布局中将其设为background
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bitmap_bg">
</RelativeLayout>
5.效果
改变android:tileMode属性 观察效果:
clamp:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/test"
android:antialias="true"
android:dither="true"
android:tileMode="clamp" />
效果:
repeat:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/test"
android:antialias="true"
android:dither="true"
android:tileMode="repeat" />
效果:
mirror:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/test"
android:antialias="true"
android:dither="true"
android:tileMode="mirror" />
效果: