一:BitmapDrawable
<?xml version="1.0" encoding="utf-8"?> <!--antialias:抗锯齿--> <!--dither:抖动--> <!--src:图片的地址--> <!--tileMode:平铺模式; 可选值: disable:关闭平铺模式 repeat:水平和垂直方向平铺 mirror:水平和垂直方向平铺的基础上加上倒影 clamp:四周扩散到周围区域--> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/wifi" android:antialias="true" android:dither="true" android:gravity="center" android:tileMode="repeat" > </bitmap>
效果图:
二:ShapeDrawable
ShapeDrawable可以渐变也可以纯色填充
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"><!--选择形状--> <!--angle:渐变角度,必须为45的倍数--> <!--type:渐变类别,可选值:linear:线性渐变;radial:径向渐变;sweep:扫描线渐变--> <gradient android:angle="45" android:centerX="100" android:centerY="100" android:startColor="#ff0000" android:centerColor="#ffff00" android:endColor="#66ffff" android:type="linear"/> <stroke android:width="5dp" android:color="#000000" android:dashWidth="2dp" android:dashGap="3dp"/> </shape>
效果图:
三:StteListDrawable
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize="false" android:dither="true" android:variablePadding="false"> <!--按下时的状态--> <item android:state_pressed="true" android:drawable="@drawable/zreo"/> <!--获取焦点时的状态--> <item android:state_focused="true" android:drawable="@drawable/one"/> <!--普通时刻状态--> <item android:drawable="@drawable/two"/> </selector>
四:LevelListDrawable
根据不同的等级切换到不同的Drawable
等级范围:0~10000
<?xml version="1.0" encoding="utf-8"?> <level-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/zero" android:maxLevel="0"/> <item android:drawable="@drawable/one" android:maxLevel="1"/> <item android:drawable="@drawable/two" android:maxLevel="2"/> </level-list>
五:ScaleDrawable
根据等级缩放Drawable
<scale xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/no" android:scaleGravity="center" android:scaleWidth="60%" android:scaleHeight="60%"> </scale>
View scale = findViewById(R.id.test); ScaleDrawable scaleDrawable = (ScaleDrawable) scale.getBackground(); scaleDrawable.setLevel(5000);
六:ClipDrawable
根据等级裁剪Drawable
<!--clipOrientation:裁剪方向--> <clip xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/not" android:clipOrientation="vertical" android:gravity="center"> </clip>
ImageView scale = (ImageView) findViewById(R.id.test); ClipDrawable clipDrawable = (ClipDrawable) scale.getDrawable(); clipDrawable.setLevel(5000);
原图
裁剪之后的图