跳到主要內容

[Android] 使用shape自訂形狀及陰影

shape可以定義下面四種類型的形狀:
    rectangle-矩形(直角矩形、圓角矩形),默認的形狀
    oval-橢圓形、圓形
    line-線形(實線、虛線)
    ring-環形、環形進度條

rectangle


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle">
<!--設定形狀的顏色-->
<solid
android:color="@color/colorPrimary"/>
<!--設定圓角的尺寸-->
<corners
android:radius="5dp"/>
<!--設定邊框-->
<stroke
android:width="2dp"
android:color="@color/colorPrimaryDark"/>
<!--設定內容與邊界的距離-->
<padding
android:top="15dp"
android:right="15dp"
android:bottom="15dp"
android:left="15dp"/>
</shape>
</item>
</selector>
執行結果:



如果填色要填入漸層色的話,需要將solid標籤改成gradient標籤,用法如下:
<!--設定形狀漸層色-->
<gradient
android:startColor="@color/colorPrimary"
android:endColor="@color/colorAccent"
android:angle="90"/>
執行結果:



oval

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="oval">
<!--設定形狀的顏色-->
<solid
android:color="@color/colorPrimary"/>
<!--設定形狀尺寸-->
<size
android:width="70dp"
android:height="70dp"/>
<!--設定邊框-->
<stroke
android:width="2dp"
android:color="@color/colorPrimaryDark"/>
<!--設定內容與邊界的距離-->
<padding
android:top="10dp"
android:right="10dp"
android:bottom="10dp"
android:left="10dp"/>
</shape>
</item>
</selector>
view raw shape_oval.xml hosted with ❤ by GitHub
執行結果:


oval和ring的漸層有三種類型(android:type):linear(線性)、radial(放射性)、sweep(掃描)

如果漸層效果為radial(放射性)的話,必須加上android:gradientRadius屬性(指定漸層的半徑)。
<!--設定形狀漸層色-->
<gradient
android:startColor="@color/colorPrimary"
android:endColor="@color/colorAccent"
android:gradientRadius="70dp"
android:type="radial" />
view raw shape_oval.xml hosted with ❤ by GitHub
執行結果:

line

線的高度是透過stroke的android:width屬性設置,而size的android:height是定義整個形狀區域的高度,所以size的height必須大於stroke的width
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="line">
<!--設定形狀的顏色-->
<stroke
android:color="@color/colorPrimary"
android:width="2dp"/>
<!--設定形狀區域的高度-->
<size
android:height="3dp"/>
</shape>
</item>
</selector>
view raw shape_line.xml hosted with ❤ by GitHub
執行結果:

ring

屬於ring的屬性:
    android:innerRadius 內環的半徑
    android:thickness 環的厚度
    android:useLevel 一般設為false,要不然環可能會無法顯示
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<!--設定形狀的半徑、厚度-->
<shape
android:shape="ring"
android:innerRadius="30dp"
android:thickness="10dp"
android:useLevel="false">
<!--設定形狀的顏色-->
<solid
android:color="@color/colorPrimary"/>
<!--設定邊框-->
<stroke
android:width="1dp"
android:color="@color/colorAccent"/>
</shape>
</item>
</selector>
view raw shape_ring.xml hosted with ❤ by GitHub
執行結果:

將環設計成環狀進度條的樣式:

1,在shape標籤外再加上rotate標籤
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="1080.0">
<!--設定形狀的半徑、厚度-->
<shape
android:shape="ring"
android:innerRadius="30dp"
android:thickness="10dp"
android:useLevel="false">
<!--設定形狀的漸層-->
<gradient
android:startColor="@color/colorPrimary"
android:endColor="@color/colorAccent"
android:type="sweep"/>
<!--設定邊框-->
<stroke
android:width="1dp"
android:color="@color/colorAccent"/>
</shape>
</rotate>
</item>
</selector>
view raw shape_ring.xml hosted with ❤ by GitHub
rotate屬性:
    android:fromDegrees:開始旋轉的角度位置
    android:toDegrees:結束時轉到的角度位置
    android:pivotX:旋轉起點的X軸座標位置,可以是數值、百分比、百分比p
    android:pivotY:旋轉起點的Y軸座標位置,可以是數值、百分比、百分比p

