【Android Drawable系列】- Other單個Drawable

Android Drawable系列
單個Drawable的使用,與shape的使用率相比其他的資原始檔使用頻率不太高,所以就合併到一塊了。其中包括: Bitmap
, Inset Drawable
, Clip Drawable
, Scale Drawable
。
Bitmap
Bitmap也就是點陣圖,如果要講這個,我一定是說不清楚的。這裡要講的是資原始檔中的Bitmap檔案,例如: .png
、 .jpg
或者 .gif
圖都屬於Bitmap檔案。圖片檔案不多了,用法都很熟悉,要講的是xml中的bitmap標籤。

這是 API 28
能聯想出來的所有屬性,其中 tileModeX
和 tileModeY
需要 drawable-v21
目錄中使用, autoMirrored
是 drawable-v19
, mipMap
是 drawable-v18
。
概覽
<?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"]//當點陣圖的畫素配置與螢幕不同時(例如:ARGB 8888 點陣圖和 RGB 565 螢幕),啟用或停用點陣圖抖動。 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"]//啟用或停用 mipmap 提示,v18屬性 android:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] //定義平鋪模式 />
android:src
為必須屬性
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 | 水平和垂直重複著色器的影象,交替映象影象以使相鄰影象始終相接。 |
示例程式碼:
<?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:tileMode="mirror" android:src="@drawable/icon"/>
這是 mirror
屬性的效果

這是 repeat
屬性的效果

這是 clamp
屬性的效果,像是把邊緣的顏色拉伸的感覺

Nine-Patch
Nine-Patch
也就是俗稱的 .9
圖, .9
圖是可以拉伸區域的 .9.png
檔案,允許根據內容調整影象大小。通常使用 .9
圖的 View
會將寬高屬性設定為 warp_content
,以便 VIew
適應內容擴充套件大小。
與Bitmap使用方式一樣,可以直接使用也可以在xml定義資源引用。直接看看使用
<?xml version="1.0" encoding="utf-8"?> <nine-patch xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/icon9"/>
與bitmap相同 android:src
為必須屬性。
Inset Drawable
直譯的意思就是插入Drawable,可以在其他指定的Drawable周圍插入距離的一種Drawable。這麼說可能不太好理解,看圖

圖中的兩個方形這是的都是 TextView
的背景,但是第一個方形的左邊和上邊還留有空白,這就是使用 inset
標籤做到的,可以在背景上增加邊距,xml程式碼如下:
<?xml version="1.0" encoding="utf-8"?> <inset xmlns:android="http://schemas.android.com/apk/res/android" android:insetLeft="10dp" android:insetTop="20dp" android:drawable="@drawable/ic_launcher_background"> </inset>
inset
所支援的屬性如下:
drawable-v21
其實 inset
標籤還支援其他的Drawable資源使用,可以直接在 <inset></inset>
之間通過建立所支援的標籤的方式使用Drawable資源。
Clip drawable
在xml檔案中定義一個可以對其他drawable物件進行裁剪的drawable物件。直接看xml屬性:
<?xml version="1.0" encoding="utf-8"?> <clip xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/drawable_resource" android:clipOrientation=["horizontal" | "vertical"] android:gravity=["top" | "bottom" | "left"| "right" | "center_vertical" |"fill_vertical" | "center_horizontal" | "fill_horizontal" |"center" | "fill" | "clip_vertical" |"clip_horizontal"] />
android:clipOrientation
控制裁剪的方向
值 | 說明 |
---|---|
horizontal | 水平裁剪可繪製物件。 |
vertical | 垂直裁剪可繪製物件。 |
android:gravity
的屬性值太多了,並沒有逐個嘗試。下面是嘗試寫的一個例子:
<?xml version="1.0" encoding="utf-8"?> <clip xmlns:android="http://schemas.android.com/apk/res/android" android:clipOrientation="horizontal" android:gravity="center" android:drawable="@drawable/ic_launcher_background"/>
可以實際並沒有任何效果,還需要在程式碼中使用 ClipDrawable
的 setLevel
方法做相應處理
注:預設級別為 0,即完全裁剪,使影象不可見。當級別為 10000 時,影象不會裁剪,而是完全可見。
ImageView imageView = findViewById(R.id.inset); ClipDrawable clipDrawable = (ClipDrawable) imageView.getDrawable(); clipDrawable.setLevel(1000);
具體效果過如下:

Scale drawable
可以改變其他Drawable物件尺寸的一種Drawable物件。xml屬性:
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/drawable_resource" android:scaleGravity=["top" | "bottom" | "left" | "right" | "center_vertical" | "fill_vertical" | "center_horizontal" | "fill_horizontal" | "center" | "fill" | "clip_vertical" | "clip_horizontal"] android:scaleHeight="percentage"//高度縮放的百分比,可是必須是xx% android:scaleWidth="percentage"//寬度度縮放的百分比,可是必須是xx% />
這標籤,差點沒弄出來,用法和Clip drawable有點類似,先使用xml在通過對應的 ScaleDrawable
的 setLevel
方法來控制顯示。xml程式碼如下:
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/icon" android:scaleGravity="center_vertical|center_horizontal" android:scaleHeight="80%" android:scaleWidth="80%"/>
java程式碼
ImageView imageView = findViewById(R.id.inset); ScaleDrawable drawable = (ScaleDrawable) imageView.getDrawable(); drawable.setLevel(1);
具體效果過如下:

總結
到此,單個的Drawable資源標籤就這些了。經常使用的標籤會比較詳細的嘗試和記錄,不常用的標籤很多屬性的細節沒有一個一個去試想過了,以後有用到的的時候在進行研究。下篇文章就開始多個Drawable的標籤用法了(要死的趕腳T_T!)。