Android Drawable 分類筆記

主要內(nèi)容出自 《Android 開發(fā)藝術(shù)探索》,僅作記錄。

1.1 BitmapDrawable

表示的就是一張圖片

  • android:src 圖片資源的 id
  • android:antialias 抗鋸齒,讓圖片變得平滑,一般開啟
  • android:dither 是否開啟抖動(dòng)效果,圖片像素和屏幕像素配置不一致,開啟可讓高質(zhì)量的圖片在低質(zhì)量的屏幕還能保持較好的顯示效果
  • android:filter 過濾效果,圖片被拉伸或壓縮,開啟能保持較好的顯示效果
  • android:gravity 圖片小于容器,用此對(duì)圖片進(jìn)行定位,可 "|" 組合使用
  • android:mipMap 紋理映射,默認(rèn) false
  • android:tileMode 平鋪模式 ["disabled" | "clamp" | "repeat" | "mirror"],開啟后 gravity 會(huì)失效。clamp:暫不知 repeat:鋪滿 mirror:左右上下各一面鏡子鋪滿

NinePatchDrawable:自動(dòng)根據(jù)寬/高進(jìn)行縮放不失真,可直接使用,可用用 XML 描述。<nine-patch >

1.2 ShapeDrawable

通過顏色來構(gòu)造圖形,既可以是純色,也可以是具有漸變效果的圖形。通過<shape>創(chuàng)建的 Drawable,實(shí)體類實(shí)際上是 GradientDrawable。

  • android:shape 表示形狀:rectangle(矩形)、oval(橢圓)、line(橫線)、和 ring(圓環(huán))。默認(rèn)是矩形,line 和 ring 需要通過 <stroke> 來指定線的寬度和顏色。
    對(duì)于 ring 這個(gè)形狀,有 5 個(gè)特殊屬性。
Value Are
android:innerRadius 圓環(huán)的內(nèi)半徑,和 android:innerRadiusRation 同時(shí)存在,以前者為準(zhǔn)
android:thickness 圓環(huán)的厚度,半徑減去內(nèi)半徑的大小
android:innerRadiusRation 內(nèi)半徑占整個(gè) Drawable 寬度的比例,默認(rèn)為 9。如果為 n 則內(nèi)半徑 = 寬度/n
android:thicknessRatio 厚度占整個(gè) Drawable 寬度的比例,默認(rèn)為 3。如果為 n 則厚度 = 寬度/n
android:useLevel 一般都應(yīng)是 false,除非被當(dāng)做 LevelListDrawable(堆疊圖片)
  • <corners> 表示 shape 四個(gè)角的角度。只適用于矩形,用 px 表示。
  1. android:radius 四個(gè)角同時(shí)設(shè)定角度,優(yōu)先級(jí)較低會(huì)被其他四個(gè)屬性覆蓋。
  2. android:topLeftRadius 左上角角度
  3. android:topRightRadius 右上角角度
  4. android:bottomLeftRaius 左下角角度
  5. android:bottomRightRadius 右下角角度
  • <gradient> 與 <solid> 互相排斥,solid 表示純色填充,gradient 表示漸變。它有以下幾個(gè)屬性:
  1. android:angle 漸變的角度默認(rèn)為0,數(shù)值為 45 的倍數(shù),0表示從左到右,90 表示從下到上。
  2. android:centerX 漸變的中心點(diǎn)橫軸坐標(biāo)
  3. android:centerY 漸變的中心點(diǎn)縱坐標(biāo)
  4. android:startColor 漸變的起始色
  5. android:centerColor 漸變的中間色
  6. android:endColor 漸變的結(jié)束色
  7. android:gradientRadius 漸變半徑,僅當(dāng) android:type = "radial" 時(shí)有效
  8. android:useLevel 一般為 false,當(dāng) Drawable 作為 StateListDrawable 時(shí)使用時(shí)為 true
  9. android:type 漸變的類別,有 linear(線性漸變)、radial(徑向漸變)、sweep(掃描線漸變)三種,其中默認(rèn)值為線性漸變
  • <solid> 表示純色填充,通過 android:color 即可指定 shape 中填充的顏色
  • <stroke> Shape 的描邊,有以下屬性
  1. android:width 描邊的寬度,越大則 shape 的邊緣線看起來越粗
  2. android:color 描邊的顏色
  3. android:dashWidth 組成虛線的線段的寬度
  4. android:dashGap 組成虛線的線段之間的間隔,間隔越大看起來空隙就越大。
  • <padding> 表示空白,包含它的 View 的空白
  • <size> shape 的大小,有兩個(gè)屬性 android:width 和 android:height,用來標(biāo)記固有寬高

