天天看點

Drawable資源——ShapeDrawable資源Drawable資源——ShapeDrawable資源

Drawable資源——ShapeDrawable資源

1,認識

ShapeDrawable資源:
在Android開發中可以去定義各種各樣的形狀。
用于定義一個基本的幾何圖形(如矩形、圓形、線條等)。
建立XML檔案:
定義ShapeDrawable的XML檔案的根本元素是<shape.../>,
檔案位置 ︰
res/drawable/ filename.xml
檔案名用作資源 id。
文法:

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

2,各屬性注解

     (1)<shape> 屬性

xmlns:android
 字元串。所需。定義 XML 命名空間,
必須是"http://schemas.android.com/apk/res/android". 
android:shape   指定哪種類型的幾何圖形。
"rectangle"     一個矩形,填充包含的視圖。這是預設的形狀。
"oval"     橢圓形的形狀,互相吻合包含視圖的尺寸。
"line"     包含視圖的寬度的水準線。這種形狀要求<stroke>元素,
               定義線的寬度。
"ring"     一個環的形狀。
   示例:android:shape=["rectangle(長方形、矩形)                  "|"oval(橢圓形)"|"line(線性)"|"ring(環形)"] 
下面的屬性隻有在android:shape="ring時可用:

android:innerRadius         

              圓環的内半徑,和android:innerRadiusRatio同時存在時,

              以innerRadius為準

android:innerRadiusRatio 

              内半徑占整個Drawable寬度的比例,預設值為9,。如果為n,

              ,那麼内半徑=寬度/n.          

android:thickness              

              圓環的厚度,即外半徑減去内半徑的值,和android:thicknessRatio

              同時存在時,以android:thickness為準。

android:thicknessRatio 

               厚度占整個Drawable寬度的比例,預設值為3.如果為n,那麼

               厚度 =寬度/n.

android:useLevel 

              一般為false,否則有可能無法達到預期的顯示效果,除非當做

              是LevelListDrawable使用時值為true

(2)<corners>  屬性

         表示Shape的四個角的角度,隻适用于矩形。

圓角 android:radius為角的弧度,值越大角越圓。
        還可以把四個角設定成不同的角度。
屬性
android:radius                             四個角的弧度,例如5dp
android:topLeftRadius                左上角的弧度,例如5dp
android:topRightRadius             右上角的弧度,例如5dp
android:bottomLeftRadius         左下角的弧度,例如5dp
android:bottomRightRadius      右下角的弧度,例如5dp
注意:  同時設定五個屬性,則Radius屬性無效       數值為整型  
(3)<gradient> 

指定形狀的漸變顔色。

      它與<solid>标簽是互斥的,其中solid表示純色填充,而gradient則表示漸變效果。

屬性 ︰
android:angle    
 漸變角度,以度為機關。預設值為 0。(當angle=0時,漸變色
是從左向右。 然後逆時針方向轉,當angle=90時為從下往上。
angle必須為45的整數倍)
android:centerX
漸變中心橫坐标的相對位置 
android:centerY
漸變中心縱坐标的相對位置,漸變的中心點會影響漸變的具體效果
android:centerColor
漸變的中間顔色,作為十六進制值或顔色資源的可選顔色.
android:endColor
漸變的 結束顔色,作為十六進制值或顔色資源的可選顔色.
android:startColor
漸變的 起始顔色,作為十六進制值或顔色資源的可選顔色.
android:type
 漸變模式,預設的為線性漸變linear  
"linear"     線性漸變。這是預設設定。
"radial"     徑向漸變。開始是中心顔色。
"sweep"     掃線漸變。
android:gradientRadius
漸變色半徑.當 android:type="radial" 時才使用。單獨使用 
android:type="radial"會報錯。 浮點數值
android:useLevel  
如果要使用LevelListDrawable對象,就要設定為true。
設定為true無漸變。false有漸變色 
(4)<padding> 屬性
padding 内邊距,即内容與邊的距離  數值為整型
android:left   左邊距
android:top    上邊距
android:right  右邊距
android:bottom   底部邊距
(5)<size>屬性
形狀的大小。
屬性 ︰
android:height   高度.
android:width    寬度
注 ︰
對于Shape來說,預設情況下它是沒有固定寬/高這個概念的,但通過size設定後,shape就有了所謂的寬/高,但是作為View的背景時,shape還會被拉伸或縮小為View的大小。

     (6)<solid>

            固體的顔色來填充形狀。

            屬性:

                     android:color

                          填充的顔色。要将作為十六進制值或顔色資源應用于形狀的顔色.
(7)<stroke>
描邊形狀線條的。
屬性 ︰
android:width        描邊的寬度 ,越大則shape的邊緣線就會看起來越粗
android:color         描邊的顔色  
android:dashGap
組成虛線的線段之間的間隔,間隔越大則虛線看起來空隙越大 即 。僅當android:dashWidth設定才有效。
android:dashWidth

組成虛線的線段的寬度, 值為0時,表示為實線。值大于0則為虛線。 僅當android:dashGap設定才有效。

注意:隻要android:dashGap和android:dashWidth有任何一個為0,那麼虛線效果将不能生效。

3,示例代碼

(1)shape_drawable_useing_1.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 設定填充顔色 -->
    <solid android:color="#fff"/>
    <!-- 設定四周的内邊距 -->
    <padding android:left="7dp" 
        android:top="7dp" 
        android:right="7dp" 
        android:bottom="7dp" />
    <!-- 設定邊框 -->
    <stroke android:width="3dip" android:color="#ff0" />
</shape>
(2)shape_drawable_useing_2.xml
<?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>
(3)shape_drawable_useing_3.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval">
    <!-- 定義填充漸變顔色 -->
    <gradient 
        android:startColor="#ff0" 
        android:endColor="#00f" 
        android:angle="45"
        android:type="sweep"/> 
    <!-- 設定内填充 -->
    <padding android:left="7dp" 
        android:top="7dp" 
        android:right="7dp" 
        android:bottom="7dp" />
    <!-- 設定圓角矩形 -->
    <corners android:radius="8dp" /> 
</shape>

繼續閱讀