Android中提供了shape形狀給我們使用,可以通過(guò)shape畫(huà)出虛線、圓角、漸變等多種效果,而且,shape是XML代碼,比圖片更小,在開(kāi)發(fā)中,我們推薦使用shape,能用shape就用shape。
概述
用shape畫(huà)形狀,XML的根節(jié)點(diǎn)是shape,shape的取值有四個(gè),簡(jiǎn)單的說(shuō)就是,我們需要在根節(jié)點(diǎn)設(shè)置android:shape=""屬性,這個(gè)屬性取值有4個(gè):rectangle(長(zhǎng)方形),oval(橢圓),line(線條),ring(圓環(huán))。
需要注意一下幾個(gè)屬性,只有在設(shè)置畫(huà)的形狀是ring(圓環(huán))的時(shí)候才會(huì)起作用:
android:innerRadius=""屬性:這個(gè)屬性是設(shè)置內(nèi)環(huán)的半徑。
android:innerRadiusRatio=""屬性:這個(gè)屬性是設(shè)置內(nèi)環(huán)的比例,假設(shè)我設(shè)置的值是2,那么內(nèi)環(huán)半徑就是外圓半徑除以2,注意,如果設(shè)置了android:innerRadius=""屬性,那么內(nèi)環(huán)比例這個(gè)屬性就不會(huì)起作用。
android:thickness=""屬性:這個(gè)屬性是設(shè)置圓環(huán)的厚度。
android:thicknessRatio=""屬性:這個(gè)屬性是設(shè)置圓環(huán)的厚度比例,假設(shè)我設(shè)置的值是2,那么圓環(huán)的厚度就是環(huán)半徑除以2,但是如果設(shè)置了android:thickness=""屬性,那么圓環(huán)厚度比例屬性就不生效。
android:useLevel=""屬性:這個(gè)屬性是設(shè)置使用級(jí)別,只有當(dāng)我們的shape使用在LevelListDrawable中的時(shí)候,這個(gè)值為true,否則為false。
shape可以在Layout和selector中使用,shape有6個(gè)子標(biāo)簽,分別是:
1.corners圓角:圓角標(biāo)簽可以設(shè)置android:Radius=""屬性,表示4個(gè)角的半徑,也可以通過(guò)下面4個(gè)屬性單獨(dú)設(shè)置
android:topLeftRadius="" ? ? ? ? ? ? ? ? 設(shè)置左上角的半徑
android:topRightRadius="" ? ? ? ? ? ? ?設(shè)置右上角的半徑
android:bottomLeftRadius="" ? ? ? ? ?設(shè)置右下角的半徑
android:bottomRightRadius="" ? ? ? ?設(shè)置左下角的半徑
2.gradient漸變:設(shè)置shape的漸變色,有如下屬性:
android:angle=""屬性:表示漸變的角度,必須是45的倍數(shù),可以設(shè)置為0,0表示從左往右漸變,逆時(shí)針旋轉(zhuǎn),依次是45,90,135.....,90表示從下往上漸變,270表示從上往下漸變,依次下去
android:startColor=""屬性:設(shè)置漸變的起始顏色
android:centerColor=""屬性:設(shè)置漸變的過(guò)渡顏色
android:endColor=""屬性:設(shè)置漸變的結(jié)束顏色
android:type=""屬性:設(shè)置漸變的類型,有三種類型,分別是:linear(線性變化),radial(輻射漸變)以及sweep(掃描漸變)
android:gradientRadius=""屬性,設(shè)置漸變半徑,只有當(dāng)漸變類型是radial(輻射漸變)的時(shí)候才需要設(shè)置
3.padding內(nèi)邊距:內(nèi)邊距沒(méi)有什么好說(shuō)的,就是設(shè)置上、下、左、右四個(gè)方向的距離。
4.size大下:大小也沒(méi)什么好說(shuō)的,就是設(shè)置寬度和高度
5.solid填充:設(shè)置填充顏色,如果設(shè)置了這個(gè)屬性,那么gradient屬性就不生效
6.stroke描邊:設(shè)置邊線的屬性:虛線或者實(shí)線、大小、顏色。有如下屬性:
android:width="" ? ? ? ? ? ? ? ? ?設(shè)置邊邊的寬度
android:color="" ? ? ? ? ? ? ? ? ?設(shè)置邊邊的顏色
android:dashWidth="" ? ? ? ? 設(shè)置虛線的寬度
android:dashGap="" ? ? ? ? ? 設(shè)置虛線的間隔寬度
需要注意的是:dashWidth和dashGap屬性,只要其中一個(gè)設(shè)置為0dp或者不設(shè)置,邊框是實(shí)線邊框
使用
上面我們講了很多shape的便簽和屬性,都是概念性的東西,下面我們來(lái)具體使用一下
使用shape畫(huà)一條虛線
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
? ?android:shape="line">
<!-- 設(shè)置虛線的樣式-->
<stroke
? ? android:width="2dp"
? ? android:color="@color/colorPrimaryDark"
? ? android:dashGap="5dp"
? ? android:dashWidth="8dp"/>
<size android:height="20dp"/>
</shape>
使用shape畫(huà)一條實(shí)線
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
? ?android:shape="line">
<!-- 設(shè)置實(shí)線的樣式-->
<stroke
? ? android:width="2dp"
? ? android:color="@color/colorPrimaryDark"
? ? />
<size android:height="20dp"/>
</shape>
使用shape畫(huà)一個(gè)實(shí)線邊框
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
? ?android:shape="rectangle">
<stroke
? ? android:width="2dp"
? ? android:color="@color/colorPrimaryDark"/>
<corners android:radius="8dp"/>
</shape>
使用shape畫(huà)一個(gè)虛線邊框
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
? ?android:shape="rectangle">
<stroke
? ? android:width="2dp"
? ? android:color="@color/colorPrimaryDark"
? ? android:dashGap="3dp"
? ? android:dashWidth="5dp"/>
<corners android:radius="8dp"/>
</shape>
使用shape畫(huà)一個(gè)漸變邊框
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
? ?android:shape="rectangle">
<corners
? ? android:bottomLeftRadius="8dp"
? ? android:bottomRightRadius="8dp"
? ? android:topLeftRadius="8dp"
? ? android:topRightRadius="8dp"/>
<stroke
? ? android:width="2dp"
? ? android:color="@color/colorPrimaryDark"/>
<gradient
? ? android:angle="180"
? ? android:endColor="#197600"
? ? android:startColor="#9cff00"/>
</shape>
使用shape畫(huà)一個(gè)圓環(huán)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
? ?android:shape="ring"
? ?android:useLevel="false">
<solid android:color="#006BB0"/>
<stroke
? ? android:width="1dp"
? ? android:color="#ffffff"/>
<size
? ? android:width="20dp"
? ? android:height="20dp"/>
</shape>
需要注意的是,在Android3.0之后會(huì)開(kāi)啟硬件加速功能,所以我們需要在Activity中添加這一句代碼,否則不會(huì)顯示虛線
android:hardwareAccelerated="false"
shape就簡(jiǎn)單介紹到這里了,我們可以使用shape畫(huà)出很多形狀,就看具體的需求了,使用shape比圖片更小。
原文鏈接:http://www.lxweimin.com/p/7a12c962b1f8