更多Android總結(jié)知識(shí)點(diǎn)
Android的八種對(duì)話(huà)框的實(shí)現(xiàn)
Android之系統(tǒng)給我們提供的常見(jiàn)的Activity
本節(jié)前言
講本節(jié)之前先講一下Drawable xml的布局,為什么要先講這個(gè)呢,因?yàn)槲野l(fā)現(xiàn)后面講解Ui很多布局里面是要用到這個(gè)的,怕大家看不懂,所以我們今天就先講一下Drawable的知識(shí)點(diǎn)
Android中給我們提供了多達(dá)13種的 Drawable,本節(jié)我們就來(lái)一個(gè)個(gè)擼一遍!

Drawable資源使用注意事項(xiàng)
(1)Drawable分為兩種: 一種是我們普通的圖片資源,在A(yíng)ndroid Studio中我們一般放到res/mipmap目錄下, 和以前的Eclipse不一樣哦!另外我們?nèi)绻压こ糖袚Q成Android項(xiàng)目模式,我們直接 往mipmap目錄下丟圖片即可,AS會(huì)自動(dòng)分hdpi,xhdpi...! 另一種是我們編寫(xiě)的XML形式的Drawable資源,我們一般把他們放到res/drawable目錄 下,比如最常見(jiàn)的按鈕點(diǎn)擊背景切換的Selctor!
(2)在XML我們直接通過(guò)@mipmap或者@drawable設(shè)置Drawable即可 比如: android:background = "@mipmap/iv_icon_zhu" / "@drawable/btn_back_selctor" 而在Java代碼中我們可以通過(guò)Resource的getDrawable(R.mipmap.xxx)可以獲得drawable資源 如果是為某個(gè)控件設(shè)置背景,比如ImageView,我們可以直接調(diào)用控件.getDrawale()同樣 可以獲得drawable對(duì)象!
(3)Android中drawable中的資源名稱(chēng)有約束,必須是:[a-z0-9_.](即:只能是字母數(shù)字及和.), 而且不能以數(shù)字開(kāi)頭,否則編譯會(huì)報(bào)錯(cuò): Invalid file name: must contain only [a-z0-9.]! 小寫(xiě)?。。。?!小寫(xiě)?。?!小寫(xiě)!——重要事情說(shuō)三遍~
好的,要注意的地方大概就這些,下面我們來(lái)對(duì)Android中給我們提供的13種Drawable進(jìn)行學(xué)習(xí)!
1.ColorDrawable
最簡(jiǎn)單的一種Drawable,當(dāng)我們將ColorDrawable繪制到Canvas(畫(huà)布)上的時(shí)候, 會(huì)使用一種固定的顏色來(lái)填充Paint,然后在畫(huà)布上繪制出一片單色區(qū)域!
1).Java中定義ColorDrawable:
ColorDrawable drawable = new ColorDrawable(0xffff2200);
txtShow.setBackground(drawable);
2).在xml中定義ColorDrawable:
<?xml version="1.0" encoding="utf-8"?>
<color
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#FF0000"/>
當(dāng)然上面這些用法,其實(shí)用得不多,更多的時(shí)候我們是在res/values目錄下創(chuàng)建一個(gè)color.xml 文件,然后把要用到的顏色值寫(xiě)到里面,需要的時(shí)候通過(guò)@color獲得相應(yīng)的值,比如:
3).建立一個(gè)color.xml文件
比如:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="material_grey_100">#fff5f5f5</color>
<color name="material_grey_300">#ffe0e0e0</color>
<color name="material_grey_50">#fffafafa</color>
<color name="material_grey_600">#ff757575</color>
<color name="material_grey_800">#ff424242</color>
<color name="material_grey_850">#ff303030</color>
<color name="material_grey_900">#ff212121</color>
</resources>
然后如果是在xml文件中話(huà)我們可以通過(guò)@color/xxx獲得對(duì)應(yīng)的color值 如果是在Java中:
int mycolor = getResources().getColor(R.color.mycolor);
btn.setBackgroundColor(mycolor);
ps:另外有一點(diǎn)要注意,如果我們?cè)贘ava中直接定義顏色值的話(huà),要加上0x,而且不能把透明度漏掉:
int mycolor = 0xff123456;
btn.setBackgroundColor(mycolor);
2.NiewPatchDrawable(這里介紹一下,理解一下就可以了)
顧名思義就是.9圖片了,Android FrameWork在顯示點(diǎn)九圖時(shí)使用了高效的 圖形優(yōu)化算法,我們不需要特殊的處理,就可以實(shí)現(xiàn)圖片拉伸的自適應(yīng)~ 另外在使用AS的時(shí)候要注意以下幾點(diǎn):
點(diǎn)9圖不能放在mipmap目錄下,而需要放在drawable目錄下!
3.ShapeDrawable
形狀的Drawable咯,定義基本的幾何圖形,如(矩形,圓形,線(xiàn)條等),根元素是<shape../> 節(jié)點(diǎn)比較多,相關(guān)的節(jié)點(diǎn)如下:
① <shape>:
visible[?v?z?bl]:設(shè)置是否可見(jiàn)
shape:形狀,可選:rectangle['rekt??g(?)] (矩形,包括正方形),oval(橢圓,包括圓),line(線(xiàn)段),ring(環(huán)形)
innerRadiusRatio:當(dāng)shape為ring才有效,表示環(huán)內(nèi)半徑所占半徑的比率,如果設(shè)置了innerRadius, 他會(huì)被忽略
innerRadius:當(dāng)shape為ring才有效,表示環(huán)的內(nèi)半徑的尺寸
thicknessRatio:當(dāng)shape為ring才有效,表環(huán)厚度占半徑的比率
thickness:當(dāng)shape為ring才有效,表示環(huán)的厚度,即外半徑與內(nèi)半徑的差
useLevel:當(dāng)shape為ring才有效,表示是否允許根據(jù)level來(lái)顯示環(huán)的一部分
②<size>:
width:圖形形狀寬度
height:圖形形狀高度
③<gradient>:后面GradientDrawable再講~
④<solid>
color:背景填充色,設(shè)置solid后會(huì)覆蓋gradient設(shè)置的所有效果!!!!!!
⑤<stroke>
width:邊框的寬度
color:邊框的顏色
dashWidth:邊框虛線(xiàn)段的長(zhǎng)度
dashGap:邊框的虛線(xiàn)段的間距
⑥<conner>
~ radius:圓角半徑,適用于上下左右四個(gè)角
~ topLeftRadius,topRightRadius,BottomLeftRadius,tBottomRightRadius: 依次是左上,右上,左下,右下的圓角值,按自己需要設(shè)置!
⑦<padding>
left,top,right,bottm:依次是左上右下方向上的邊距!
使用示例:(后面的TextView會(huì)講)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 設(shè)置一個(gè)黑色邊框 -->
<stroke android:width="2px" android:color="#000000"/>
<!-- 漸變 -->
<gradient
android:angle="270"
android:endColor="#C0C0C0"
android:startColor="#FCD209" />
<!-- 設(shè)置一下邊距,讓空間大一點(diǎn) -->
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp"/>
</shape>
4.GradientDrawable
一個(gè)具有漸變區(qū)域的Drawable,可以實(shí)現(xiàn)線(xiàn)性漸變,發(fā)散漸變和平鋪漸變效果 核心節(jié)點(diǎn):<gradient/>,有如下可選屬性:
startColor:漸變的起始顏色
centerColor:漸變的中間顏色
endColor:漸變的結(jié)束顏色
type:漸變類(lèi)型,可選(linear,radial,sweep), 線(xiàn)性漸變(可設(shè)置漸變角度),發(fā)散漸變(中間向四周發(fā)散),平鋪漸變
centerX:漸變中間亞瑟的x坐標(biāo),取值范圍為:0~1
centerY:漸變中間顏色的Y坐標(biāo),取值范圍為:0~1
angle:只有l(wèi)inear類(lèi)型的漸變才有效,表示漸變角度,必須為45的倍數(shù)哦
gradientRadius:只有radial和sweep類(lèi)型的漸變才有效,radial必須設(shè)置,表示漸變效果的半徑
useLevel:判斷是否根據(jù)level繪制漸變效果
代碼示例:(三種漸變效果的演示):
運(yùn)行效果圖:

先在drawable下創(chuàng)建三個(gè)漸變xml文件:
(線(xiàn)性漸變)gradient_linear.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<gradient
android:angle="90"
android:centerColor="#FFEB82"
android:endColor="#35B2DE"
android:startColor="#DEACAB" />
<stroke
android:dashGap="5dip"
android:dashWidth="4dip"
android:width="3dip"
android:color="#fff" />
</shape>
(發(fā)散漸變)gradient_radial.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dip"
android:shape="ring"
android:thickness="70dip"
android:useLevel="false" >
<gradient
android:centerColor="#FFEB82"
android:endColor="#35B2DE"
android:gradientRadius="70"
android:startColor="#DEACAB"
android:type="radial"
android:useLevel="false" />
</shape>
(平鋪漸變)gradient_sweep.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadiusRatio="8"
android:shape="ring"
android:thicknessRatio="3"
android:useLevel="false" >
<gradient
android:centerColor="#FFEB82"
android:endColor="#35B2DE"
android:startColor="#DEACAB"
android:type="sweep"
android:useLevel="false" />
</shape>
5.BitmapDrawable
對(duì)Bitmap的一種封裝,可以設(shè)置它包裝的bitmap在BitmapDrawable區(qū)域中的繪制方式,有: 平鋪填充,拉伸填或保持圖片原始大小!以<bitmap>為根節(jié)點(diǎn)! 可選屬性如下:
src:圖片資源~
antialias['?nti'eili?s]:是否支持抗鋸齒
filter:是否支持位圖過(guò)濾,支持的話(huà)可以是圖批判顯示時(shí)比較光滑
dither:是否對(duì)位圖進(jìn)行抖動(dòng)處理
gravity:若位圖比容器小,可以設(shè)置位圖在容器中的相對(duì)位置tileMode:指定圖片平鋪填充容器的模式,設(shè)置這個(gè)的話(huà),gravity屬性會(huì)被忽略,有以下可選值: disabled(整個(gè)圖案拉伸平鋪),clamp(原圖大小), repeat(平鋪),mirror(鏡像平鋪)
對(duì)應(yīng)的效果圖:

①XML定義BitmapDrawable:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true"
android:src="@drawable/ic_launcher"
android:tileMode="mirror" />
②實(shí)現(xiàn)相同效果的Java代碼:
BitmapDrawable bitDrawable = new BitmapDrawable(bitmap);
bitDrawable.setDither(true);
bitDrawable.setTileModeXY(TileMode.MIRROR,TileMode.MIRROR);
6.InsetDrawable
表示把一個(gè)Drawable嵌入到另外一個(gè)Drawable的內(nèi)部,并且在內(nèi)部留一些間距, 類(lèi)似與Drawable的padding屬性,但padding表示的是Drawable的內(nèi)容與Drawable本身的邊距! 而InsetDrawable表示的是兩個(gè)Drawable與容器之間的邊距,當(dāng)控件需要的背景比實(shí)際的邊框 小的時(shí)候,比較適合使用InsetDrawable,比如使用這個(gè)可以解決我們自定義Dialog與屏幕之間 的一個(gè)間距問(wèn)題,相信做過(guò)的朋友都知道,即使我們?cè)O(shè)置了layout_margin的話(huà)也是沒(méi)用的,這個(gè) 時(shí)候就可以用到這個(gè)InsetDrawable了!只需為InsetDrawable設(shè)置一個(gè)insetXxx設(shè)置不同 方向的邊距,然后為設(shè)置為Dialog的背景即可!
相關(guān)屬性如下:
- 1.drawable:引用的Drawable,如果為空,必須有一個(gè)Drawable類(lèi)型的子節(jié)點(diǎn)!
- 2.visible:設(shè)置Drawable是否額空間
- 3.insetLeft,insetRight,insetTop,insetBottm:設(shè)置左右上下的邊距
①XML中使用:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/test1"
android:insetBottom="10dp"
android:insetLeft="10dp"
android:insetRight="10dp"
android:insetTop="10dp" />
在Java代碼中使用:
InsetDrawable insetDrawable = new InsetDrawable(getResources() .getDrawable(R.drawable.test1), 10, 10, 10, 10);
使用效果圖:

