一、ShapeDrawable:
通過(guò)顏色來(lái)構(gòu)造圖形炕淮,既可以為純色圖形拆火,也可以為具有漸變效果的圖形。能構(gòu)成的圖形有rectangle涂圆、oval们镜、ring、line
1.示例:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="100dp"
android:shape="ring"
android:thickness="10dp"
android:useLevel="false" >
<stroke
android:width="10dp"
android:color="@color/colorAccent" />
</shape>
2.里面屬性的介紹:
- android:shape 要繪制的形狀润歉,rectangle模狭、oval、ring踩衩、line
- <stroke> 形狀的描邊嚼鹉,有如下屬性
- android:width 描邊的寬度
- android:color 描邊的顏色
- android:dashGap 繪制虛線的線寬
- android:dashWidth 繪制虛線的線段間隔 (要繪制虛線,后兩者均不能為0)
-<solid> 純色填充驱富,android:color指定shape顏色 - <gradient> 漸變效果锚赤,與solid不可一起用,有如下屬性
- android:angle 漸變的角度褐鸥,必須為45的倍數(shù)
- android:startColor 漸變的起始顏色
- android:centerColor 漸變的中間顏色
- android:endColor 漸變的結(jié)束顏色
- android:centerX 漸變的中心點(diǎn)橫坐標(biāo)
- android:centerY 漸變的中心點(diǎn)縱坐標(biāo)
- android:gradientRadius 漸變半徑
- android:type 漸變類型线脚,linear(線性)、sweep(掃視)晶疼、radial(徑向) - <corners> 表示矩形(rectangle)的四個(gè)角的角度酒贬,不適用于其他shape 又憨,有如下屬性
- android:topLeftRadius翠霍、android:topRightRadius、android:bottomLeftRadius蠢莺、android:bottomRightRadius 分別為設(shè)置左上角寒匙、右上角、左下角、右下角的角度
- android:radius 為四角設(shè)置相同角度锄弱,優(yōu)先級(jí)低考蕾,會(huì)被其他四個(gè)屬性覆蓋 - <size> shape的寬高,對(duì)應(yīng)著android:width会宪、android:height
- shape默認(rèn)無(wú)寬高肖卧,getIntrinsicHeight、getIntrinsicWidth返回-1
- 通過(guò)size可以設(shè)置其寬高掸鹅,但作為view背景時(shí)任然會(huì)被拉伸或縮小為 view大小 - <padding> 設(shè)置容納shape的view的空白間距
二塞帐、StateListDrawable:
可以看作是一個(gè)狀態(tài)選擇器,通過(guò)view不同的狀態(tài)選擇對(duì)應(yīng)的item中的drawable顯示
1.示例:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorPrimaryDark" android:state_pressed="false"></item>
<item android:drawable="@color/colorAccent" android:state_pressed="true"></item>
</selector>
2.常見狀態(tài):
android:state_pressed 當(dāng)按住一個(gè)view時(shí)巍沙,按下的狀態(tài)
android:state_checked 當(dāng)一個(gè)view被選中時(shí)葵姥,適用于CheckBox
android:state_selected 當(dāng)一個(gè)view被選擇時(shí)
android:state_enabled 當(dāng)一個(gè)view處于可用狀態(tài)
android:state_focused 當(dāng)view獲取焦點(diǎn)
三、LayerDeawable:
表示的是一種分層的的Drawable集合句携,類似于ps中的圖層的概念榔幸,將多個(gè)drawable放在不同的層上面形成一種疊加的效果
1.示例:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/night" />
<item
android:drawable="@mipmap/photo6"
android:gravity="center" />
</layer-list>
2.常用事項(xiàng):
1、layer-list可以包含多個(gè)item矮嫉,每個(gè)item表示一個(gè)drawable削咆,并且后添加的item會(huì)覆蓋到之前添加的item上面
2、默認(rèn)情況下蠢笋,layer-list所有的drawable都會(huì)縮放至view大大小态辛,通過(guò)設(shè)施android:gravity可以調(diào)節(jié)縮放的效果
3、可以設(shè)置上下左右偏移量挺尿,android:top奏黑、android:bottom、android:left编矾、android:right
四熟史、LevelListDrawable:
表示一個(gè)drawable集合,集合中的每一個(gè)Drawable都有一個(gè)等級(jí)(level)窄俏,通過(guò)設(shè)置不同的等級(jí)蹂匹,可以使LevelListDrawable切換至不同的Drawable。等級(jí)范圍在0~10000之間凹蜈, android:maxLevel設(shè)置最大level限寞, android:minLevel設(shè)置最小level
1.示例:
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@mipmap/photo0"
android:maxLevel="20"
android:minLevel="10" />
<item
android:drawable="@mipmap/photo1"
android:maxLevel="40"
android:minLevel="30" />
</level-list>
2.通過(guò)設(shè)置level可切換不同的Drawable,在代碼中:
//將ImageView的背景切換為photo1仰坦, 35 在30~40之間
iv.setImageLevel(35);
//將ImageView的背景切換為photo0履植, 15在10~20之間
iv.setImageLevel(15);
五、InsetDrawable:
嵌入其他Drawable悄晃,并可以在四周保留一定的間距
1.示例:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@mipmap/photo6"
android:inset="20dp">
</inset>
六玫霎、ClipDrawable:
根據(jù)自己的等級(jí)(level)來(lái)對(duì)另一個(gè)Drawable進(jìn)行裁剪,裁剪的方向由android:clipOrientation、android:gravity共同決定庶近。設(shè)置level進(jìn)行裁剪翁脆,level的大小從0到10000,level為0時(shí)完全不顯示鼻种,為10000時(shí)完全顯示 :
1.示例:
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:clipOrientation="horizontal"
android:drawable="@mipmap/night"
android:gravity="right"></clip>
2.用法:
<ImageView
android:id="@+id/iv_clip"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/drawable_clip" />
通過(guò)設(shè)置level來(lái)裁剪:
ImageView iv = (ImageView) findViewById(R.id.iv_clip);
ClipDrawable drawable= (ClipDrawable) iv.getDrawable();
drawable.setLevel(5000); // 設(shè)置的level越大裁剪的范圍越小
android:clipOrientation 反番,horizontal 水平方向裁剪,vertical 垂直方向裁剪
android:gravity 叉钥,配合裁剪方向