1.3 LayerDrawable

對(duì)應(yīng)標(biāo)簽 <layer-list>,表示的是一種層次化的 Drawable 集合。一個(gè) <layer-list> 可以包含多個(gè) <item>,每個(gè) item 表示一個(gè) Drawable。每個(gè) item 也可以是類似 <shape> 那樣的 Drawable。

  • android:drawable 指定圖片資源
  • android:top | right | bottom | left

1.4 StateListDrawable

對(duì)應(yīng) <selector> 標(biāo)簽,也是 Drawable 的集合,每個(gè) Drawable 對(duì)應(yīng) View 的一種狀態(tài),最常見的是 Button 的實(shí)現(xiàn)。

  • android:constantSize true 表示 StateListDrawable 的固有大小保持不變,最大值為內(nèi)部所有 Drawable 大小的最大值,false 會(huì)隨著狀態(tài)的改變而改變
  • android:dither 是否開啟抖動(dòng),默認(rèn)true
  • android:variablePadding padding 是否跟隨狀態(tài)改變而改變,不建議開啟
    View 常見狀態(tài):系統(tǒng)會(huì)根據(jù)狀態(tài)從上到下查找合適的 item,默認(rèn)的 item 都應(yīng)該放在 selector 的最后一條且不附帶任何狀態(tài)
狀態(tài) 含義
android:state_pressed 按下狀態(tài),比如 Button 按下沒有松開
android:state_focused View 已經(jīng)獲取了焦點(diǎn)
android:state_selected 選擇了 View
android:state_checked CheckBox 選中
android:state_enabled 當(dāng)前 View 處于可用狀態(tài)

1.5 LevelListDrawable

對(duì)應(yīng)于 <level-list> 標(biāo)簽,同樣表示一個(gè) Drawable 集合,集合中的每一個(gè) Drawable 都有一個(gè) level 概念,根據(jù)不同的等級(jí),LevelListDrawable 會(huì)切換對(duì)應(yīng)的 Drawable

  • android:drawable
  • android:maxLevel = "integer"
  • android:minLevel = "Integer"
    通過 Drawable 的 setImageLevel 來切換 Drawable,等級(jí)是有范圍的 0 - 10000.

1.6 TransitionDrawable

對(duì)應(yīng)于 <transition> 標(biāo)簽,用于實(shí)現(xiàn)兩個(gè) Drawable 直接的淡入淡出效果。

TransitionDrawable drawable = (TransitionDrawable) textView.getBackground();
drawable.startTransition(1000);

1.7 InsetDrawable

對(duì)應(yīng)于 <inset> 標(biāo)簽,可以將其他 Drawable 內(nèi)嵌到自己當(dāng)中,通過 LayerDrawable 也可以實(shí)現(xiàn)。

  • android:insetBottom | insetLeft | insetLeft | insetRight

1.8 ScaleDrawable

對(duì)應(yīng)于 <scale> 標(biāo)簽,根據(jù)自己的等級(jí)將指定的 Drawable 縮放到一定的比例。

1.9 ClipDrawable

對(duì)應(yīng)于 <clip> 標(biāo)簽,根據(jù)自己當(dāng)前的 level 來裁剪另一個(gè) Drawable

  • android:clipOrientation 表示裁剪方向,有水平和垂直兩個(gè)方向
  • android:gravity 標(biāo)簽裁剪的與 android:clipOrientation 配合的方向和位置
ImageView clipImage = (ImageView)findViewById(R.id.clip_image);
ClipDrawable clipDrawable = clipImage.getDrawable();
clipDrawable.setLevel(5000);

等級(jí)為 0 - 10000,5000表示裁剪一半。

1.10 自定義 Drawable

自定義 Drawable 可以用來實(shí)現(xiàn)作為ImageView、TextView等資源的背景

參考資料:

  1. Android Drawable 那些不為人知的高效用法
  2. 《Android 開發(fā)藝術(shù)探索》
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容