7.ClipDrawable
Clip [kl?p]可以譯為剪的意思,我們可以把ClipDrawable理解為從位圖上剪下一個(gè)部分; Android中的進(jìn)度條就是使用ClipDrawable來(lái)實(shí)現(xiàn)的,他根據(jù)設(shè)置level的值來(lái)決定剪切 區(qū)域的大小,根節(jié)點(diǎn)是<clip>
相關(guān)屬性如下:
- clipOrietntion:設(shè)置剪切的方向,可以設(shè)置水平和豎直2個(gè)方向
- gravity:從那個(gè)位置開(kāi)始裁剪
- drawable:引用的drawable資源,為空的話(huà)需要有一個(gè)Drawable類(lèi)型的子節(jié)點(diǎn) ps:這個(gè)Drawable類(lèi)型的子節(jié)點(diǎn):就是在<clip里>加上這樣的語(yǔ)句: 這樣...
使用示例:
核心:通過(guò)代碼修改ClipDrawable的level的值!Level的值是0~10000!
運(yùn)行效果圖:

代碼實(shí)現(xiàn):
①定義一個(gè)ClipDrawable的資源xml:
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:clipOrientation="horizontal"
android:drawable="@mipmap/ic_bg_meizi"
android:gravity="left" />
②在activity_main主布局文件中設(shè)置一個(gè)ImageView,將src設(shè)置為clipDrawable! 記住是src哦,如果你寫(xiě)成了blackground的話(huà)可是會(huì)報(bào)空指針的哦!!!!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/img_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/clip_bg" />
</LinearLayout>
③MainActivity.java通過(guò)setLevel設(shè)置截取區(qū)域大小:
public class MainActivity extends AppCompatActivity {
private ImageView img_show;
private ClipDrawable cd;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x123) {
cd.setLevel(cd.getLevel() + 500);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img_show = (ImageView) findViewById(R.id.img_show);
// 核心實(shí)現(xiàn)代碼
cd = (ClipDrawable) img_show.getDrawable();
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(0x123);
if (cd.getLevel() >= 10000) {
timer.cancel();
}
}
}, 0, 300);
}
}
8.RotateDrawable
用來(lái)對(duì)Drawable進(jìn)行旋轉(zhuǎn),也是通過(guò)setLevel來(lái)控制旋轉(zhuǎn)的,最大值也是:10000
相關(guān)屬性如下:
fromDegrees:起始的角度,,對(duì)應(yīng)最低的level值,默認(rèn)為0
toDegrees:結(jié)束角度,對(duì)應(yīng)最高的level值,默認(rèn)360
pivotX:設(shè)置參照點(diǎn)的x坐標(biāo),取值為0~1,默認(rèn)是50%,即0.5
pivotY:設(shè)置參照點(diǎn)的Y坐標(biāo),取值為0~1,默認(rèn)是50%,即0.5 ps:如果出現(xiàn)旋轉(zhuǎn)圖片顯示不完全的話(huà)可以修改上述兩個(gè)值解決!
drawable:設(shè)置位圖資源
visible:設(shè)置drawable是否可見(jiàn)!
角度圖如下:

使用示例:
運(yùn)行效果圖:

代碼實(shí)現(xiàn):
在第三點(diǎn)的clipDrawable上做一點(diǎn)點(diǎn)修改即可!
①定義一個(gè)rotateDrawable資源文件:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@mipmap/ic_launcher"
android:fromDegrees="-180"
android:pivotX="50%"
android:pivotY="50%" />
②activity_main.xml中修改下src指向上述drawable即可,MainActivity只需要把ClipDrawable 改成rotateDrawable即可!
public class MainActivity extends AppCompatActivity {
private ImageView img_show;
private RotateDrawable cd;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x123) {
if (cd.getLevel() >= 10000)
Toast.makeText(MainActivity.this, "轉(zhuǎn)完了~",
Toast.LENGTH_LONG).show();
cd.setLevel(cd.getLevel() + 400);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img_show = (ImageView) findViewById(R.id.img_show);
// 核心實(shí)現(xiàn)代碼
cd = (RotateDrawable) img_show.getDrawable();
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(0x123);
if (cd.getLevel() >= 10000) {
timer.cancel();
}
}
}, 0, 100);
}
}
9.AnimationDrawable(此篇會(huì)在后面詳細(xì)介紹)
AnimationDrawable是用來(lái)實(shí)現(xiàn)Android中幀動(dòng)畫(huà)的,就是把一系列的 Drawable,按照一定得順序一幀幀地播放;Android中動(dòng)畫(huà)比較豐富,有傳統(tǒng)補(bǔ)間動(dòng)畫(huà),平移, 縮放等等效果,但是這里我們僅僅介紹這個(gè)AnimationDrawable實(shí)現(xiàn)幀動(dòng)畫(huà),關(guān)于alpha,scale, translate,rotate等,后續(xù)在動(dòng)畫(huà)章節(jié)再進(jìn)行詳細(xì)的介紹~
我們這里使用<animation-list>作為根節(jié)點(diǎn)
相關(guān)屬性方法:
oneshot:設(shè)置是否循環(huán)播放,false為循環(huán)播放!!!
duration:幀間隔時(shí)間,通常我們會(huì)設(shè)置為300毫秒
我們獲得AniamtionDrawable實(shí)例后,需要調(diào)用它的start()方法播放動(dòng)畫(huà),另外要注意 在OnCreate()方法中調(diào)用的話(huà),是沒(méi)有任何效果的,因?yàn)閂iew還沒(méi)完成初始化,我們可以 用簡(jiǎn)單的handler來(lái)延遲播放動(dòng)畫(huà)!當(dāng)然還有其他的方法
10.LayerDrawable
層圖形對(duì)象,包含一個(gè)Drawable數(shù)組,然后按照數(shù)組對(duì)應(yīng)的順序來(lái)繪制他們,索引 值最大的Drawable會(huì)被繪制在最上層!雖然這些Drawable會(huì)有交叉或者重疊的區(qū)域,但 他們位于不同的層,所以并不會(huì)相互影響,以<layer-list>作為根節(jié)點(diǎn)!
相關(guān)屬性如下:
drawable:引用的位圖資源,如果為空需要有一個(gè)Drawable類(lèi)型的子節(jié)點(diǎn)
left:層相對(duì)于容器的左邊距
right:層相對(duì)于容器的右邊距
top:層相對(duì)于容器的上邊距
bottom:層相對(duì)于容器的下邊距
id:層的id
使用示例:
運(yùn)行效果圖:

