API位置 Dev Guide/Framework Topics/Application Resources/Resource Types/Drawable下
9path图片!android-sdk-windows\tools下draw9patch.bat可以编辑
Layer List
layerlist.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/faceback" />
<item android:drawable="@drawable/user"
android:top="68dp"
android:right="18dp"
android:bottom="22dp"
android:left="18dp"
android:id="@+id/userimage"
/>
</layer-list>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/layerlist"
android:id="@+id/imageView"
/>
<Button
android:background="@drawable/rectangle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="切换"
android:onClick="changeImage"
/>
</LinearLayout>
核心代码 public void changeImage(View v){
//LayerDrawable layerDrawable = (LayerDrawable) imageView.getDrawable();
//层列表 LayerDrawabl如果被使用 尝试修改会失败 所以上面的无效
LayerDrawable layerDrawable = (LayerDrawable)getResources().getDrawable(R.drawable.layerlist);
Drawable drawable = getResources().getDrawable(R.drawable.icon);
layerDrawable.setDrawableByLayerId(R.id.userimage, drawable);//指定条目的照片更换
imageView.setImageDrawable(layerDrawable);
}
State List 按照状态来控制是否显示图片
statelist.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed 按下去且可用 组合作用显示 -->
<item android:drawable="@drawable/bg_selected" android:state_enabled="true" android:state_pressed="true"/>
<!-- focused 得到焦点显示-->
<item android:drawable="@drawable/bg_selected" android:state_selected="true"/>
<!-- default 匹配所有的状态,从第一个开始寻找,如果不匹配则往后,故不能放前面 -->
<item android:drawable="@drawable/bg_normal"/>
</selector>
<Button
android:background="@drawable/statelist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="切换"
android:onClick="changeImage"
/>
Level List 级别列表图形 图片切换可以实现充电过程的显示
/drawable/res/drawable/levellist.xml 设置级别则对应显示
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/faceback"
android:maxLevel="10"
android:minLevel="0"/>
<item
android:drawable="@drawable/user"
android:maxLevel="20"
android:minLevel="11"/>
</level-list>
main.xml中 ...android:src="@drawable/levellist"...
MainActivity中
LevelListDrawable levelListDrawable = (LevelListDrawable) imageView.getDrawable();
levelListDrawable.setLevel(12);
Transition Drawable 过渡
/drawable/res/drawable/transition.xml
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/bg_normal" />
<item android:drawable="@drawable/bg_selected" />
</transition>
main.xml中 <Button android:background="@drawable/transition"...
MainActivity中
TransitionDrawable transitionDrawable = (TransitionDrawable) ((Button)v).getBackground();
transitionDrawable.startTransition(500);
Clip Drawable 可以实现进度条
main.xml中 android:src="@drawable/clip"
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/user"
android:clipOrientation="horizontal"
android:gravity="left" />
//0表示完全裁剪 不显示 10000则完全显示不裁剪
ClipDrawable clipDrawable = (ClipDrawable) imageView.getDrawable();
clipDrawable.setLevel(clipDrawable.getLevel()+1000);
Scale Drawable 缩放
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/logo"
android:scaleGravity="center_vertical|center_horizontal"
android:scaleHeight="80%"
android:scaleWidth="80%" />
Shape Drawable
=["rectangle" |"oval" |
"line" |"ring"]
> 分别是矩形 椭圆 线性 环形corners圆角
<gradient渐变padding内间距
<size 边界线的尺寸
<solid单一填充色与渐变不同时 <stroke
/drawable/res/drawable/rectangle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FFFF0000"
android:endColor="#80FF00FF"
android:angle="270"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners android:radius="8dp" />
<stroke
android:width="2dp"
android:color="#FFFFFF"
android:dashWidth="8dp"
android:dashGap="4dp" />
</shape>