Android Drawable

BitmapDrawable:

這幾乎是最簡單的Drawable了,他表示的就是一張圖片。在實際開發中,我們可以直接引用原始的圖片即可,但是也可以通過xml的方式來描述他,通過cml來描述的BitmapDrawable可以設置更多的效果:

<bitmap

xmlns:android="http://schemas.android.com/apk/res/android"

android:src="@[package:]drawable/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:titleMode=["disable"|"clamp"|"repeat"|"mirror"]

下面是它的各個屬性的含義:

android:src

這個很簡單,就是圖片的資源id

android:antialias

是否開啓圖片的抗鋸齒功能。開啓後會讓圖片變得平滑,同時會在一定程度上降低圖片的清晰度,但是這個降低的幅度較低以至於可以忽略,因此抗鋸齒選項應該開啓。

android:dither

是否開啓抖動效果。當圖片的像素配置和手機屏幕配置不一致時開啓這個選項可以讓高質量的圖片在低質量的屏幕上還能保持良好的顯示效果

android:filter

是否開啓過濾效果。當圖片尺寸被拉伸或者壓縮時,開啓過濾效果可以保持較好的顯示效果。

android:gravity

當圖片小於容器的尺寸時,設置此項可以對圖片進行定位。這個屬性選項比較多,不同的選項可以通過“|”來組合使用

android:mipMap

一種圖像處理技術,不常用

android:titleMode

平鋪模式。這個選項有如下幾個值:["disable"]["clamp"]["repeat"]["mirror"]disable表示關閉平鋪模式,此值爲默認值。其它三個都表示開啓平鋪模式,但效果有差別,具體在使用中感受。

ShapeDrawable

ShapDrawable是一種很常見的Drawable,可以理解爲通過顏色來構造的圖形,他既可以是純色的圖形,也可以是具有漸變效果的圖形。語法規則如下:

<?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="integer"
       
android:centerY="integer"
       
android:centerColor="integer"
       
android:endColor="color"
       
android:gradientRadius="integer"
       
android:startColor="color"
       
android:type=["linear" | "radial" | "sweep"]
       
android:usesLevel=["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>
<shape>
定義這是一個GradientDrawable,必須作爲根元素。

屬性:

xmlns:android
String類型。必須的,定義xml文件的命名空間,必須是"http://schemas.android.com/apk/res/android".
android:shape
關鍵字。定義shape的值,必須是下面的之一:
描述
"rectangle" 矩陣,這也是默認的shape
"oval" 橢圓
"line" 一條水平的直線。這種shape必須使用 <stroke> 元素來定義這條線的寬度
"ring" 圓環

下面的屬性只有當 android:shape="ring"才使用:

android:innerRadius
尺寸。 內環的半徑。一個尺寸值(dip等等)或者一個尺寸資源。
android:innerRadiusRatio
Float類型。這個值表示內部環的比例,例如,如果android:innerRadiusRatio = " 5 ",那麼內部的半徑等於環的寬度除以5。這個值會被android:innerRadius重寫。 默認值是9。
android:thickness
尺寸。環的厚度,是一個尺寸值或尺寸的資源。
android:thicknessRatio
Float類型。厚度的比例。例如,如果android:thicknessRatio= " 2 ",然後厚度等於環的寬度除以2。這個值是被android:innerRadius重寫, 默認值是3。
android:useLevel
Boolean類型。如果用在 LevelListDrawable裏,那麼就是true。如果通常不出現則爲false。
<corners>
爲Shape創建一個圓角,只有shape是rectangle時候才使用。

屬性:

android:radius
Dimension。圓角的半徑。會被下面每個特定的圓角屬性重寫。
android:topLeftRadius
Dimension。top-left 圓角的半徑。
android:topRightRadius
Dimension。top-right 圓角的半徑。
android:bottomLeftRadius
Dimension。 bottom-left圓角的半徑。
android:bottomRightRadius
Dimension。bottom-right圓角的半徑。

注意:每個圓角半徑值都必須大於1,否側就沒有圓角。

             下面的話不明白,我直接設置圓角爲0就可以不圓了,其餘的設置有圓角,一樣的可行。不知道它爲什麼要這麼講。

(If you want specific cornersto not be rounded, a work-around is to use android:radius to set a default cornerradius greater than 1, but then override each and every corner with the values you reallywant, providing zero ("0dp") where you don't want rounded corners.)

<gradient>
指定這個shape的漸變顏色。

屬性:

android:angle
Integer。漸變的角度。 0 代表從 left 到 right。90 代表bottom到 top。必須是45的倍數,默認爲0
android:centerX
Float。漸變中心的相對X座標,在0到1.0之間。
android:centerY
Float。漸變中心的相對Y座標,在0到1.0之間。
android:centerColor
Color。可選的顏色值。基於startColor和endColor之間。
android:endColor
Color。 結束的顏色。
android:gradientRadius
Float 。漸變的半徑。只有在 android:type="radial"才使用
android:startColor
Color。開始的顏色值。
android:type
Keyword。漸變的模式,下面值之一:
描述
"linear" 線形漸變。這也是默認的模式
"radial" 輻射漸變。startColor即輻射中心的顏色
"sweep" 掃描線漸變。
android:useLevel
Boolean。如果在LevelListDrawable中使用,則爲true

<padding>

     內容與視圖邊界的距離

屬性:

android:left
Dimension。左邊填充距離.
android:top
Dimension。頂部填充距離.
android:right
Dimension。右邊填充距離.
android:bottom
Dimension。底部填充距離.
<size>
這個shape的大小。

屬性:

android:height
Dimension。這個shape的高度。
android:width
Dimension。這個shape的寬度。

注意:默認情況下,這個shape會縮放到與他所在容器大小成正比。當你在一個ImageView中使用這個shape,你可以使用 android:scaleType="center"來限制這種縮放。

<solid>
填充這個shape的純色

屬性:

android:color
Color。顏色值,十六進制數,或者一個Color資源
<stroke>
這個shape使用的筆畫,當android:shape="line"的時候,必須設置改元素。

屬性:

android:width
Dimension。筆畫的粗細。
android:color
Color。筆畫的顏色
android:dashGap
Dimension。每畫一條線就間隔多少。只有當android:dashWidth也設置了纔有效。
android:dashWidth
Dimension。每畫一條線的長度。只有當 android:dashGap也設置了纔有效。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章