Drawable子类之—— BitmapDrawable (可控制对齐平铺的图像)

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

1、BitmapDrawable

Bitmap,代表一个位图图像,Android支持三种格式的位图图像:.png (preferred),.jpg (acceptable), .gif (discouraged)。
括号里的说明,代表这三种格式的图片在Android中的支持情况,.png格式图片优先,.jpg格式也可以,但是效果没有.png好,.gif支持最差。

在构建应用的时候,Bitmap文件可能会被appt工具压缩自动优化为无损图像。例如,一个真彩色PNG,不需要超过256的颜色可以被转换成一个8位PNG和调色板。这将导致一个图像质量相同,但这需要更少的内存。所以要意识到,在drawable目录中图像的二进制文件在构建程序时可以改变。如果你打算读一个图像作为字节流并将它转换成一个位图,把你的图片放在在res /raw/文件夹里,在那里他们不会被优化。

可以直接使用图片的名称作为资源ID,来直接引用一个位图图片。也可以再XML文件中创建一个资源别名的ID。

明明图片拉进去对应的文件之后我们就直接设置为背景,那么谷歌还要弄一个BitmapDrawable干嘛,简单说就是你直接设背景能控制背景怎么对齐吗,能控制背景如何平铺吗,不能。

所以最看得见的好处就是:

  • BitmapDrawable可以设定背景的对齐方式
  • BitmapDrawable可以设定背景的对齐方式。

有图有真相:

对其和平铺.gif

1.1、最简单的BitmapDrawable的使用方法:

在程序的drawable文件面新建一个文件:
内容如下:

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

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@mipmap/demo_pic"
    android:tileMode="repeat"
    >
</bitmap>

然后在activity的布局文件随便写一个TextView,宽高比我们的src对应的图片大一些,然后把刚刚的 根元素为bitmap 的xml文件设为该TextView的背景

Paste_Image.png

然后运行程序,即可看到效果。

Paste_Image.png

1.2 BitmapDrawable 用法简述

BitmapDrawable的xml需要什么?

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

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@mipmap/demo_pic"
    android:tileMode="repeat"
    >
</bitmap>

  • 开头
    <?xml version="1.0" encoding="utf-8"?>是xml必须的,不必说
  • 根元素
    BitmapDrawable对应的根元素就是 bitmap,也没什么可说
  • 节点
    src 节点是必不可少的。

也就是说:在一个根元素是bitmap带src节点的xml文件。
其他的节点就是修饰,比如我们还用到的 tileMode

BitmapDrawable可设置的节点

<?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:mipmap=["true" | "false"]
    android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
                      "fill_vertical" | "center_horizontal" | "fill_horizontal" |
                      "center" | "fill" | "clip_vertical" | "clip_horizontal"]
    android:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] >

</bitmap>
  • xmlns:android
    类型:String。
    定义了命名空间,必须是"http://schemas.android.com/apk/res/android"。
    如果<bitmap>是根元素,那么他是必须的,
    如果是嵌套在<itme>里面,那么就不是必须的。比如我们上面就不用。

  • android:src 资源id
    没什么可说,图片资源id

  • android:antialias 抗锯齿
    是否开启抗锯齿,图片会更加平滑,会降低清楚度,但是基本忽略,所以,开

  • android:dither 抖动效果
    是否开启抖动,开。
    抖动的作用:让高质量的图片的比较低质量的屏幕上不失真,得到比较好的显示效果。
    比如图片的色彩模式是 ARGB8888,但是手机设备的支持RGB555的色彩模式,那么开启这么就可以有效减少失真现象。
    (Android中我们创建的Bitmap一般会选择ARGB888模式,ARGB每个通道各占8位,8位1个字节,一个像素4个字节,一个像素的位数总和越高,图片越逼真)

  • android:filter 过滤效果
    开。在图片图片被拉伸或者压缩的时候开启过滤效果可以显示更加好的效果。

  • android:gravity 图片的对齐效果

  • android:mipmap 纹理映射,as里面就用了这个了。先默认为false(待考究)

gravity 选项的可选项

可选项 含义
top 保持原有大小,图片至于容器的顶部
bottom 保持原有大小,图片至于容器的底部
left 保持原有大小,图片至于容器的左部
right 保持原有大小,图片至于容器的右部
center_vertical 保持原有大小,图片垂直居中
fill_vertical 图片垂直方向填充容器
center_horizontal 保持原有大小,图片水平居中
fill_horizontal 图片水平方向填充容器
center 保持原有大小,图片同时水平和垂直居中
fill 默认值,同时水平和垂直拉伸
clip_vertical 附加项,表示水平方向的裁剪
clip_horizontal 附加项,表示水平方向的裁剪

