本文出自 “阿敏其人” 簡書博客,轉(zhuǎn)載或引用請注明出處。
1、BitmapDrawable
Bitmap,代表一個位圖圖像,Android支持三種格式的位圖圖像:.png (preferred),.jpg (acceptable), .gif (discouraged)。
括號里的說明,代表這三種格式的圖片在Android中的支持情況,.png格式圖片優(yōu)先,.jpg格式也可以,但是效果沒有.png好,.gif支持最差。
在構(gòu)建應(yīng)用的時候,Bitmap文件可能會被appt工具壓縮自動優(yōu)化為無損圖像。例如,一個真彩色PNG,不需要超過256的顏色可以被轉(zhuǎn)換成一個8位PNG和調(diào)色板。這將導(dǎo)致一個圖像質(zhì)量相同,但這需要更少的內(nèi)存。所以要意識到,在drawable目錄中圖像的二進(jìn)制文件在構(gòu)建程序時可以改變。如果你打算讀一個圖像作為字節(jié)流并將它轉(zhuǎn)換成一個位圖,把你的圖片放在在res /raw/文件夾里,在那里他們不會被優(yōu)化。
可以直接使用圖片的名稱作為資源ID,來直接引用一個位圖圖片。也可以再XML文件中創(chuàng)建一個資源別名的ID。
明明圖片拉進(jìn)去對應(yīng)的文件之后我們就直接設(shè)置為背景,那么谷歌還要弄一個BitmapDrawable干嘛,簡單說就是你直接設(shè)背景能控制背景怎么對齊嗎,能控制背景如何平鋪嗎,不能。
所以最看得見的好處就是:
- BitmapDrawable可以設(shè)定背景的對齊方式
- BitmapDrawable可以設(shè)定背景的對齊方式。
有圖有真相:

1.1、最簡單的BitmapDrawable的使用方法:
在程序的drawable文件面新建一個文件:
內(nèi)容如下:
<?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對應(yīng)的圖片大一些,然后把剛剛的 根元素為bitmap 的xml文件設(shè)為該TextView的背景

然后運(yùn)行程序,即可看到效果。

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對應(yīng)的根元素就是 bitmap,也沒什么可說 - 節(jié)點
src 節(jié)點是必不可少的。
也就是說:在一個根元素是bitmap帶src節(jié)點的xml文件。
其他的節(jié)點就是修飾,比如我們還用到的 tileMode
BitmapDrawable可設(shè)置的節(jié)點
<?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
沒什么可說,圖片資源idandroid:antialias 抗鋸齒
是否開啟抗鋸齒,圖片會更加平滑,會降低清楚度,但是基本忽略,所以,開android:dither 抖動效果
是否開啟抖動,開。
抖動的作用:讓高質(zhì)量的圖片的比較低質(zhì)量的屏幕上不失真,得到比較好的顯示效果。
比如圖片的色彩模式是 ARGB8888,但是手機(jī)設(shè)備的支持RGB555的色彩模式,那么開啟這么就可以有效減少失真現(xiàn)象。
(Android中我們創(chuàng)建的Bitmap一般會選擇ARGB888模式,ARGB每個通道各占8位,8位1個字節(jié),一個像素4個字節(jié),一個像素的位數(shù)總和越高,圖片越逼真)
android:filter 過濾效果
開。在圖片圖片被拉伸或者壓縮的時候開啟過濾效果可以顯示更加好的效果。android:gravity 圖片的對齊效果
android:mipmap 紋理映射,as里面就用了這個了。先默認(rèn)為false(待考究)
gravity 選項的可選項
| 可選項 | 含義 |
|---|---|
| top | 保持原有大小,圖片至于容器的頂部 |
| bottom | 保持原有大小,圖片至于容器的底部 |
| left | 保持原有大小,圖片至于容器的左部 |
| right | 保持原有大小,圖片至于容器的右部 |
| center_vertical | 保持原有大小,圖片垂直居中 |
| fill_vertical | 圖片垂直方向填充容器 |
| center_horizontal | 保持原有大小,圖片水平居中 |
| fill_horizontal | 圖片水平方向填充容器 |
| center | 保持原有大小,圖片同時水平和垂直居中 |
| fill | 默認(rèn)值,同時水平和垂直拉伸 |
| clip_vertical | 附加項,表示水平方向的裁剪 |
| clip_horizontal | 附加項,表示水平方向的裁剪 |
需要說明的是,這些對其方式可以利用 | 符號同時使用,比如
android:gravity="top|left"
- android:tileMode 平鋪模式
**當(dāng)開啟 tileMode 之后 ,gravity 屬性會被忽略 **
tileMode 選項的可選項
| 可選項 | 含義 |
|---|---|
| disabled | ?關(guān)閉平鋪模式 |
| clamp | 大小不變,像素在四周擴(kuò)散 |
| repeat | 常見的水平和垂直方向的平鋪 |
| mirror | 水平和垂直方向的鏡面投影 |
全部節(jié)點介紹完了。
總結(jié),節(jié)點很多,最需要的關(guān)心的(最能看到區(qū)別的)就是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調(diào)用,直接調(diào)用圖片"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="直接src調(diào)用,直接調(diào)用圖片"
android:background="@mipmap/demo_pic"
android:layout_marginBottom="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="調(diào)用drawable下面的bitmap文件"/>
<!--默認(rèn)是作為背景是整體拉伸的-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="調(diào)用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節(jié)點"
android:textSize="30sp"
/>
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:text="下面舉兩個例子說明 gravity節(jié)點"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="調(diào)用bitmap文件 gravity top"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:text="調(diào)用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="調(diào)用bitmap文件 gravity left"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:text="調(diào)用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="調(diào)用bitmap文件 tileMode disabled"/>
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:text="調(diào)用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="調(diào)用bitmap文件 tileMode clamp"/>
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:text="調(diào)用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="調(diào)用bitmap文件 tileMode report"/>
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:text="調(diào)用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="調(diào)用bitmap文件 tileMode mirror"/>
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:text="調(diào)用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 節(jié)點必不可少
也就是說最簡答就是: 根元素為bitmap下戴一個有效的src節(jié)點
-->
<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>



了解更多的Drawable分類 Drawable圖像資源抽象類
本篇完。
相關(guān)參考:
《android開發(fā)藝術(shù)探索》