2,在style.xml裡加上style樣式
<style name="ring">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:indeterminateDrawable">@drawable/shape_ring</item>
</style>
view raw style.xml hosted with ❤ by GitHub
3,在activity_main.xml裡加上progressBar標籤
<ProgressBar
android:layout_width="100dp"
android:layout_height="100dp"
style="@style/ring"/>
執行結果:


Shape加上陰影的方法

將selector標籤改成layer-list標籤,並在原本形狀的item標籤上面再加上一組item標籤,作為陰影的樣式
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--陰影部分-->
<!--設置陰影左邊跟上面的offset-->
<item
android:left="3dp"
android:top="3dp">
<shape
android:shape="rectangle">
<!--設定陰影顏色-->
<solid
android:color="#56000000" />
<!--設定圓角的尺寸-->
<corners
android:radius="5dp"/>
</shape>
</item>
<!--物件部分-->
<!--設置物件右邊跟下面的offset-->
<item
android:bottom="3dp"
android:right="3dp">
<shape
android:shape="rectangle">
<!--設定形狀的顏色-->
<solid
android:color="@color/colorPrimary"/>
<!--設定圓角的尺寸-->
<corners
android:radius="5dp"/>
<!--設定邊框-->
<stroke
android:width="2dp"
android:color="@color/colorPrimaryDark"/>
<!--設定內容與邊界的距離-->
<padding
android:top="15dp"
android:right="15dp"
android:bottom="15dp"
android:left="15dp"/>
</shape>
</item>
</layer-list>
一定要設置陰影和物件的offset,要不然陰影和物件會重疊,會看不到效果。
執行結果:





參考資料:Keegan小钢

留言

這個網誌中的熱門文章

[隋堂筆記] Android 利用BaseAdapter來自訂ListView

BaseAdapter 可以讓使用者自己定義許多種 View ,像是 Spinner, ListView, GridView等,而且 可以重用View來節省資源 :使用setTag將View存起來,之後就可以利用getTag將數據取出來 Step1  在activity_main.xml中新增ListView 例如: Step2 在layout中新增list_content.xml,並排好想要的ListView item佈局 Step3 在MainActivity.java 新增一個類別BA並繼承BaseAdapter 透過繼承 BaseAdapter,我們可以重新覆寫4個方法:     public int getCount();     取得 ListView 列表 Item 的數量。通常數量就是從建構子傳入的陣列或是集合大小。     public Object getItem(int position);     取得 ListView 列表於 position 位置上的 Item。position 通常是資料在陣列或是集合上的位置。     public long getItemId(int position);     取得 ListView 列表於 position 位置上的 Item 的 ID,一般用 position 的值即可。     public View getView(int position, View view, ViewGroup viewGroup);     通常會設定與回傳View 作為顯示在這個 position 位置的 Item 的 View。 基本上只要處理兩個方法 getCount跟getView即可。 getCount回傳我所宣告的String陣列st1的長度。 在getView之前,要先取得LayoutInflater 再來要先宣告一個類別viewHolder,用來存放list_content.xml內所有元件的內容 接著判斷view是否為空,如果為空就要...

[iOS] Objective-C 自訂UITableViewCell樣式

Step1 在Storyboard中拉出TableView和按下TableView Cell後要切換的View,並設定對應的class檔 TableView連結至ListController類別,並設定Storyboard ID為ListController 要切換的View連結至ViewController類別,並設定Storyboard ID為viewController Step2 新增CustomCell.xib 新增File(File → New → File...),選擇View,並將檔案命名為CustomCell 開啟CustomCell.xib,把預設的View元件刪掉,然後從元件庫拖曳Table View Cell,並拉好想要的Cell內容排版 Step3 新增自訂Objective-C class給這個NIB檔 新增File(File → New → File...),選擇Cocoa Touch class 需要繼承UITableViewCell Step4 回到CustomCell.xib,將Class連結到CustomTableViewCell Step5 在CustomTableViewCell.h檔中,設定元件變數 Step6 在ListController.h中設定TableView的元件變數,且宣告一個陣列,負責儲存表格上要呈現的資料 Step7 在ListController.m  的viewDidLoad方法中設定TableView的兩個輸出口:dataSource、delegate,並初始化陣列資料 dataSource所連結到的View Controller用來提供表格上Cell的內容 delegate為指定哪一個View Controller需要處理使用者在表格上的操作 Step8 在ListController.m中,實作tableView:numberOfRowsInSection:方法 -通知Table View需要產生多少個Cell(儲存格)來顯示資料 Step9 在ListController.m中,實作tableView:cellForRowAtIndexPat...