[大白裝逼]Android好多好多的Drawable

前言

Android中提供了很多的Drawable,一般我們都是用來當做背景的,其實他還有很多其他的用途….

有什麼Drawable?

bitmapDrawable
ShapeDrawable
LayerDrawable
StateListDrawable
LevelListDrawable
TransitionDrawable
InsetDrawable
ScaleDrawable
ClipDrawable

注意

在drawable中,每個標籤<…>顯示的內容會按順序繪製,所以放在後面的標籤會覆蓋前面的繪製

bitmapDrawable

顯示圖片,並對圖片的顯示進行處理,其中也可以對點9的圖片進行處理,還有個NinePatchDrawable是用於顯示點9圖片的

<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/icon"//設置顯示的圖片爲icon
android:antialias="true"//抗鋸齒功能,最好開啓讓圖片更加平滑,雖然會模糊一點點
android:dither="true"//抖動效果,在相片像素和手機像素不一致或手機不支持其色彩模式時(有些低端機不支持ARGB8888格式,現在的應該都兼容,但是都不好說)會保持較好的顯示效果,應該開啓
android:filter="true"//過濾效果,當圖片被拉伸或壓縮時,保持良好的顯示效果,應該開啓
android:gravity="center"//當圖片大小小於容器大小時,其顯示的位置
android:tileMode="disabled"//平鋪模式,默認關閉(disabled),開啓時會覆蓋gravity的設置,會議tileMode設置的平鋪方式來填充容器,repeat(簡單平鋪,會使用多個本圖片水平和豎直方向進行填充),mirror(水平和豎直方向上的鏡面投影效果),clamp(邊緣像素會向四周擴散)
/>
<nine-path
.......//與bitmapDrawable一樣
/>

ShapeDrawable

通過顏色來構造圖形,可爲純色和漸變色

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"//圖形的形狀,默認爲rectangle(矩形),還有oval(橢圓),line(橫線)和ring(圓環).其中line和ring要使用<stroke>設置其寬度和顏色,否則顯示會出問題
(ring有特殊的屬性,其他的都沒有
android:innerRadius="圓環內半徑,與innerRadiusRatio同時存在時,以innerRadius爲準"
android:innerRadiusRatio="內半徑佔整個Drawable寬度的比例,默認爲9,內半徑=寬度/9"
android:thickness="圓環厚度,與thicknessRatio同時存在時,以thickness爲準"
android:thicknessRatio="圓環厚度佔整個Drawable寬度的比例,默認爲3,厚度=寬度/3"
android:useLevel="false"//一般爲False,在將其用於LevelListDrawable時才使用true
)
>
<corners//四個角的角度(圓角度),僅適用於矩形
android:radius="2dp"//設置4個角的角度,會被另4個單獨設置的屬性覆蓋
android:topLeftRadius="2dp"//左上角角度
android:topRightRadius="2dp"//右上角角度
android:bottomLeftRadius="2dp"/左下角角度
android:bottomRightRadius="2dp"//右下角角度
/>
<gradient//漸變色填充
android:angle="0"//漸變角度,必須爲45的倍數
android:centerX="0"//漸變中心點的橫座標
android:centerY="0"//漸變中心點的縱座標
android:startColor=" "//漸變的起始色
android:centerColor=" "//漸變中間色
android:endColor=" "//漸變的結束色
android:gradientRadius=""//漸變半徑,在type爲radial(徑向漸變)時有效
android:useLevel="false"//一般爲false,在作爲StateListDrawable時爲true
android:type="linear"//漸變類別,默認爲linear(線性漸變),radial(徑向漸變),sweep(掃描先漸變)
/>
<solid//純色填充
android:color=" "//填充的顏色
/>
<stroke//描邊,其中dashWidth和dashGap一起設置纔會有效果
android:width="2dp"//描邊的厚度
android:color=" "//描邊的顏色
android:dashWidth="5dp"//虛線描邊的每節虛線長度
android:dashGap="5dp"//虛線之間的間隔長度
/>
<padding//所在的控件留白
android:left="1dp"
android:right="1dp"
android:top="1dp"
android:bottom="1dp"
/>
<size//shape的固有大小,但將drawable設置爲背景時,其還是會根據控件的大小進行自適應
android:width="10dp"
android:height="10dp"
/>
</shape>

LayerDrawable

層次化的drawable集合,將不同的drawable放到不同的層去顯示達到疊加後的效果

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item//一個item爲一層顯示,後面的item後遮蓋上面的item
android:drawable=" "//顯示的drawable
android:id=" "//設置id
android:top=" "//頂部偏移量
android:bottom=" "//底部偏移量
android:left=" "//左邊偏移量
android:right=" "//右邊偏移量
/>
<item
...>
<shape ...>//也可以在item中直接自定義drawable
</item>
</layer-list>

StateListDrawable

根據view的不同狀態來設置不同的背景

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
android:constantSize="false"//固有大小是否隨drawable的改變而改變,默認false爲改變
android:dither="true"//抖動效果
android:variablePadding="true"//其padding是否隨drawable的改變而改變,默認爲true不改變,其padding爲所有drawable的最大值
>
<item//一個item表示一種狀態 
android:state_pressed="true"//按下爲鬆開的狀態,false爲不處於此狀態
android:drawable=" "//此狀態下要先顯示的drawable
/>
//其他的狀態
//state_focused:view獲取焦點
//state_selected:用戶選擇了view
//state_checked:用戶選中了view,使用與checkBox
//state_enabled:當前view可用
//若沒有註明狀態,則爲默認狀態
</selector>

LevelListDrawable

根據當前等級來顯示不同的drawable,可通過Drawable或者ImageView的setImageLevel來設置等級,默認爲0,最大等級爲10000.

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item//一個item爲一個drawable,在其最大值和最小值之間的等級就會顯示他
android:drawable=" "//顯示的drawable
android:maxLevel=" "//最大的等級,默認爲10000
android:minLevel=" "//最小的等級,默認爲0
/>
</level-list>

TransitionDrawable

實現兩個drawable之間的淡入淡出的轉化效果

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable=" "/>//一個item代表一個
<item android:drawable=" "/>
</transition>

將上面的drawable設置爲View的背景,要切換時在代碼中調用如下代碼:

TextView tv = findViewById(R.id.tv);
TransitionDrawable drawable = (TransitionDrawable)tv.getBackGround();
drawable.startTransition(1000);//開始轉換動畫,進行兩個item的切換

InsetDrawable

將其他drawable內嵌到自身中並留白,可使drawable做背景時,比實際區域小

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable=" "//顯示的drawable
android:insetTop=" "//留白
android:insetBottom=" "
android:insetLeft=" "
android:insetRight=" "
/>

ScaleDrawable

根據等級縮放,其等級爲0時不會顯示,等級越大顯示的越大,縮放越大顯示的越小.需要注意的是,其縮放公式(僞代碼)爲

if(level != 0){
width -= width * (10000-level) * scaleWidth/10000
}
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable=" "//顯示的drawable
android:scaleGravity=" "//與gravity一樣
android:scaleHeight="25%"//按比例縮放高
android:scaleWidth="25%"//按比例縮放寬
/>

以上的drawable沒有設置等級,無法顯示,可在代碼中設置等級

((ScaleDrawable)view.getBackground()).setlevel(1);//設置drawable的等級

ClipDrawable

用於裁剪drawable,其等級表示裁剪剩餘多少,如0爲全裁剪,10000爲不裁剪,666爲裁剪(666/10000)*100%

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable=" "//處理的drawable
android:clipOrientation=" "//裁剪方向,horizontal(水平方向),vertical(豎直方向)
android:gravity=" "//裁剪方式
/>

裁剪方式比較多,且可以多個使用”|”一起組合
top : 放在頂部,爲vertical時,從底部開始裁剪,不改變大小
bottom : 放在底部,爲vertical時,從頂部開始裁剪,不改變大小
left : 放在左邊,爲horizontal時,從右邊開始裁剪,不改變大小
right : 放在右邊,爲horizontal時,從左邊開始裁剪,不改變大小
center_vertical : 豎直居中,爲vertical時,上下同時開始裁剪,不改變大小
fill_vertical : 豎直方向上填充容器,會改變大小,當等級爲0時會不顯示
center_horizontal : 水平居中,horizontal時,從左右兩邊同時裁剪,不改變大小
fill_horizontal : 水平方向上填充容器,會改變大小,等級爲0時不顯示
center : 水平和豎直方向都居中,爲vertical時,上下同時裁剪,爲horizontal時,左右同時裁剪,不改變大小
fill : 水平和豎直方向同時填充,會改變大小,等級爲0時不顯示
clip_vertical : 豎直方向的裁剪
clip_horizontal : 水平方向的裁剪

小結

Drawable一般用來作爲控件的背景,而android提供了很多的Drawable,這些已經可以滿足一般開發的需求,並且使用drawable可以很輕鬆的實現我們想要的效果~~

個人博客:https://lewis-v.github.io/

公衆號 個人公衆號

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章