Drawable種類很多,具體可以參考文檔連接,常見的有以下的幾種:
name | 功能 |
---|---|
BitmapDrawable(位圖文件) | 位圖圖形文件(.png、.jpg 或 .gif) |
NinePatchDrawable | 具有可拉伸區域的 PNG 文件,允許根據內容調整圖像大小 (.9.png) |
LayerDrawable(圖層列表) | 管理其他可繪製對象陣列的可繪製對象。它們按陣列順序繪製,因此索引最大的元素繪製在頂部。 |
StateListDrawable(狀態列表) | 此 XML 文件爲不同狀態引用不同位圖圖形(例如,按下按鈕時使用不同的圖像)。 |
LevelListDrawable(級別列表) | 此 XML 文件用於定義管理大量備選可繪製對象的可繪製對象,每個可繪製對象都分配有最大的備選數量 |
TransitionDrawable(轉換可繪製對象) | 此 XML 文件用於定義可在兩種可繪製對象資源之間交錯淡出的可繪製對象 |
ClipDrawable(裁剪可繪製對象) | 此 XML 文件用於定義對其他可繪製對象進行裁剪(根據其當前級別值)的可繪製對象 |
ScaleDrawable(縮放可繪製對象) | 此 XML 文件用於定義更改其他可繪製對象大小(根據其當前級別值)的可繪製對象 |
ShapeDrawable(形狀繪製對象) | 此 XML 文件用於定義幾何形狀(包括顏色和漸變) |
1.BitmapDrawable(位圖文件)
BitmapDrawable是最簡單的一種Drawable,表示一張圖片,我們可以直接引用圖片,也可以在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:tileMode=["disabled" | "clamp" | "repeat" | "mirror"] />
屬性:
name | 作用 |
---|---|
android:antialias | Boolean:是否開啓抗鋸齒 |
android:dither | Boolean:如果位圖與屏幕的像素配置不同時,是否允許抖動.(例如:一個位圖的像素設置是 ARGB 8888,但屏幕的設置是RGB 565) |
android:filter | Boolean:是否允許對位圖進行濾波。對位圖進行收縮或者延展使用濾波可以獲得平滑的外觀效果。 |
android:gravity | 位圖定位 |
android:tileMode | Keyword:定義了tile模式。當tile模式被啓用,位圖是重複的,並且gravity屬性將被忽略。 |
tileMode 屬性就是用於定義背景的顯示模式:
- disabled
默認值,表示不使用平鋪 - clamp
複製邊緣色彩 - repeat
X、Y 軸進行重複圖片顯示,也就是我們說要說的平鋪 - mirror
在水平和垂直方向上使用交替鏡像的方式重複圖片的繪製
2.NinePatchDrawable(可拉伸區域.9png)
一個NinePatch也是一個PNG的圖片,不過不同的是可以爲這種格式的圖片定義可伸縮的區域,當某個視圖的內容超過了正常的尺寸的時候,這種格式的圖片會自動拉伸以適應不同的尺寸。一般這種圖片是作爲視圖的背景,並且視圖至少有一個尺寸(layout_width或者layout_height)是設置爲”warp_content”。當視圖自增長來適應內容的時候,Nine-Patch格式的圖片也會相應的進行縮放來匹配視圖的尺寸。
NinePatchDrawable的創建方式幾乎和BitmapDrawable一模一樣,使用方式也沒有多大的區別。只是在xml創建的時候,沒有那麼多的屬性。
<nine-patch
xmlns:android="http://schemas.android.com/apk/res/android"
android:src=""
android:dither=""/>
製作.9圖片過程自行google~(左、上黑線表示可拉伸區域,右、下表示內容顯示區域可選)
3.LayerDrawable
一個LayerDrawable是一個可以管理一組drawable對象的drawable。在LayerDrawable的drawable資源按照列表的順序繪製,列表的最後一個drawable繪製在最上層。它所包含的一組drawable資源用多個元素表示,一個元素代表一個drawable資源。
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@[package:]drawable/drawable_resource"
android:id="@[+][package:]id/resource_name"
android:top="dimension"
android:right="dimension"
android:bottom="dimension"
android:left="dimension" />
<item ... />
<item ... />
...
</layer-list>
4.StateListDrawable(狀態列表)
這個是用的最多:比如按鈕點擊有多種狀態,獲取焦點,失去焦點,點擊等等;
一個StateListDrawable就是一個在xml文件中定義,根據該對象不同的狀態,用幾張不同的圖片來代表相同的圖形。使用StateListDrawable可以根據不同的狀態提供不同的背景。
在XML文件中描述這些狀態列表。在唯一的一個標籤下,使用標籤來代表一個圖形。每個標籤使用各種屬性來描述它所代表的狀態所需要的drawable資源。
再次狀態發生改變的時候,都會從上到下遍歷這個狀態列表,第一個和它匹配的將會被使用——-而不是去選擇最適合的匹配。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize=["true" | "false"] //大小是否不跟隨狀態改變,默認false 跟隨改變
android:dither=["true" | "false"] //是否允許抖動.默認true
android:variablePadding=["true" | "false"] > //padding是否跟隨狀態改變 默認false
<item
android:drawable="@[package:]drawable/drawable_resource"
android:state_pressed=["true" | "false"]
android:state_focused=["true" | "false"]
android:state_hovered=["true" | "false"]
android:state_selected=["true" | "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true" | "false"]
android:state_enabled=["true" | "false"]
android:state_activated=["true" | "false"]
android:state_window_focused=["true" | "false"] />
</selector>
舉例:
<?xml version="1.0" encoding="UTF-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/botton_add" />
<item android:state_pressed="true" android:drawable="@drawable/botton_add_down" />
<item android:state_selected="true" android:drawable="@drawable/botton_add" />
<item android:drawable="@drawable/botton_add" />//默認
</selector>
//添加drawable
<Button
android:background="@drawable/statelist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
5.LevelListDrawable(級別列表)
一個LeveListDrawable管理着一組交替的drawable資源。LeveListDrawable裏面的每一個drawable資源與一個最大數值結合起來,作爲LevelListDrawable資源的一項。
調用Drawable的setLevel()方法可以加載level-list或代碼中定義的某個drawable資源,判斷加載某項的方式:level-list中某項的Android:maxLevel數值大於或者等於setLevel設置的數值,就會被加載。如下:參考這篇
<level-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/drawable_resource"
android:maxLevel="integer"//最大leave
android:minLevel="integer" />//最小leave
</level-list>
6.TransitionDrawable(轉換可繪製對象)
一個TransitionDrawable是一個特殊的Drawable對象,可以實現兩個drawable資源之間淡入淡出的效果。
節點下的每個代表一個drawable資源。只能有兩個。先前轉換調用startTransition()。向後,調用 reverseTransition()。
語法:
<?xml version="1.0" encoding="utf-8"?>
<transition
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@[package:]drawable/drawable_resource"
android:id="@[+][package:]id/resource_name"
android:top="dimension"
android:right="dimension"
android:bottom="dimension"
android:left="dimension" />
</transition>
1、xml方式使用
transition.xml:
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/image01"/>
<item android:drawable="@drawable/image02"/>
</transition>
在layout中使用:
<ImageView
android:id="@+id/imgView"
android:src="@drawable/transition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
在代碼中控制:
ImageView imageView = (ImageView) findViewById(R.id.imgView);
TransitionDrawable transitionDrawable = (TransitionDrawable) imageView.getDrawable();
transitionDrawable.startTransition(3000);
7.ClipDrawable(裁剪可繪製對象)
ClipDrawable 是對一個Drawable進行剪切操作,可以控制這個drawable的剪切區域,以及相對於容器的對齊方式,Android中的進度條就是使用一個ClipDrawable實現效果的,它根據level的屬性值,決定剪切區域的大小。
<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"] />
默認的level是0,表示全部裁剪掉了,圖片不可見,但是顯示的時候,依然會佔據位置。當level是10,000,圖片相當於沒裁剪,完全可見。
當在代碼設置setleave(8000)的時候就相當於裁剪2000,裁剪20%的區域。
8.ScaleDrawable(縮放可繪製對象)
對另一個drawable資源,基於當前的level,進行尺寸變換的drawable。
<?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"
android:scaleWidth="percentage" />
執行兩步:
1.編寫scale xml文件
2.設置leave大於0
當然還有RotateDrawable:使用rotate標籤
9.ShapeDrawable(形狀繪製對象)
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle" | "oval" | "line" | "ring"]
>//
<corners//圓角
android:radius="integer"
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer" />
<gradient//shape的漸變顏色。
android:angle="integer"
android:centerX="integer"
android:centerY="integer"
android:centerColor="integer"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type=["linear" | "radial" | "sweep"]
android:useLevel=["true" | "false"] />
<padding// 內容與視圖邊界的距離
android:left="integer"
android:top="integer"
android:right="integer"
android:bottom="integer" />
<size//shape大小
android:width="integer"
android:height="integer" />
<solid//填充這個shape的純色
android:color="color" />
<stroke//這個shape使用的筆畫,當android:shape="line"的時候,必須設置改元素。
android:width="integer"
android:color="color"
android:dashWidth="integer"
android:dashGap="integer" />
</shape>
android:shape=”“
值 | 描述 |
---|---|
“rectangle” | 矩陣,這也是默認的shape |
“oval” | 橢圓 |
“line” | 一條水平的直線。這種shape必須使用 元素來定義這條線的寬度 |
“ring” | 圓環 |
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FFFF0000"
android:endColor="#80FF00FF"
android:angle="45"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners android:radius="8dp" />
</shape>
http://blog.csdn.net/lonelyroamer/article/details/8254592
10.InsetDrawable
InsetDrawable 表示一個drawable嵌入到另外一個drawable內部,並且在內部留一些間距,這一點很像drawable的padding屬性,區別在於 padding表示drawable的內容與drawable本身的邊距,insetDrawable表示兩個drawable和容器之間的邊距。當控件需要的背景比實際的邊框小的時候比較適合使用InsetDrawable。
<?xml version="1.0" encoding="utf-8"?>
<inset
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/drawable_resource"
android:insetTop="dimension"
android:insetRight="dimension"
android:insetBottom="dimension"
android:insetLeft="dimension" />
當然還有其他的類型Drawable,詳細參考文檔~。睡覺。。
參考:
https://developer.android.google.cn/reference/android/graphics/drawable/package-summary.html