寫在最前:不管在什么系統(tǒng)中,如果你應(yīng)用的圖片資源比較多的話,會(huì)導(dǎo)致安裝包較大,比如四角有弧度的長(zhǎng)方形、圓形、正方形等等簡(jiǎn)單圖形,完全沒有比較實(shí)用圖片,在Android中如何優(yōu)雅的避免這個(gè)問(wèn)題呢,就是使用shape了。
1.新建文件
首先在你的項(xiàng)目下新建drawable文件夾,你的shape xml文件就放到這個(gè)地下,然后新建Android xml文件,如圖:
2.下面來(lái)介紹一下shape的屬性
如圖,shape有6個(gè)屬性,分別為:
- corners:設(shè)置圓角,即四個(gè)角的弧度
- gradient:顏色漸變
- padding:間隔,xml里經(jīng)常用到,不比多解釋
- size:長(zhǎng)寬
- solid:填充物,只有一個(gè)屬性 -> 顏色
- stroke:設(shè)置圖片邊緣顏色
來(lái)看一下官方給我的詳細(xì)解釋:
<?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>
形狀的屬性,只有這6種,下面詳細(xì)介紹一下6種屬性下的各個(gè)屬性的意義,
**1).corners:Creates rounded corners for the shape. Applies only when the shape is a rectangle.創(chuàng)建圓角的形狀。僅適用于當(dāng)其形狀是一個(gè)長(zhǎng)方形
android:radius="20dp" 為角的弧度,值越大角越圓。
android:topRightRadius="20dp" 右上角
android:bottomLeftRadius="20dp" 右下角
android:topLeftRadius="1dp" 左上角
android:bottomRightRadius="0dp" 左下角
一個(gè)提示:bottomLeftRadius 是右下角,而不是左下角,謹(jǐn)記謹(jǐn)記,其實(shí)記反了也沒有多大影響。
2).gradient:Specifies a gradient color for the shape.指定一個(gè)漸變顏色的形狀。
android:startColor 起始顏色
android:endColor 結(jié)束顏色
android:centerColor 中心顏色
android:angle 漸變角度,必須為45的整數(shù)倍。
android:type 漸變模式默認(rèn)為linear,即線性漸變,可以指定漸變?yōu)閺较驖u變,android:type="radial"
android:gradientRadius="50" 徑向漸變需要指定半徑
android:useLevel 值為true或false,至今不知道有什么作用
3)padding : 定義內(nèi)容離邊界的距離
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp"
略
4)size:The size of the shape.形狀的長(zhǎng)寬
android:height="100dp"
android:width="100dp"
略
5)solid :A solid color to fill the shape.填充顏色
android:color 指定填充的顏色
6)stroke:A stroke line for the shape.圖形邊緣畫線
android:width="2dp" 描邊的寬度
android:color="#FFFFFF" 描邊的顏色
android:dashWidth 虛線中這樣一個(gè)'-'橫線的寬度
android:dashGap 兩個(gè)'-'之間的距離