Android Drawable基础(一)

本文详细介绍了Android中Drawable的概念,包括其在Canvas上的绘制原理、常见类型如BitmapDrawable、ShapeDrawable等,以及如何通过XML定义和代码创建。特别聚焦于BitmapDrawable的使用,解释了其属性如android:src、android:antialias等的作用,并展示了如何在布局中设置为背景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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" />

效果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值