Android Drawable(一)之ShapeDrawable

在Android開發中經常需要自定義一些形狀,這個時候就要用到shape drawable。

Android Developer : http://developer.android.com/intl/zh-cn/guide/topics/resources/drawable-resource.html#Shape

創建及使用shape drawable

  • 文件位置:res/drawable/filename.xml
  • 編譯資源類型:GradientDrawable
  • 資源引用:
    - In Java: R.drawable.filename
    - In XML: @drawable/filename

具體結構:

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

Elements說明

shape:根節點

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape=["rectangle" | "oval" | "line" | "ring"] //矩形|橢圓|線|圓環

//當android:shape="ring"時,還需要指定以下屬性:
 android:innerRadius:指定內環的半徑
 android:thickness:指定環的厚度  
 android:innerRadiusRatio:寬度比,如果該值爲2,則內環半徑等於環的寬度除以5
 android:thicknessRatio:寬度比,如果該值爲2,則環的厚度等於環的寬度除以2
 android:useLevel:當爲ring時,該屬性必須指定爲false
>

stroke:邊框

<stroke       
    android:width="dimension"   //邊框寬度  
    android:color="color"       //邊框顏色  
    android:dashWidth="dimension"   //虛線寬度
    android:dashGap="dimension" />  //虛線間隔 

solid:填充色

<solid  android:color="color" />  

gradient:漸變

<gradient 
    android:type=["linear" | "radial" | "sweep"]    //共有3中漸變類型,線性漸變(默認)/放射漸變/掃描式漸變  
    android:angle="integer"     //漸變角度,必須爲45的倍數,0爲從左到右,90爲從上到下  
    android:centerX="float"     //漸變中心X的相當位置,範圍爲0~1  
    android:centerY="float"     //漸變中心Y的相當位置,範圍爲0~1  
    android:startColor="color"   //漸變開始點的顏色  
    android:centerColor="color"  //漸變中間點的顏色,在開始與結束點之間  
    android:endColor="color"    //漸變結束點的顏色  
    android:gradientRadius="float"  //漸變的半徑,只有當漸變類型爲radial時才能使用  
    android:useLevel=["true" | "false"] />  //使用LevelListDrawable時就要設置爲true。設爲false時纔有漸變效果  

三種漸變效果演示

corners:圓角半徑

這個element僅僅在rectangle時使用

<corners
        android:radius="integer"   //該屬性指定四個圓角的半徑,不能和其他屬性重用
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />

size:shape大小

<size
        android:width="integer"
        android:height="integer" />

padding:內邊距

<padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />

size和padding在具體控件也可以指定。

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