需要说明的是,这些对其方式可以利用 | 符号同时使用,比如
android:gravity="top|left"

  • android:tileMode 平铺模式

**当开启 tileMode 之后 ,gravity 属性会被忽略 **

tileMode 选项的可选项

可选项 含义
disabled �关闭平铺模式
clamp 大小不变,像素在四周扩散
repeat 常见的水平和垂直方向的平铺
mirror 水平和垂直方向的镜面投影

全部节点介绍完了。

总结,节点很多,最需要的关心的(最能看到区别的)就是gravity和tileMode。

1.3 demo示例

这里贴上代码,分别看一下的2个gravity的对其效果和4中tileMode的效果:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="原样"/>



            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="原样"
                android:background="@mipmap/demo_pic"
                android:layout_marginBottom="10dp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="直接src调用,直接调用图片"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="直接src调用,直接调用图片"
                android:background="@mipmap/demo_pic"
                android:layout_marginBottom="10dp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="调用drawable下面的bitmap文件"/>
            <!--默认是作为背景是整体拉伸的-->
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="调用drawable下面的bitmap文件"
                android:background="@drawable/bitmapdrawable_simple"
                android:layout_marginBottom="10dp"

                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="下面举两个例子说明 gravity节点"
                android:textSize="30sp"
                />

            <TextView
                android:layout_width="100dp"
                android:layout_height="100dp"

                android:text="下面举两个例子说明 gravity节点"
                android:textSize="30sp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="调用bitmap文件 gravity top"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="100dp"
                android:text="调用bitmap文件 gravity top"
                android:background="@drawable/bitmapdraw_gravity_top"
                android:layout_marginBottom="10dp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="调用bitmap文件 gravity left"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="100dp"
                android:text="调用bitmap文件 gravity left"
                android:background="@drawable/bitmapdraw_gravity_left"
                android:layout_marginBottom="10dp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="下面说的都是tileMode"
                android:layout_marginBottom="10dp"
                android:textSize="30sp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="调用bitmap文件 tileMode disabled"/>
            <TextView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="调用bitmap文件 tileMode disabled"
                android:background="@drawable/bitmapdraw_tilemode_disabled"
                android:layout_marginBottom="10dp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="调用bitmap文件 tileMode clamp"/>
            <TextView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="调用bitmap文件 tileMode clamp"
                android:background="@drawable/bitmapdraw_tilemode_clamp"
                android:layout_marginBottom="10dp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="调用bitmap文件 tileMode report"/>
            <TextView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="调用bitmap文件 tileMode report"
                android:background="@drawable/bitmapdraw_tilemode_report"
                android:layout_marginBottom="10dp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="调用bitmap文件 tileMode mirror"/>
            <TextView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:text="调用bitmap文件 tileMode mirror"
                android:background="@drawable/bitmapdraw_tilemode_mirror"
                android:layout_marginBottom="10dp"

                />

        </LinearLayout>

    </ScrollView>

</LinearLayout>

附上xml文件

.
.
bitmapdrawable_simple.xml

<?xml version="1.0" encoding="utf-8"?>
<!--
这是 bitmapdrawable 最简单的用法

根元素 名称必须是 bitmap
src 节点必不可少

也就是说最简答就是: 根元素为bitmap下戴一个有效的src节点

-->
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@mipmap/demo_pic">
</bitmap>

.
.

bitmapdraw_gravity_left.xml

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

<!--android:gravity="left"-->
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="left"
    android:src="@mipmap/demo_pic"></bitmap>

.
.

bitmapdraw_gravity_top.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@mipmap/demo_pic"
    android:gravity="top"
    >
</bitmap>

.
.
bitmapdraw_tilemode_clamp.xml

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

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@mipmap/demo_pic"
    android:tileMode="clamp"
    >
</bitmap>

.
.
bitmapdraw_tilemode_disabled.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@mipmap/demo_pic"
    android:tileMode="disabled"
    >
</bitmap>

.
.
bitmapdraw_tilemode_mirror.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@mipmap/demo_pic"
    android:tileMode="mirror"
    >
</bitmap>

.
.
bitmapdraw_tilemode_report.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@mipmap/demo_pic"
    android:tileMode="repeat"
    >
</bitmap>
对其和平铺.gif
Paste_Image.png
Paste_Image.png

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


相关参考:
《android开发艺术探索》

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

推荐阅读更多精彩内容