代碼實(shí)現(xiàn):
非常簡(jiǎn)單,結(jié)合前面學(xué)習(xí)的shapeDrawable和ClipDrawable:
layerList_one.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape android:shape="rectangle">
<solid android:color="#C2C2C1" />
<corners android:radius="50dp" />
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape android:shape="rectangle">
<solid android:color="#BCDA73" />
<corners android:radius="50dp" />
</shape>
</clip>
</item>
</layer-list>
然后在布局文件中添加一個(gè)Seekbar,內(nèi)容如下:
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"
android:indeterminateOnly="false"
android:maxHeight="10dp"
android:minHeight="5dp"
android:progressDrawable="@drawable/layerlist_one"
android:thumb="@drawable/shape_slider" />
我們還可以弄個(gè)層疊圖片的效果:
運(yùn)行效果圖:

實(shí)現(xiàn)代碼:
層疊圖片的layerlist_two.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:gravity="center"
android:src="@mipmap/ic_bg_ciwei" />
</item>
<item
android:left="25dp"
android:top="25dp">
<bitmap
android:gravity="center"
android:src="@mipmap/ic_bg_ciwei" />
</item>
<item
android:left="50dp"
android:top="50dp">
<bitmap
android:gravity="center"
android:src="@mipmap/ic_bg_ciwei" />
</item>
</layer-list>
然后在activity_main.xml里加個(gè)ImageView,內(nèi)容如下:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/layerlist_two"/>
11.TransitionDrawable
LayerDrawable的一個(gè)子類(lèi),TransitionDrawable只管理兩層的Drawable!兩層!兩層! 并且提供了透明度變化的動(dòng)畫(huà),可以控制一層Drawable過(guò)度到另一層Drawable的動(dòng)畫(huà)效果。 根節(jié)點(diǎn)為<transition>,記住只有兩個(gè)Item,多了也沒(méi)用,屬性和LayerDrawable差不多, 我們需要調(diào)用startTransition方法才能啟動(dòng)兩層間的切換動(dòng)畫(huà); 也可以調(diào)用reverseTransition()方法反過(guò)來(lái)播放:
使用示例:
運(yùn)行效果圖:

