android中的shape资源,Android Drawable资源讲解之shape篇
運用好Android的drawable資源,在開發中就可以減少圖片的使用量,這樣既縮小了app的體積,也省去了設計圖片的時間,而且也容易適配不同的屏幕尺寸。
今天首先來講講shape資源的使用。
1. Shape的屬性介紹
shape共支持四種形狀,在根節點通過android:shape=””來定義
rectangle
方形
oval
橢圓形
line
線形,需要節點來定義其屬性
ring
環形
我們常用的應該是rectangle和oval形狀,我們上個圖來看一下總體效果:
其中下面幾個屬性是環形時才會用到的:
android:innerRadius
尺寸。環內部(中間的孔)的半徑,以尺寸值或尺寸資源表示。
android:innerRadiusRatio
浮點型。環內部的半徑,以環寬度的比率表示。例如,如果 android:innerRadiusRatio=”5″,則內半徑等于環寬度除以 5。此值被 android:innerRadius 覆蓋。默認值為 9。
android:thickness
尺寸。環的厚度,以尺寸值或尺寸資源表示。
android:thicknessRatio
浮點型。環的厚度,表示為環寬度的比率。例如,如果 android:thicknessRatio=”2″,則厚度等于環寬度除以 2。此值被 android:innerRadius 覆蓋。默認值為 3。
android:useLevel
布爾值。如果這用作 LevelListDrawable,則此值為“true”。這通常應為“false”,否則形狀不會顯示。當我們要設計一個圓形時必須設置它為false。
2.子節點之實色填充效果
solid節點可以實現形狀的填充效果,通過android:color屬性設置顏色值,如下示例:
XHTML
android:shape="rectangle">
1
2
3
4
5
6
android:shape="rectangle">
3.子節點之漸變填充效果
gradient節點可以實現漸變的填充效果,示例如下:
XHTML
android:shape="rectangle">
android:endColor="#33ddee"
android:centerColor="#eedd33"
android:angle="90"
android:centerX="0.4"
android:centerY="0.4"
android:type="linear">
1
2
3
4
5
6
7
8
9
10
11
12
android:shape="rectangle">
android:endColor="#33ddee"
android:centerColor="#eedd33"
android:angle="90"
android:centerX="0.4"
android:centerY="0.4"
android:type="linear">
4.子節點之實現邊框效果
XHTML
android:shape="rectangle">
android:endColor="#33ddee"
android:centerColor="#eedd33"
android:angle="90"
android:centerX="0.4"
android:centerY="0.4"
android:type="linear">
android:width="2dp"
android:dashGap="2dp"
android:dashWidth="2dp">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
android:shape="rectangle">
android:endColor="#33ddee"
android:centerColor="#eedd33"
android:angle="90"
android:centerX="0.4"
android:centerY="0.4"
android:type="linear">
android:width="2dp"
android:dashGap="2dp"
android:dashWidth="2dp">
各個屬性值的解釋:
android:color
線框的顏色
android:width
線框的寬度
android:dashGap
如果畫虛線框,虛線框的間隙
android:dashWidth
如果畫虛線框,虛線框的寬度
5.子節點之實現圓角效果
為了美觀,我們在畫按鈕時都會給按鈕背影加個圓角效果,在shape里可以通過corner節點來實現圓角效果,如下所示:
XHTML
android:shape="rectangle">
android:endColor="#33ddee"
android:centerColor="#eedd33"
android:angle="90"
android:centerX="0.4"
android:centerY="0.4"
android:type="linear">
android:width="1dp">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
android:shape="rectangle">
android:endColor="#33ddee"
android:centerColor="#eedd33"
android:angle="90"
android:centerX="0.4"
android:centerY="0.4"
android:type="linear">
android:width="1dp">
radius用于設置圓角的半徑大小,也可以通過android:topLeftRadius,android:topRightRadius,android:bottomLeftRadius,android:bottomRightRadius分別設置四個角的圓角半徑大小,比如在實現tab標簽頭部按鈕時,我們可能只需要為左上和右上角設置圓角效果。比如還可以實現一些特殊形狀的導航按鈕等等。
6.padding節點
padding節點用于設置形狀相對于四邊的內部間距,可以用于在layer-list中設置陰影的效果。
打賞
微信掃一掃,打賞作者吧~
總結
以上是生活随笔為你收集整理的android中的shape资源,Android Drawable资源讲解之shape篇的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pm1 android,Android
- 下一篇: 家居灯光控制系统设计 android,基