Android Drawable

1.BitmapDrawable

png(最佳)栏笆,jpg(可以接受)放到drawable目錄下,通過R.drawable引用臊泰,也可以通過xml來定義bitmapdrawable蛉加,可以給drawable增加一些屬性

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

2.NinePatchDrawable

后綴為9.png的文件。也可以通過xml定義

<nine-patch
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@[package:]drawable/drawable_resource"
    android:dither=["true" | "false"] />

3.LayerDrawable

將不同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" />
</layer-list>

4.StateListDrawable

不同狀態(tài)時(shí)展示不同drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize=["true" | "false"]
    android:dither=["true" | "false"]
    android:variablePadding=["true" | "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>

5.LevelListDrawable

根據(jù)drawable的不同level展示不同圖片

<?xml version="1.0" encoding="utf-8"?>
<level-list
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@drawable/drawable_resource"
        android:maxLevel="integer"
        android:minLevel="integer" />
</level-list>

6.TransitionDrawable

在兩個(gè)drawable之間做淡入淡出, 只能有兩個(gè)item

<?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>

7.InsetDrawable

在包裹的drawable周圍加上襯邊

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

8.ClipDrawable

對(duì)包裹的drawable進(jìn)行裁剪

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

9.ScaleDrawable

對(duì)包裹的drawable進(jìn)行縮放

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

10.ShapeDrawable

通過xml定義shape

<?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
        android:angle="integer"
        android:centerX="float"
        android:centerY="float"
        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
        android:width="integer"
        android:height="integer" />
    <solid
        android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>

11.AnimationDrawable

通過定義每幀的drawable來定義的可動(dòng)畫drawable缸逃,通過Animatable接口的start方法來進(jìn)行動(dòng)畫的播放

<animation-list android:id="@+id/selected" android:oneshot="false">
    <item android:drawable="@drawable/wheel0" android:duration="50" />
    <item android:drawable="@drawable/wheel1" android:duration="50" />
    <item android:drawable="@drawable/wheel2" android:duration="50" />
    <item android:drawable="@drawable/wheel3" android:duration="50" />
    <item android:drawable="@drawable/wheel4" android:duration="50" />
    <item android:drawable="@drawable/wheel5" android:duration="50" />
 </animation-list>

12.ColorDrawable

通過單一顏色填充的drawable

<resources>
 <drawable name="red_color">#ff0000</drawable>
 <drawable name="blue_color">#0000ff</drawable>
 <drawable name="green_color">#00ff00</drawable>
</resources>

13.RippleDrawable (Added in API 21)

在子item上疊加ripple效果

<ripple android:color="#ffff0000">//ripple的顏色
   <item android:id="@android:id/mask"
         android:drawable="@android:color/white" /> //ripple的遮罩针饥,可以限制ripple的范圍
   <item ..> //添加其他item
 </ripple>

在view的background設(shè)置android:background="?android:attr/selectableItemBackground" 或者"?android:attr/selectableItemBackgroundBorderless"也可以起到設(shè)置波紋的效果

14.VectorDrawable (Added in API 21)

通過xml來定義矢量圖形,有<vector>來定義

 <vector xmlns:android="http://schemas.android.com/apk/res/android"
     android:height="64dp"
     android:width="64dp"
     android:viewportHeight="600"
     android:viewportWidth="600" >
     <group
         android:name="rotationGroup"
         android:pivotX="300.0"
         android:pivotY="300.0"
         android:rotation="45.0" >
         <path
             android:name="v"
             android:fillColor="#000000"
             android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
     </group>
 </vector>

15.AnimatedVectorDrawable (Added in API 21)

通過Object Animator來對(duì)VectorDrawable的屬性做動(dòng)畫需频,由<animated-vector>定義

<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
     android:drawable="@drawable/vectordrawable" > <- vector drawble
     <target
         android:name="rotationGroup" <- 屬性
         android:animation="@anim/rotation" /> <- object animator的定義文件
     <target
         android:name="v"
         android:animation="@anim/path_morph" />
 </animated-vector>

16.AnimatedStateListDrawable (Added in API 21)

StateListDrawble的派生類丁眼,state之間切換時(shí)可以定義動(dòng)畫

<animated-selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- provide a different drawable for each state-->
    <item android:id="@+id/pressed" android:drawable="@drawable/drawableP"
        android:state_pressed="true"/>
    <item android:id="@+id/focused" android:drawable="@drawable/drawableF"
        android:state_focused="true"/>
    <item android:id="@id/default"
        android:drawable="@drawable/drawableD"/>

    <!-- specify a transition -->
    <transition android:fromId="@+id/default" android:toId="@+id/pressed">
        <animation-list>
            <item android:duration="15" android:drawable="@drawable/dt1"/>
            <item android:duration="15" android:drawable="@drawable/dt2"/>
            ...
        </animation-list>
    </transition>
    ...
</animated-selector>

引用

1.https://developer.android.com/guide/topics/resources/drawable-resource.html#LevelList

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市昭殉,隨后出現(xiàn)的幾起案子苞七,更是在濱河造成了極大的恐慌,老刑警劉巖挪丢,帶你破解...
    沈念sama閱讀 217,084評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蹂风,死亡現(xiàn)場離奇詭異,居然都是意外死亡乾蓬,警方通過查閱死者的電腦和手機(jī)硫眨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來巢块,“玉大人,你說我怎么就攤上這事巧号∽迳荩” “怎么了?”我有些...
    開封第一講書人閱讀 163,450評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵丹鸿,是天一觀的道長越走。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么廊敌? 我笑而不...
    開封第一講書人閱讀 58,322評(píng)論 1 293
  • 正文 為了忘掉前任铜跑,我火速辦了婚禮,結(jié)果婚禮上骡澈,老公的妹妹穿的比我還像新娘锅纺。我一直安慰自己,他們只是感情好肋殴,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,370評(píng)論 6 390
  • 文/花漫 我一把揭開白布囤锉。 她就那樣靜靜地躺著,像睡著了一般护锤。 火紅的嫁衣襯著肌膚如雪官地。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,274評(píng)論 1 300
  • 那天烙懦,我揣著相機(jī)與錄音驱入,去河邊找鬼。 笑死氯析,一個(gè)胖子當(dāng)著我的面吹牛亏较,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播魄鸦,決...
    沈念sama閱讀 40,126評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼宴杀,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了拾因?” 一聲冷哼從身側(cè)響起旺罢,我...
    開封第一講書人閱讀 38,980評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎绢记,沒想到半個(gè)月后扁达,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,414評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蠢熄,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,599評(píng)論 3 334
  • 正文 我和宋清朗相戀三年跪解,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片签孔。...
    茶點(diǎn)故事閱讀 39,773評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡叉讥,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出饥追,到底是詐尸還是另有隱情图仓,我是刑警寧澤,帶...
    沈念sama閱讀 35,470評(píng)論 5 344
  • 正文 年R本政府宣布但绕,位于F島的核電站救崔,受9級(jí)特大地震影響惶看,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜六孵,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,080評(píng)論 3 327
  • 文/蒙蒙 一纬黎、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧劫窒,春花似錦本今、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至煤禽,卻和暖如春铐达,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背檬果。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評(píng)論 1 269
  • 我被黑心中介騙來泰國打工瓮孙, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人选脊。 一個(gè)月前我還...
    沈念sama閱讀 47,865評(píng)論 2 370
  • 正文 我出身青樓杭抠,卻偏偏與公主長得像,于是被迫代替她去往敵國和親恳啥。 傳聞我的和親對(duì)象是個(gè)殘疾皇子偏灿,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,689評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容