實(shí)現(xiàn)代碼:
在res/drawable創(chuàng)建一個(gè)TransitionDrawable的xml文件
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@mipmap/ic_bg_meizi1"/>
<item android:drawable="@mipmap/ic_bg_meizi2"/>
</transition>
然后布局文件里加個(gè)ImageView,然后把src設(shè)置成上面的這個(gè)drawable 然后MainActivity.java內(nèi)容如下:
public class MainActivity extends AppCompatActivity {
private ImageView img_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img_show = (ImageView) findViewById(R.id.img_show);
TransitionDrawable td = (TransitionDrawable) img_show.getDrawable();
td.startTransition(3000);
//你可以可以反過(guò)來(lái)播放,使用reverseTransition即可~
//td.reverseTransition(3000);
}
}
12.LevelListDrawable
用來(lái)管理一組Drawable的,我們可以為里面的drawable設(shè)置不同的level, 當(dāng)他們繪制的時(shí)候,會(huì)根據(jù)level屬性值獲取對(duì)應(yīng)的drawable繪制到畫(huà)布上,根節(jié)點(diǎn) 為:<level-list>他并沒(méi)有可以設(shè)置的屬性,我們能做的只是設(shè)置每個(gè)<item> 的屬性!
item可供設(shè)置的屬性如下:
drawable:引用的位圖資源,如果為空 需要有一個(gè)Drawable類(lèi)型的子節(jié)點(diǎn)
minlevel:level對(duì)應(yīng)的最小值
maxlevel:level對(duì)應(yīng)的最大值
使用示例:
運(yùn)行效果圖:

