Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Drawable 全解:九種常見 Drawable 用法詳解

編輯:關於 Android 程式設計

1. BitmapDrawable 相關方法

新建於 drawable 目錄下,示例如下:

  

各屬性含義如下:

   android:antialias="true"  android:dither="true"  android:filter="true"  android:gravity="center"  android:mipMap="false"  android:src="@drawable/ic_launcher"  android:tileMode="disabled" /> 

上述排版僅為方便查看,不能直接複製使用。

2. ShapeDrawable:純色、漸變色的 Drawable

基本示例代碼:

         

各標簽詳細說明:

<shape> 表示圖形形狀,有 rectangle(矩形)、oval(橢圓)、line(橫線)、ring(圓環)四種,默認矩形。使用 line 和 ring 時需通過 stroke 指定線寬與顏色,否則顯示異常。設置 ring 時有五個特殊屬性:innerRadius(內半徑,與 innerRadiusRatio 同時存在時以前者為準)、thickness(圓環厚度,即外半徑減內半徑)、innerRadiusRatio(內半徑占寬度比例,默認 9)、thicknessRatio(厚度占比例,默認 3,厚度=寬度/n)、useLevel(一般為 false,除非作為 LevelListDrawable 使用)。

<corners> 表示四個角的角度,僅適用於矩形,用 px 表示圓角程度。radius 為四角統一設置角度但優先級較低,topLeftRadius、topRightRadius、bottomLeftRadius、bottomRightRadius 分別對應四個角。

<gradient> 表示漸變填充,與 solid 互斥。屬性包括:angle(漸變角度,默認 0,必須為 45 的倍數)、centerX/centerY(漸變中心點座標)、startColor/centerColor/endColor(起止與中間色)、gradientRadius(漸變半徑)、useLevel(一般為 false)、type(linear 線性、radial 徑向、sweep 掃描線,默認線性)。

<solid> 用於純色填充,通過 android:color 指定填充顏色。

<stroke> 為描邊屬性:width(描邊寬度)、color(描邊顏色)、dashWidth(虛線線段寬度)、dashGap(線段間隔)。

<padding> 為內邊距,包含 top、bottom、left、right 四個屬性,用法與在 view 中加邊距類似。

<size> 包含 android:width 與 android:height,表示 view 的寬高,會視情況拉伸。

3. LayerDrawable:層次化的 Drawable(類似圖層)

它是多個 Drawable 的集合,類似圖層概念,通過將不同 Drawable 放在不同層上達到疊加效果,語法如下:

      

一個 layer-list 可包含多個 item,每個 item 代表一個 Drawable。上下左右四個屬性表示 Drawable 相對 view 的偏移量(像素),可用 drawable 直接引用資源,也可在 item 下自定義 Drawable。默認所有 Drawable 都會被縮放到 view 大小;由於存在層次概念,下面的 Drawable 會覆蓋上面的,合理分層即可實現特殊的疊加效果。

4. StateListDrawable(選擇器)

選擇器對應 <selector> 標簽,也是 Drawable 的集合,常用於根據當前狀態改變顏色:

   android:constantSize="true"  android:dither="true"  android:variablePadding="true">  android:drawable="@drawable/ic_launcher"  android:state_pressed="true"  android:state_focused="true"  android:state_hovered="true"  android:state_selected="true"  android:state_checkable="true"  android:state_checked="true"  android:state_enabled="true"  android:state_activated="true"  android:state_window_focused="true">   

注意:若有多個 item,程序會從上到下匹配,最先匹配者生效(並非最佳匹配)。沒有任何狀態說明的 item 可被任意狀態匹配,因此默認 item 應放在最後一條,否則會導致下面的選項失效。

5. LevelListDrawable(不同等級切換不同 Drawable)

LevelListDrawable 對應 <level-list> 標簽,集合中每個 Drawable 都有一個等級,根據不同等級切換為對應的 Drawable:

    android:drawable = "@drawable/ic_lunch"  android:maxLevel = "2"  android:minLevel = "1">   

6. TransitionDrawable(可實現淡入淡出)

用於實現兩個 Drawable 之間的淡入淡出效果,使用方法如下:

定義漸變:

     

將漸變添加到背景:

 

在代碼中控制:

 TextView textView = (TextView)findViewById(R.id.button); TransitionDrawable drawable = (TransitionDrawable)textView.getBackground(); drawable.startTransition(1000);

7. InsetDrawable(內嵌一個 Drawable)

可將 Drawable 內嵌到自己內部並在四周留出空隙,常用於希望背景比實際區域小的 view:

   android:drawable="@drawable/ic_launcher"  android:insetBottom="2dp" android:insetLeft="2dp" android:insetRight="2dp" android:insetTop="2dp" >   

8. ScaleDrawable(縮放類,可以縮小)

對應代碼如下:

   android:drawable="@drawable/ic_launcher"  android:scaleGravity="center"  android:scaleHeight="50%" android:scaleWidth="50%" >  

使用時必須設置 Drawable 的級別,否則無法生效:

 View testScale = findViewById(R.id.test_scale); scaleDrawable testScaleDrawable = (ScaleDrawable)testScale.getBackground(); testScaleDrawable.setLevel(1);//范圍是1~10000,只要不為零就可以

9. ClipDrawable(根據等級裁剪 Drawable)

可根據當前等級裁剪另一個 Drawable:

   android:clipOrientation="horizontal" android:drawable="@+id/icon"  android:gravity="top" >  

gravity 取值說明:top(頂部,豎直裁剪時從底部開始裁)、bottom(底部,從頂部開始裁)、left(左邊,水平裁剪時從右開始裁,默認)、right(右邊,從左邊開始裁)、center_vertical(豎直居中,上下同時裁)、fill_vertical(豎直填充,等級為 0 時才有裁剪行為)、center(中部居中)、fill(充滿整個 Drawable,等級為 0 時有效)、clip_vertical/clip_horizontal(附加選項,表示豎直/水平方向裁剪)。水平方向也有 center_horizontal 和 fill_horizontal 等對應屬性。

使用時需在代碼中設置等級:

 ImageViwe testClip = (ImageView)findViewById(R.id.test_clip); ClipDrawable testClipDrawable = (ClipDrawable)testClip.getDrawable(); //等級為從0到10000,等級0表示完全裁剪,等級10000表示不裁剪 testClipDrawable.setLevel(5000);
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved