一,*.png,*.gif,*.jpg,*.9.png
直接将对应的图片类型放入,使用即可;
二,xml文件
A:StateListDrawable资源
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:color="#f00"/> <item android:state_focused="false" android:color="#0F0"/> </selector>如上,使用<selector xxx>作为其文件的根元素,此xml文件当做drawable来使用,这里相当于选择结构,如果focused真则color="#f00",如果focused为假,则color="#0f0"
B:LayerDrawable资源
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background" android:drawable="@drawable/grow" /> <item android:id="@android:id/progress" android:drawable="@drawable/ok" /> </layer-list>以<layer-list>为根元素,这里中的item对象都会显示出来,而不是选择了;不过这里的显示是特殊的显示,每个item的作用对象是不一样的,如果作用对象相同,则显示效果将会覆盖;
C:ShapDrawable资源
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#fff" /> <padding android:bottom="7dp" android:left="7dp" android:right="7dp" android:top="7dp" /> <stroke android:width="3dp" android:color="#ff0" /> </shape>
上面的为my_shape_1.xml的drawable文件
使用在EditText布局中,
android:background="@drawable/my_shape_1" />
效果是让EditText的背景为该drawable;
还可以用gradient(渐变)属性
<gradient android:angle="45" android:endColor="#80ff00ff" android:startColor="#ff0" />能够让背景更加漂亮!
D:ClipDrawable资源
<clip xmlns:android="http://schemas.android.com/apk/res/android" android:clipOrientation="horizontal" android:drawable="@drawable/shuangta" android:gravity="center"> </clip>布局中使用
<ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/my_clip" android:id="@+id/image" android:scaleType="fitStart"/>
代码中使用
ImageView imageView=(ImageView)findViewById(R.id.image); final ClipDrawable drawable=(ClipDrawable)imageView.getDrawable();
ClipDrawable是用来对源drawable中的图片进行截取操作的;