代碼實(shí)現(xiàn):
通過(guò)shapeDrawable畫(huà)圓,一式五份,改下寬高即可:
shape_cir1.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#2C96ED"/>
<size android:height="20dp" android:width="20dp"/>
</shape>
接著到LevelListDrawable,這里我們?cè)O(shè)置五層:
level_cir.xml:
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/shape_cir1" android:maxLevel="2000"/>
<item android:drawable="@drawable/shape_cir2" android:maxLevel="4000"/>
<item android:drawable="@drawable/shape_cir3" android:maxLevel="6000"/>
<item android:drawable="@drawable/shape_cir4" android:maxLevel="8000"/>
<item android:drawable="@drawable/shape_cir5" android:maxLevel="10000"/>
</level-list>
最后MainActivity寫(xiě)如下代碼:
public class MainActivity extends AppCompatActivity {
private ImageView img_show;
private LevelListDrawable ld;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 0x123) {
if (ld.getLevel() > 10000) ld.setLevel(0);
img_show.setImageLevel(ld.getLevel() + 2000);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img_show = (ImageView) findViewById(R.id.img_show);
ld = (LevelListDrawable) img_show.getDrawable();
img_show.setImageLevel(0);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(0x123);
}
}, 0, 100);
}
}
13.StateListDrawable
好了終于迎來(lái)了最后一個(gè)drawable:StateListDrawable,這個(gè)名字看上去模式,其實(shí)我們 以前就用到了,還記得為按鈕設(shè)置不同狀態(tài)的drawable的<selctor>嗎?沒(méi)錯(cuò),用到的 就是這個(gè)StateListDrawable!
可供設(shè)置的屬性如下:
drawable:引用的Drawable位圖,我們可以把他放到最前面,就表示組件的正常狀態(tài)~
state_focused:是否獲得焦點(diǎn)
state_window_focused:是否獲得窗口焦點(diǎn)
state_enabled:控件是否可用
state_checkable:控件可否被勾選,eg:checkbox
state_checked:控件是否被勾選
state_selected:控件是否被選擇,針對(duì)有滾輪的情況
state_pressed:控件是否被按下
state_active:控件是否處于活動(dòng)狀態(tài),eg:slidingTab
state_single:控件包含多個(gè)子控件時(shí),確定是否只顯示一個(gè)子控件
state_first:控件包含多個(gè)子控件時(shí),確定第一個(gè)子控件是否處于顯示狀態(tài)
state_middle:控件包含多個(gè)子控件時(shí),確定中間一個(gè)子控件是否處于顯示狀態(tài)
state_last:控件包含多個(gè)子控件時(shí),確定最后一個(gè)子控件是否處于顯示狀態(tài)
使用示例:
那就來(lái)寫(xiě)個(gè)簡(jiǎn)單的圓角按鈕吧!
運(yùn)行效果圖:

代碼實(shí)現(xiàn):
那就先通過(guò)shapeDrawable來(lái)畫(huà)兩個(gè)圓角矩形,只是顏色不一樣而已:
shape_btn_normal.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#DD788A"/>
<corners android:radius="5dp"/>
<padding android:top="2dp" android:bottom="2dp"/>
</shape>
接著我們來(lái)寫(xiě)個(gè)selctor:selctor_btn.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/shape_btn_pressed"/>
<item android:drawable="@drawable/shape_btn_normal"/>
</selector>
然后按鈕設(shè)置android:background="@drawable/selctor_btn"就可以了~ 你可以根據(jù)自己需求改成矩形或者橢圓,圓形等!